项目原始demo,不改动
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
Dieses Repo ist archiviert. Du kannst Dateien sehen und es klonen, kannst aber nicht pushen oder Issues/Pull-Requests öffnen.
 
 
 
 

190 Zeilen
6.8 KiB

  1. /**
  2. * @fileoverview Rule to flag on declaring variables already declared in the outer scope
  3. * @author Ilya Volodin
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. docs: {
  16. description: "disallow variable declarations from shadowing variables declared in the outer scope",
  17. category: "Variables",
  18. recommended: false,
  19. url: "https://eslint.org/docs/rules/no-shadow"
  20. },
  21. schema: [
  22. {
  23. type: "object",
  24. properties: {
  25. builtinGlobals: { type: "boolean" },
  26. hoist: { enum: ["all", "functions", "never"] },
  27. allow: {
  28. type: "array",
  29. items: {
  30. type: "string"
  31. }
  32. }
  33. },
  34. additionalProperties: false
  35. }
  36. ]
  37. },
  38. create(context) {
  39. const options = {
  40. builtinGlobals: Boolean(context.options[0] && context.options[0].builtinGlobals),
  41. hoist: (context.options[0] && context.options[0].hoist) || "functions",
  42. allow: (context.options[0] && context.options[0].allow) || []
  43. };
  44. /**
  45. * Check if variable name is allowed.
  46. *
  47. * @param {ASTNode} variable The variable to check.
  48. * @returns {boolean} Whether or not the variable name is allowed.
  49. */
  50. function isAllowed(variable) {
  51. return options.allow.indexOf(variable.name) !== -1;
  52. }
  53. /**
  54. * Checks if a variable of the class name in the class scope of ClassDeclaration.
  55. *
  56. * ClassDeclaration creates two variables of its name into its outer scope and its class scope.
  57. * So we should ignore the variable in the class scope.
  58. *
  59. * @param {Object} variable The variable to check.
  60. * @returns {boolean} Whether or not the variable of the class name in the class scope of ClassDeclaration.
  61. */
  62. function isDuplicatedClassNameVariable(variable) {
  63. const block = variable.scope.block;
  64. return block.type === "ClassDeclaration" && block.id === variable.identifiers[0];
  65. }
  66. /**
  67. * Checks if a variable is inside the initializer of scopeVar.
  68. *
  69. * To avoid reporting at declarations such as `var a = function a() {};`.
  70. * But it should report `var a = function(a) {};` or `var a = function() { function a() {} };`.
  71. *
  72. * @param {Object} variable The variable to check.
  73. * @param {Object} scopeVar The scope variable to look for.
  74. * @returns {boolean} Whether or not the variable is inside initializer of scopeVar.
  75. */
  76. function isOnInitializer(variable, scopeVar) {
  77. const outerScope = scopeVar.scope;
  78. const outerDef = scopeVar.defs[0];
  79. const outer = outerDef && outerDef.parent && outerDef.parent.range;
  80. const innerScope = variable.scope;
  81. const innerDef = variable.defs[0];
  82. const inner = innerDef && innerDef.name.range;
  83. return (
  84. outer &&
  85. inner &&
  86. outer[0] < inner[0] &&
  87. inner[1] < outer[1] &&
  88. ((innerDef.type === "FunctionName" && innerDef.node.type === "FunctionExpression") || innerDef.node.type === "ClassExpression") &&
  89. outerScope === innerScope.upper
  90. );
  91. }
  92. /**
  93. * Get a range of a variable's identifier node.
  94. * @param {Object} variable The variable to get.
  95. * @returns {Array|undefined} The range of the variable's identifier node.
  96. */
  97. function getNameRange(variable) {
  98. const def = variable.defs[0];
  99. return def && def.name.range;
  100. }
  101. /**
  102. * Checks if a variable is in TDZ of scopeVar.
  103. * @param {Object} variable The variable to check.
  104. * @param {Object} scopeVar The variable of TDZ.
  105. * @returns {boolean} Whether or not the variable is in TDZ of scopeVar.
  106. */
  107. function isInTdz(variable, scopeVar) {
  108. const outerDef = scopeVar.defs[0];
  109. const inner = getNameRange(variable);
  110. const outer = getNameRange(scopeVar);
  111. return (
  112. inner &&
  113. outer &&
  114. inner[1] < outer[0] &&
  115. // Excepts FunctionDeclaration if is {"hoist":"function"}.
  116. (options.hoist !== "functions" || !outerDef || outerDef.node.type !== "FunctionDeclaration")
  117. );
  118. }
  119. /**
  120. * Checks the current context for shadowed variables.
  121. * @param {Scope} scope - Fixme
  122. * @returns {void}
  123. */
  124. function checkForShadows(scope) {
  125. const variables = scope.variables;
  126. for (let i = 0; i < variables.length; ++i) {
  127. const variable = variables[i];
  128. // Skips "arguments" or variables of a class name in the class scope of ClassDeclaration.
  129. if (variable.identifiers.length === 0 ||
  130. isDuplicatedClassNameVariable(variable) ||
  131. isAllowed(variable)
  132. ) {
  133. continue;
  134. }
  135. // Gets shadowed variable.
  136. const shadowed = astUtils.getVariableByName(scope.upper, variable.name);
  137. if (shadowed &&
  138. (shadowed.identifiers.length > 0 || (options.builtinGlobals && "writeable" in shadowed)) &&
  139. !isOnInitializer(variable, shadowed) &&
  140. !(options.hoist !== "all" && isInTdz(variable, shadowed))
  141. ) {
  142. context.report({
  143. node: variable.identifiers[0],
  144. message: "'{{name}}' is already declared in the upper scope.",
  145. data: variable
  146. });
  147. }
  148. }
  149. }
  150. return {
  151. "Program:exit"() {
  152. const globalScope = context.getScope();
  153. const stack = globalScope.childScopes.slice();
  154. while (stack.length) {
  155. const scope = stack.pop();
  156. stack.push.apply(stack, scope.childScopes);
  157. checkForShadows(scope);
  158. }
  159. }
  160. };
  161. }
  162. };