valid-services-property.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @fileoverview Ensures that property accesses on Services.<alias> are valid.
  3. * Although this largely duplicates the valid-services rule, the checks here
  4. * require an objdir and a manual run.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. "use strict";
  11. const helpers = require("../helpers");
  12. function findInterfaceNames(name) {
  13. let interfaces = [];
  14. for (let [key, value] of Object.entries(helpers.servicesData)) {
  15. if (value == name) {
  16. interfaces.push(key);
  17. }
  18. }
  19. return interfaces;
  20. }
  21. function isInInterface(interfaceName, name) {
  22. let interfaceDetails = helpers.xpidlData.get(interfaceName);
  23. // TODO: Bug 1790261 - check only methods if the expression is callable.
  24. if (interfaceDetails.methods.some(m => m.name == name)) {
  25. return true;
  26. }
  27. if (interfaceDetails.consts.some(c => c.name == name)) {
  28. return true;
  29. }
  30. if (interfaceDetails.parent) {
  31. return isInInterface(interfaceDetails.parent, name);
  32. }
  33. return false;
  34. }
  35. module.exports = {
  36. meta: {
  37. docs: {
  38. url:
  39. "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/valid-services-property.html",
  40. },
  41. messages: {
  42. unknownProperty:
  43. "Unknown property access Services.{{ alias }}.{{ propertyName }}, Interfaces: {{ checkedInterfaces }}",
  44. },
  45. type: "problem",
  46. },
  47. create(context) {
  48. let servicesInterfaceMap = helpers.servicesData;
  49. let serviceAliases = new Set([
  50. ...Object.values(servicesInterfaceMap),
  51. // This is defined only for Android, so most builds won't pick it up.
  52. "androidBridge",
  53. // These are defined without interfaces and hence are not in the services map.
  54. "cpmm",
  55. "crashmanager",
  56. "mm",
  57. "ppmm",
  58. // The new xulStore also does not have an interface.
  59. "xulStore",
  60. ]);
  61. return {
  62. MemberExpression(node) {
  63. if (node.computed || node.object.type !== "Identifier") {
  64. return;
  65. }
  66. let mainNode;
  67. if (node.object.name == "Services") {
  68. mainNode = node;
  69. } else if (
  70. node.property.name == "Services" &&
  71. node.parent.type == "MemberExpression"
  72. ) {
  73. mainNode = node.parent;
  74. } else {
  75. return;
  76. }
  77. let alias = mainNode.property.name;
  78. if (!serviceAliases.has(alias)) {
  79. return;
  80. }
  81. if (
  82. mainNode.parent.type == "MemberExpression" &&
  83. !mainNode.parent.computed
  84. ) {
  85. let propertyName = mainNode.parent.property.name;
  86. if (propertyName == "wrappedJSObject") {
  87. return;
  88. }
  89. let interfaces = findInterfaceNames(alias);
  90. if (!interfaces.length) {
  91. return;
  92. }
  93. let checkedInterfaces = [];
  94. for (let item of interfaces) {
  95. if (isInInterface(item, propertyName)) {
  96. return;
  97. }
  98. checkedInterfaces.push(item);
  99. }
  100. context.report({
  101. node,
  102. messageId: "unknownProperty",
  103. data: {
  104. alias,
  105. propertyName,
  106. checkedInterfaces,
  107. },
  108. });
  109. }
  110. },
  111. };
  112. },
  113. };