no-var.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /**
  2. * @fileoverview Rule to check for the usage of var.
  3. * @author Jamund Ferguson
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("./utils/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Helpers
  12. //------------------------------------------------------------------------------
  13. /**
  14. * Check whether a given variable is a global variable or not.
  15. * @param {eslint-scope.Variable} variable The variable to check.
  16. * @returns {boolean} `true` if the variable is a global variable.
  17. */
  18. function isGlobal(variable) {
  19. return Boolean(variable.scope) && variable.scope.type === "global";
  20. }
  21. /**
  22. * Finds the nearest function scope or global scope walking up the scope
  23. * hierarchy.
  24. * @param {eslint-scope.Scope} scope The scope to traverse.
  25. * @returns {eslint-scope.Scope} a function scope or global scope containing the given
  26. * scope.
  27. */
  28. function getEnclosingFunctionScope(scope) {
  29. let currentScope = scope;
  30. while (currentScope.type !== "function" && currentScope.type !== "global") {
  31. currentScope = currentScope.upper;
  32. }
  33. return currentScope;
  34. }
  35. /**
  36. * Checks whether the given variable has any references from a more specific
  37. * function expression (i.e. a closure).
  38. * @param {eslint-scope.Variable} variable A variable to check.
  39. * @returns {boolean} `true` if the variable is used from a closure.
  40. */
  41. function isReferencedInClosure(variable) {
  42. const enclosingFunctionScope = getEnclosingFunctionScope(variable.scope);
  43. return variable.references.some(reference =>
  44. getEnclosingFunctionScope(reference.from) !== enclosingFunctionScope);
  45. }
  46. /**
  47. * Checks whether the given node is the assignee of a loop.
  48. * @param {ASTNode} node A VariableDeclaration node to check.
  49. * @returns {boolean} `true` if the declaration is assigned as part of loop
  50. * iteration.
  51. */
  52. function isLoopAssignee(node) {
  53. return (node.parent.type === "ForOfStatement" || node.parent.type === "ForInStatement") &&
  54. node === node.parent.left;
  55. }
  56. /**
  57. * Checks whether the given variable declaration is immediately initialized.
  58. * @param {ASTNode} node A VariableDeclaration node to check.
  59. * @returns {boolean} `true` if the declaration has an initializer.
  60. */
  61. function isDeclarationInitialized(node) {
  62. return node.declarations.every(declarator => declarator.init !== null);
  63. }
  64. const SCOPE_NODE_TYPE = /^(?:Program|BlockStatement|SwitchStatement|ForStatement|ForInStatement|ForOfStatement)$/u;
  65. /**
  66. * Gets the scope node which directly contains a given node.
  67. * @param {ASTNode} node A node to get. This is a `VariableDeclaration` or
  68. * an `Identifier`.
  69. * @returns {ASTNode} A scope node. This is one of `Program`, `BlockStatement`,
  70. * `SwitchStatement`, `ForStatement`, `ForInStatement`, and
  71. * `ForOfStatement`.
  72. */
  73. function getScopeNode(node) {
  74. for (let currentNode = node; currentNode; currentNode = currentNode.parent) {
  75. if (SCOPE_NODE_TYPE.test(currentNode.type)) {
  76. return currentNode;
  77. }
  78. }
  79. /* c8 ignore next */
  80. return null;
  81. }
  82. /**
  83. * Checks whether a given variable is redeclared or not.
  84. * @param {eslint-scope.Variable} variable A variable to check.
  85. * @returns {boolean} `true` if the variable is redeclared.
  86. */
  87. function isRedeclared(variable) {
  88. return variable.defs.length >= 2;
  89. }
  90. /**
  91. * Checks whether a given variable is used from outside of the specified scope.
  92. * @param {ASTNode} scopeNode A scope node to check.
  93. * @returns {Function} The predicate function which checks whether a given
  94. * variable is used from outside of the specified scope.
  95. */
  96. function isUsedFromOutsideOf(scopeNode) {
  97. /**
  98. * Checks whether a given reference is inside of the specified scope or not.
  99. * @param {eslint-scope.Reference} reference A reference to check.
  100. * @returns {boolean} `true` if the reference is inside of the specified
  101. * scope.
  102. */
  103. function isOutsideOfScope(reference) {
  104. const scope = scopeNode.range;
  105. const id = reference.identifier.range;
  106. return id[0] < scope[0] || id[1] > scope[1];
  107. }
  108. return function(variable) {
  109. return variable.references.some(isOutsideOfScope);
  110. };
  111. }
  112. /**
  113. * Creates the predicate function which checks whether a variable has their references in TDZ.
  114. *
  115. * The predicate function would return `true`:
  116. *
  117. * - if a reference is before the declarator. E.g. (var a = b, b = 1;)(var {a = b, b} = {};)
  118. * - if a reference is in the expression of their default value. E.g. (var {a = a} = {};)
  119. * - if a reference is in the expression of their initializer. E.g. (var a = a;)
  120. * @param {ASTNode} node The initializer node of VariableDeclarator.
  121. * @returns {Function} The predicate function.
  122. * @private
  123. */
  124. function hasReferenceInTDZ(node) {
  125. const initStart = node.range[0];
  126. const initEnd = node.range[1];
  127. return variable => {
  128. const id = variable.defs[0].name;
  129. const idStart = id.range[0];
  130. const defaultValue = (id.parent.type === "AssignmentPattern" ? id.parent.right : null);
  131. const defaultStart = defaultValue && defaultValue.range[0];
  132. const defaultEnd = defaultValue && defaultValue.range[1];
  133. return variable.references.some(reference => {
  134. const start = reference.identifier.range[0];
  135. const end = reference.identifier.range[1];
  136. return !reference.init && (
  137. start < idStart ||
  138. (defaultValue !== null && start >= defaultStart && end <= defaultEnd) ||
  139. (!astUtils.isFunction(node) && start >= initStart && end <= initEnd)
  140. );
  141. });
  142. };
  143. }
  144. /**
  145. * Checks whether a given variable has name that is allowed for 'var' declarations,
  146. * but disallowed for `let` declarations.
  147. * @param {eslint-scope.Variable} variable The variable to check.
  148. * @returns {boolean} `true` if the variable has a disallowed name.
  149. */
  150. function hasNameDisallowedForLetDeclarations(variable) {
  151. return variable.name === "let";
  152. }
  153. //------------------------------------------------------------------------------
  154. // Rule Definition
  155. //------------------------------------------------------------------------------
  156. /** @type {import('../shared/types').Rule} */
  157. module.exports = {
  158. meta: {
  159. type: "suggestion",
  160. docs: {
  161. description: "Require `let` or `const` instead of `var`",
  162. recommended: false,
  163. url: "https://eslint.org/docs/rules/no-var"
  164. },
  165. schema: [],
  166. fixable: "code",
  167. messages: {
  168. unexpectedVar: "Unexpected var, use let or const instead."
  169. }
  170. },
  171. create(context) {
  172. const sourceCode = context.getSourceCode();
  173. /**
  174. * Checks whether the variables which are defined by the given declarator node have their references in TDZ.
  175. * @param {ASTNode} declarator The VariableDeclarator node to check.
  176. * @returns {boolean} `true` if one of the variables which are defined by the given declarator node have their references in TDZ.
  177. */
  178. function hasSelfReferenceInTDZ(declarator) {
  179. if (!declarator.init) {
  180. return false;
  181. }
  182. const variables = context.getDeclaredVariables(declarator);
  183. return variables.some(hasReferenceInTDZ(declarator.init));
  184. }
  185. /**
  186. * Checks whether it can fix a given variable declaration or not.
  187. * It cannot fix if the following cases:
  188. *
  189. * - A variable is a global variable.
  190. * - A variable is declared on a SwitchCase node.
  191. * - A variable is redeclared.
  192. * - A variable is used from outside the scope.
  193. * - A variable is used from a closure within a loop.
  194. * - A variable might be used before it is assigned within a loop.
  195. * - A variable might be used in TDZ.
  196. * - A variable is declared in statement position (e.g. a single-line `IfStatement`)
  197. * - A variable has name that is disallowed for `let` declarations.
  198. *
  199. * ## A variable is declared on a SwitchCase node.
  200. *
  201. * If this rule modifies 'var' declarations on a SwitchCase node, it
  202. * would generate the warnings of 'no-case-declarations' rule. And the
  203. * 'eslint:recommended' preset includes 'no-case-declarations' rule, so
  204. * this rule doesn't modify those declarations.
  205. *
  206. * ## A variable is redeclared.
  207. *
  208. * The language spec disallows redeclarations of `let` declarations.
  209. * Those variables would cause syntax errors.
  210. *
  211. * ## A variable is used from outside the scope.
  212. *
  213. * The language spec disallows accesses from outside of the scope for
  214. * `let` declarations. Those variables would cause reference errors.
  215. *
  216. * ## A variable is used from a closure within a loop.
  217. *
  218. * A `var` declaration within a loop shares the same variable instance
  219. * across all loop iterations, while a `let` declaration creates a new
  220. * instance for each iteration. This means if a variable in a loop is
  221. * referenced by any closure, changing it from `var` to `let` would
  222. * change the behavior in a way that is generally unsafe.
  223. *
  224. * ## A variable might be used before it is assigned within a loop.
  225. *
  226. * Within a loop, a `let` declaration without an initializer will be
  227. * initialized to null, while a `var` declaration will retain its value
  228. * from the previous iteration, so it is only safe to change `var` to
  229. * `let` if we can statically determine that the variable is always
  230. * assigned a value before its first access in the loop body. To keep
  231. * the implementation simple, we only convert `var` to `let` within
  232. * loops when the variable is a loop assignee or the declaration has an
  233. * initializer.
  234. * @param {ASTNode} node A variable declaration node to check.
  235. * @returns {boolean} `true` if it can fix the node.
  236. */
  237. function canFix(node) {
  238. const variables = context.getDeclaredVariables(node);
  239. const scopeNode = getScopeNode(node);
  240. if (node.parent.type === "SwitchCase" ||
  241. node.declarations.some(hasSelfReferenceInTDZ) ||
  242. variables.some(isGlobal) ||
  243. variables.some(isRedeclared) ||
  244. variables.some(isUsedFromOutsideOf(scopeNode)) ||
  245. variables.some(hasNameDisallowedForLetDeclarations)
  246. ) {
  247. return false;
  248. }
  249. if (astUtils.isInLoop(node)) {
  250. if (variables.some(isReferencedInClosure)) {
  251. return false;
  252. }
  253. if (!isLoopAssignee(node) && !isDeclarationInitialized(node)) {
  254. return false;
  255. }
  256. }
  257. if (
  258. !isLoopAssignee(node) &&
  259. !(node.parent.type === "ForStatement" && node.parent.init === node) &&
  260. !astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type)
  261. ) {
  262. // If the declaration is not in a block, e.g. `if (foo) var bar = 1;`, then it can't be fixed.
  263. return false;
  264. }
  265. return true;
  266. }
  267. /**
  268. * Reports a given variable declaration node.
  269. * @param {ASTNode} node A variable declaration node to report.
  270. * @returns {void}
  271. */
  272. function report(node) {
  273. context.report({
  274. node,
  275. messageId: "unexpectedVar",
  276. fix(fixer) {
  277. const varToken = sourceCode.getFirstToken(node, { filter: t => t.value === "var" });
  278. return canFix(node)
  279. ? fixer.replaceText(varToken, "let")
  280. : null;
  281. }
  282. });
  283. }
  284. return {
  285. "VariableDeclaration:exit"(node) {
  286. if (node.kind === "var") {
  287. report(node);
  288. }
  289. }
  290. };
  291. }
  292. };