reject-chromeutils-import-params.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * @fileoverview Reject calls to ChromeUtils.import(..., null). This allows to
  3. * retrieve the global object for the JSM, instead we should rely on explicitly
  4. * exported symbols.
  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. function isIdentifier(node, id) {
  12. return node && node.type === "Identifier" && node.name === id;
  13. }
  14. function getRangeAfterArgToEnd(context, argNumber, args) {
  15. let sourceCode = context.getSourceCode();
  16. return [
  17. sourceCode.getTokenAfter(args[argNumber]).range[0],
  18. args[args.length - 1].range[1],
  19. ];
  20. }
  21. module.exports = {
  22. meta: {
  23. docs: {
  24. url:
  25. "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/reject-chromeutils-import-params.html",
  26. },
  27. hasSuggestions: true,
  28. type: "problem",
  29. },
  30. create(context) {
  31. return {
  32. CallExpression(node) {
  33. let { callee } = node;
  34. if (
  35. isIdentifier(callee.object, "ChromeUtils") &&
  36. isIdentifier(callee.property, "import") &&
  37. node.arguments.length >= 2
  38. ) {
  39. context.report({
  40. node,
  41. message: "ChromeUtils.import only takes one argument.",
  42. suggest: [
  43. {
  44. desc: "Remove the unnecessary parameters.",
  45. fix: fixer => {
  46. return fixer.removeRange(
  47. getRangeAfterArgToEnd(context, 0, node.arguments)
  48. );
  49. },
  50. },
  51. ],
  52. });
  53. }
  54. },
  55. };
  56. },
  57. };