reject-globalThis-modification.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * @fileoverview Enforce the standard object name for
  3. * ChromeUtils.defineESMGetters
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. */
  9. "use strict";
  10. function isIdentifier(node, id) {
  11. return node.type === "Identifier" && node.name === id;
  12. }
  13. function calleeToString(node) {
  14. if (node.type === "Identifier") {
  15. return node.name;
  16. }
  17. if (node.type === "MemberExpression" && !node.computed) {
  18. return calleeToString(node.object) + "." + node.property.name;
  19. }
  20. return "???";
  21. }
  22. module.exports = {
  23. meta: {
  24. docs: {
  25. url:
  26. "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/reject-globalThis-modification.html",
  27. },
  28. type: "problem",
  29. },
  30. create(context) {
  31. return {
  32. AssignmentExpression(node, parents) {
  33. let target = node.left;
  34. while (target.type === "MemberExpression") {
  35. target = target.object;
  36. }
  37. if (isIdentifier(target, "globalThis")) {
  38. context.report({
  39. node,
  40. message:
  41. "`globalThis` shouldn't be modified. `globalThis` is the shared global inside the system module, and properties defined on it is visible from all modules.",
  42. });
  43. }
  44. },
  45. CallExpression(node) {
  46. const calleeStr = calleeToString(node.callee);
  47. if (calleeStr.endsWith(".deserialize")) {
  48. return;
  49. }
  50. for (const arg of node.arguments) {
  51. if (isIdentifier(arg, "globalThis")) {
  52. context.report({
  53. node,
  54. message:
  55. "`globalThis` shouldn't be passed to function that can modify it. `globalThis` is the shared global inside the system module, and properties defined on it is visible from all modules.",
  56. });
  57. }
  58. }
  59. },
  60. };
  61. },
  62. };