项目原始demo,不改动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Deze repo is gearchiveerd. U kunt bestanden bekijken en het klonen, maar niet pushen of problemen/pull-requests openen.
 
 
 
 

123 regels
3.9 KiB

  1. /**
  2. * @fileoverview Rule that warns when identifier names that are
  3. * blacklisted in the configuration are used.
  4. * @author Keith Cirkel (http://keithcirkel.co.uk)
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. docs: {
  13. description: "disallow specified identifiers",
  14. category: "Stylistic Issues",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/id-blacklist"
  17. },
  18. schema: {
  19. type: "array",
  20. items: {
  21. type: "string"
  22. },
  23. uniqueItems: true
  24. }
  25. },
  26. create(context) {
  27. //--------------------------------------------------------------------------
  28. // Helpers
  29. //--------------------------------------------------------------------------
  30. const blacklist = context.options;
  31. /**
  32. * Checks if a string matches the provided pattern
  33. * @param {string} name The string to check.
  34. * @returns {boolean} if the string is a match
  35. * @private
  36. */
  37. function isInvalid(name) {
  38. return blacklist.indexOf(name) !== -1;
  39. }
  40. /**
  41. * Verifies if we should report an error or not based on the effective
  42. * parent node and the identifier name.
  43. * @param {ASTNode} effectiveParent The effective parent node of the node to be reported
  44. * @param {string} name The identifier name of the identifier node
  45. * @returns {boolean} whether an error should be reported or not
  46. */
  47. function shouldReport(effectiveParent, name) {
  48. return effectiveParent.type !== "CallExpression" &&
  49. effectiveParent.type !== "NewExpression" &&
  50. isInvalid(name);
  51. }
  52. /**
  53. * Reports an AST node as a rule violation.
  54. * @param {ASTNode} node The node to report.
  55. * @returns {void}
  56. * @private
  57. */
  58. function report(node) {
  59. context.report({
  60. node,
  61. message: "Identifier '{{name}}' is blacklisted.",
  62. data: {
  63. name: node.name
  64. }
  65. });
  66. }
  67. return {
  68. Identifier(node) {
  69. const name = node.name,
  70. effectiveParent = (node.parent.type === "MemberExpression") ? node.parent.parent : node.parent;
  71. // MemberExpressions get special rules
  72. if (node.parent.type === "MemberExpression") {
  73. // Always check object names
  74. if (node.parent.object.type === "Identifier" &&
  75. node.parent.object.name === node.name) {
  76. if (isInvalid(name)) {
  77. report(node);
  78. }
  79. // Report AssignmentExpressions only if they are the left side of the assignment
  80. } else if (effectiveParent.type === "AssignmentExpression" &&
  81. (effectiveParent.right.type !== "MemberExpression" ||
  82. effectiveParent.left.type === "MemberExpression" &&
  83. effectiveParent.left.property.name === node.name)) {
  84. if (isInvalid(name)) {
  85. report(node);
  86. }
  87. }
  88. // Properties have their own rules
  89. } else if (node.parent.type === "Property") {
  90. if (shouldReport(effectiveParent, name)) {
  91. report(node);
  92. }
  93. // Report anything that is a match and not a CallExpression
  94. } else if (shouldReport(effectiveParent, name)) {
  95. report(node);
  96. }
  97. }
  98. };
  99. }
  100. };