no-throw-cr-literal.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * @fileoverview Rule to prevent throwing bare Cr.ERRORs.
  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. function isCr(object) {
  10. return object.type === "Identifier" && object.name === "Cr";
  11. }
  12. function isComponentsResults(object) {
  13. return (
  14. object.type === "MemberExpression" &&
  15. object.object.type === "Identifier" &&
  16. object.object.name === "Components" &&
  17. object.property.type === "Identifier" &&
  18. object.property.name === "results"
  19. );
  20. }
  21. function isNewError(argument) {
  22. return (
  23. argument.type === "NewExpression" &&
  24. argument.callee.type === "Identifier" &&
  25. argument.callee.name === "Error" &&
  26. argument.arguments.length === 1
  27. );
  28. }
  29. function fixT(context, node, argument, fixer) {
  30. const sourceText = context.getSourceCode().getText(argument);
  31. return fixer.replaceText(node, `Components.Exception("", ${sourceText})`);
  32. }
  33. module.exports = {
  34. meta: {
  35. docs: {
  36. url:
  37. "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/no-throw-cr-literal.html",
  38. },
  39. fixable: "code",
  40. messages: {
  41. bareCR: "Do not throw bare Cr.ERRORs, use Components.Exception instead",
  42. bareComponentsResults:
  43. "Do not throw bare Components.results.ERRORs, use Components.Exception instead",
  44. newErrorCR:
  45. "Do not pass Cr.ERRORs to new Error(), use Components.Exception instead",
  46. newErrorComponentsResults:
  47. "Do not pass Components.results.ERRORs to new Error(), use Components.Exception instead",
  48. },
  49. type: "problem",
  50. },
  51. create(context) {
  52. return {
  53. ThrowStatement(node) {
  54. if (node.argument.type === "MemberExpression") {
  55. const fix = fixT.bind(null, context, node.argument, node.argument);
  56. if (isCr(node.argument.object)) {
  57. context.report({
  58. node,
  59. messageId: "bareCR",
  60. fix,
  61. });
  62. } else if (isComponentsResults(node.argument.object)) {
  63. context.report({
  64. node,
  65. messageId: "bareComponentsResults",
  66. fix,
  67. });
  68. }
  69. } else if (isNewError(node.argument)) {
  70. const argument = node.argument.arguments[0];
  71. if (argument.type === "MemberExpression") {
  72. const fix = fixT.bind(null, context, node.argument, argument);
  73. if (isCr(argument.object)) {
  74. context.report({
  75. node,
  76. messageId: "newErrorCR",
  77. fix,
  78. });
  79. } else if (isComponentsResults(argument.object)) {
  80. context.report({
  81. node,
  82. messageId: "newErrorComponentsResults",
  83. fix,
  84. });
  85. }
  86. }
  87. }
  88. },
  89. };
  90. },
  91. };