reject-importGlobalProperties.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * @fileoverview Reject use of Cu.importGlobalProperties
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. */
  8. "use strict";
  9. const path = require("path");
  10. const privilegedGlobals = Object.keys(
  11. require("../environments/privileged.js").globals
  12. );
  13. module.exports = {
  14. meta: {
  15. docs: {
  16. url:
  17. "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/reject-importGlobalProperties.html",
  18. },
  19. messages: {
  20. unexpectedCall: "Unexpected call to Cu.importGlobalProperties",
  21. unexpectedCallCuWebIdl:
  22. "Unnecessary call to Cu.importGlobalProperties for {{name}} (webidl names are automatically imported)",
  23. unexpectedCallXPCOMWebIdl:
  24. "Unnecessary call to XPCOMUtils.defineLazyGlobalGetters for {{name}} (webidl names are automatically imported)",
  25. },
  26. schema: [
  27. {
  28. enum: ["everything", "allownonwebidl"],
  29. },
  30. ],
  31. type: "problem",
  32. },
  33. create(context) {
  34. return {
  35. CallExpression(node) {
  36. if (
  37. node.callee.type !== "MemberExpression" ||
  38. // TODO Bug 1501127: sjs files have their own sandbox, and do not inherit
  39. // the Window backstage pass directly.
  40. path.extname(context.getFilename()) == ".sjs"
  41. ) {
  42. return;
  43. }
  44. let memexp = node.callee;
  45. if (
  46. memexp.object.type === "Identifier" &&
  47. // Only Cu, not Components.utils as `use-cc-etc` handles this for us.
  48. memexp.object.name === "Cu" &&
  49. memexp.property.type === "Identifier" &&
  50. memexp.property.name === "importGlobalProperties"
  51. ) {
  52. if (context.options.includes("allownonwebidl")) {
  53. for (let element of node.arguments[0].elements) {
  54. if (privilegedGlobals.includes(element.value)) {
  55. context.report({
  56. node,
  57. messageId: "unexpectedCallCuWebIdl",
  58. data: { name: element.value },
  59. });
  60. }
  61. }
  62. } else {
  63. context.report({ node, messageId: "unexpectedCall" });
  64. }
  65. }
  66. if (
  67. memexp.object.type === "Identifier" &&
  68. memexp.object.name === "XPCOMUtils" &&
  69. memexp.property.type === "Identifier" &&
  70. memexp.property.name === "defineLazyGlobalGetters" &&
  71. node.arguments.length >= 2
  72. ) {
  73. if (context.options.includes("allownonwebidl")) {
  74. for (let element of node.arguments[1].elements) {
  75. if (privilegedGlobals.includes(element.value)) {
  76. context.report({
  77. node,
  78. messageId: "unexpectedCallXPCOMWebIdl",
  79. data: { name: element.value },
  80. });
  81. }
  82. }
  83. } else {
  84. context.report({ node, messageId: "unexpectedCall" });
  85. }
  86. }
  87. },
  88. };
  89. },
  90. };