use-cc-etc.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * @fileoverview Reject use of Components.classes etc, prefer the shorthand instead.
  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 componentsMap = {
  10. classes: "Cc",
  11. interfaces: "Ci",
  12. results: "Cr",
  13. utils: "Cu",
  14. };
  15. module.exports = {
  16. meta: {
  17. docs: {
  18. url:
  19. "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/use-cc-etc.html",
  20. },
  21. type: "suggestion",
  22. fixable: "code",
  23. },
  24. create(context) {
  25. return {
  26. MemberExpression(node) {
  27. if (
  28. node.object.type === "Identifier" &&
  29. node.object.name === "Components" &&
  30. node.property.type === "Identifier" &&
  31. Object.getOwnPropertyNames(componentsMap).includes(node.property.name)
  32. ) {
  33. context.report({
  34. node,
  35. message: `Use ${
  36. componentsMap[node.property.name]
  37. } rather than Components.${node.property.name}`,
  38. fix: fixer =>
  39. fixer.replaceTextRange(
  40. [node.range[0], node.range[1]],
  41. componentsMap[node.property.name]
  42. ),
  43. });
  44. }
  45. },
  46. };
  47. },
  48. };