123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- "use strict";
- const astUtils = require("./utils/ast-utils");
- function isGlobal(variable) {
- return Boolean(variable.scope) && variable.scope.type === "global";
- }
- function getEnclosingFunctionScope(scope) {
- let currentScope = scope;
- while (currentScope.type !== "function" && currentScope.type !== "global") {
- currentScope = currentScope.upper;
- }
- return currentScope;
- }
- function isReferencedInClosure(variable) {
- const enclosingFunctionScope = getEnclosingFunctionScope(variable.scope);
- return variable.references.some(reference =>
- getEnclosingFunctionScope(reference.from) !== enclosingFunctionScope);
- }
- function isLoopAssignee(node) {
- return (node.parent.type === "ForOfStatement" || node.parent.type === "ForInStatement") &&
- node === node.parent.left;
- }
- function isDeclarationInitialized(node) {
- return node.declarations.every(declarator => declarator.init !== null);
- }
- const SCOPE_NODE_TYPE = /^(?:Program|BlockStatement|SwitchStatement|ForStatement|ForInStatement|ForOfStatement)$/u;
- function getScopeNode(node) {
- for (let currentNode = node; currentNode; currentNode = currentNode.parent) {
- if (SCOPE_NODE_TYPE.test(currentNode.type)) {
- return currentNode;
- }
- }
-
- return null;
- }
- function isRedeclared(variable) {
- return variable.defs.length >= 2;
- }
- function isUsedFromOutsideOf(scopeNode) {
-
- function isOutsideOfScope(reference) {
- const scope = scopeNode.range;
- const id = reference.identifier.range;
- return id[0] < scope[0] || id[1] > scope[1];
- }
- return function(variable) {
- return variable.references.some(isOutsideOfScope);
- };
- }
- function hasReferenceInTDZ(node) {
- const initStart = node.range[0];
- const initEnd = node.range[1];
- return variable => {
- const id = variable.defs[0].name;
- const idStart = id.range[0];
- const defaultValue = (id.parent.type === "AssignmentPattern" ? id.parent.right : null);
- const defaultStart = defaultValue && defaultValue.range[0];
- const defaultEnd = defaultValue && defaultValue.range[1];
- return variable.references.some(reference => {
- const start = reference.identifier.range[0];
- const end = reference.identifier.range[1];
- return !reference.init && (
- start < idStart ||
- (defaultValue !== null && start >= defaultStart && end <= defaultEnd) ||
- (start >= initStart && end <= initEnd)
- );
- });
- };
- }
- function hasNameDisallowedForLetDeclarations(variable) {
- return variable.name === "let";
- }
- module.exports = {
- meta: {
- type: "suggestion",
- docs: {
- description: "require `let` or `const` instead of `var`",
- category: "ECMAScript 6",
- recommended: false,
- url: "https://eslint.org/docs/rules/no-var"
- },
- schema: [],
- fixable: "code"
- },
- create(context) {
- const sourceCode = context.getSourceCode();
-
- function hasSelfReferenceInTDZ(declarator) {
- if (!declarator.init) {
- return false;
- }
- const variables = context.getDeclaredVariables(declarator);
- return variables.some(hasReferenceInTDZ(declarator.init));
- }
-
- function canFix(node) {
- const variables = context.getDeclaredVariables(node);
- const scopeNode = getScopeNode(node);
- if (node.parent.type === "SwitchCase" ||
- node.declarations.some(hasSelfReferenceInTDZ) ||
- variables.some(isGlobal) ||
- variables.some(isRedeclared) ||
- variables.some(isUsedFromOutsideOf(scopeNode)) ||
- variables.some(hasNameDisallowedForLetDeclarations)
- ) {
- return false;
- }
- if (astUtils.isInLoop(node)) {
- if (variables.some(isReferencedInClosure)) {
- return false;
- }
- if (!isLoopAssignee(node) && !isDeclarationInitialized(node)) {
- return false;
- }
- }
- if (
- !isLoopAssignee(node) &&
- !(node.parent.type === "ForStatement" && node.parent.init === node) &&
- !astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type)
- ) {
-
- return false;
- }
- return true;
- }
-
- function report(node) {
- context.report({
- node,
- message: "Unexpected var, use let or const instead.",
- fix(fixer) {
- const varToken = sourceCode.getFirstToken(node, { filter: t => t.value === "var" });
- return canFix(node)
- ? fixer.replaceText(varToken, "let")
- : null;
- }
- });
- }
- return {
- "VariableDeclaration:exit"(node) {
- if (node.kind === "var") {
- report(node);
- }
- }
- };
- }
- };
|