项目原始demo,不改动
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
Den här utvecklingskatalogen är arkiverad. Du kan se filer och klona katalogen, men inte öppna ärenden eller genomföra push- eller pull-förfrågningar.
 
 
 
 

150 rader
4.8 KiB

  1. /**
  2. * @fileoverview Rule to enforce consistent naming of "this" context variables
  3. * @author Raphael Pigulla
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. docs: {
  12. description: "enforce consistent naming when capturing the current execution context",
  13. category: "Stylistic Issues",
  14. recommended: false,
  15. url: "https://eslint.org/docs/rules/consistent-this"
  16. },
  17. schema: {
  18. type: "array",
  19. items: {
  20. type: "string",
  21. minLength: 1
  22. },
  23. uniqueItems: true
  24. },
  25. messages: {
  26. aliasNotAssignedToThis: "Designated alias '{{name}}' is not assigned to 'this'.",
  27. unexpectedAlias: "Unexpected alias '{{name}}' for 'this'."
  28. }
  29. },
  30. create(context) {
  31. let aliases = [];
  32. if (context.options.length === 0) {
  33. aliases.push("that");
  34. } else {
  35. aliases = context.options;
  36. }
  37. /**
  38. * Reports that a variable declarator or assignment expression is assigning
  39. * a non-'this' value to the specified alias.
  40. * @param {ASTNode} node - The assigning node.
  41. * @param {string} name - the name of the alias that was incorrectly used.
  42. * @returns {void}
  43. */
  44. function reportBadAssignment(node, name) {
  45. context.report({ node, messageId: "aliasNotAssignedToThis", data: { name } });
  46. }
  47. /**
  48. * Checks that an assignment to an identifier only assigns 'this' to the
  49. * appropriate alias, and the alias is only assigned to 'this'.
  50. * @param {ASTNode} node - The assigning node.
  51. * @param {Identifier} name - The name of the variable assigned to.
  52. * @param {Expression} value - The value of the assignment.
  53. * @returns {void}
  54. */
  55. function checkAssignment(node, name, value) {
  56. const isThis = value.type === "ThisExpression";
  57. if (aliases.indexOf(name) !== -1) {
  58. if (!isThis || node.operator && node.operator !== "=") {
  59. reportBadAssignment(node, name);
  60. }
  61. } else if (isThis) {
  62. context.report({ node, messageId: "unexpectedAlias", data: { name } });
  63. }
  64. }
  65. /**
  66. * Ensures that a variable declaration of the alias in a program or function
  67. * is assigned to the correct value.
  68. * @param {string} alias alias the check the assignment of.
  69. * @param {Object} scope scope of the current code we are checking.
  70. * @private
  71. * @returns {void}
  72. */
  73. function checkWasAssigned(alias, scope) {
  74. const variable = scope.set.get(alias);
  75. if (!variable) {
  76. return;
  77. }
  78. if (variable.defs.some(def => def.node.type === "VariableDeclarator" &&
  79. def.node.init !== null)) {
  80. return;
  81. }
  82. /*
  83. * The alias has been declared and not assigned: check it was
  84. * assigned later in the same scope.
  85. */
  86. if (!variable.references.some(reference => {
  87. const write = reference.writeExpr;
  88. return (
  89. reference.from === scope &&
  90. write && write.type === "ThisExpression" &&
  91. write.parent.operator === "="
  92. );
  93. })) {
  94. variable.defs.map(def => def.node).forEach(node => {
  95. reportBadAssignment(node, alias);
  96. });
  97. }
  98. }
  99. /**
  100. * Check each alias to ensure that is was assinged to the correct value.
  101. * @returns {void}
  102. */
  103. function ensureWasAssigned() {
  104. const scope = context.getScope();
  105. aliases.forEach(alias => {
  106. checkWasAssigned(alias, scope);
  107. });
  108. }
  109. return {
  110. "Program:exit": ensureWasAssigned,
  111. "FunctionExpression:exit": ensureWasAssigned,
  112. "FunctionDeclaration:exit": ensureWasAssigned,
  113. VariableDeclarator(node) {
  114. const id = node.id;
  115. const isDestructuring =
  116. id.type === "ArrayPattern" || id.type === "ObjectPattern";
  117. if (node.init !== null && !isDestructuring) {
  118. checkAssignment(node, id.name, node.init);
  119. }
  120. },
  121. AssignmentExpression(node) {
  122. if (node.left.type === "Identifier") {
  123. checkAssignment(node, node.left.name, node.right);
  124. }
  125. }
  126. };
  127. }
  128. };