use-services.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @fileoverview Require use of Services.* rather than getService.
  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 helpers = require("../helpers");
  10. let servicesInterfaceMap = helpers.servicesData;
  11. module.exports = {
  12. meta: {
  13. docs: {
  14. url:
  15. "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/use-services.html",
  16. },
  17. // fixable: "code",
  18. type: "suggestion",
  19. },
  20. create(context) {
  21. return {
  22. CallExpression(node) {
  23. if (!node.callee || !node.callee.property) {
  24. return;
  25. }
  26. if (
  27. node.callee.property.type == "Identifier" &&
  28. node.callee.property.name == "defineLazyServiceGetter" &&
  29. node.arguments.length == 4 &&
  30. node.arguments[3].type == "Literal" &&
  31. node.arguments[3].value in servicesInterfaceMap
  32. ) {
  33. let serviceName = servicesInterfaceMap[node.arguments[3].value];
  34. context.report(
  35. node,
  36. `Use Services.${serviceName} rather than defineLazyServiceGetter.`
  37. );
  38. return;
  39. }
  40. if (
  41. node.callee.property.type == "Identifier" &&
  42. node.callee.property.name == "defineLazyServiceGetters" &&
  43. node.arguments.length == 2 &&
  44. node.arguments[1].type == "ObjectExpression"
  45. ) {
  46. for (let property of node.arguments[1].properties) {
  47. if (
  48. property.value.type == "ArrayExpression" &&
  49. property.value.elements.length == 2 &&
  50. property.value.elements[1].value in servicesInterfaceMap
  51. ) {
  52. let serviceName =
  53. servicesInterfaceMap[property.value.elements[1].value];
  54. context.report(
  55. property.value,
  56. `Use Services.${serviceName} rather than defineLazyServiceGetters.`
  57. );
  58. }
  59. }
  60. return;
  61. }
  62. if (
  63. node.callee.property.type != "Identifier" ||
  64. node.callee.property.name != "getService" ||
  65. node.arguments.length != 1 ||
  66. !node.arguments[0].property ||
  67. node.arguments[0].property.type != "Identifier" ||
  68. !node.arguments[0].property.name ||
  69. !(node.arguments[0].property.name in servicesInterfaceMap)
  70. ) {
  71. return;
  72. }
  73. let serviceName = servicesInterfaceMap[node.arguments[0].property.name];
  74. context.report({
  75. node,
  76. message: `Use Services.${serviceName} rather than getService().`,
  77. // This is not enabled by default as for mochitest plain tests we
  78. // would need to replace with `SpecialPowers.Services.${serviceName}`.
  79. // At the moment we do not have an easy way to detect that.
  80. // fix(fixer) {
  81. // let sourceCode = context.getSourceCode();
  82. // return fixer.replaceTextRange(
  83. // [
  84. // sourceCode.getFirstToken(node.callee).range[0],
  85. // sourceCode.getLastToken(node).range[1],
  86. // ],
  87. // `Services.${serviceName}`
  88. // );
  89. // },
  90. });
  91. },
  92. };
  93. },
  94. };