[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Rule to check for "block scoped" variables by binding context
|
---|
| 3 | * @author Matt DuVall <http://www.mattduvall.com>
|
---|
| 4 | */
|
---|
| 5 | "use strict";
|
---|
| 6 |
|
---|
| 7 | //------------------------------------------------------------------------------
|
---|
| 8 | // Rule Definition
|
---|
| 9 | //------------------------------------------------------------------------------
|
---|
| 10 |
|
---|
| 11 | /** @type {import('../shared/types').Rule} */
|
---|
| 12 | module.exports = {
|
---|
| 13 | meta: {
|
---|
| 14 | type: "suggestion",
|
---|
| 15 |
|
---|
| 16 | docs: {
|
---|
| 17 | description: "Enforce the use of variables within the scope they are defined",
|
---|
| 18 | recommended: false,
|
---|
| 19 | url: "https://eslint.org/docs/latest/rules/block-scoped-var"
|
---|
| 20 | },
|
---|
| 21 |
|
---|
| 22 | schema: [],
|
---|
| 23 |
|
---|
| 24 | messages: {
|
---|
| 25 | outOfScope: "'{{name}}' declared on line {{definitionLine}} column {{definitionColumn}} is used outside of binding context."
|
---|
| 26 | }
|
---|
| 27 | },
|
---|
| 28 |
|
---|
| 29 | create(context) {
|
---|
| 30 | let stack = [];
|
---|
| 31 | const sourceCode = context.sourceCode;
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * Makes a block scope.
|
---|
| 35 | * @param {ASTNode} node A node of a scope.
|
---|
| 36 | * @returns {void}
|
---|
| 37 | */
|
---|
| 38 | function enterScope(node) {
|
---|
| 39 | stack.push(node.range);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | /**
|
---|
| 43 | * Pops the last block scope.
|
---|
| 44 | * @returns {void}
|
---|
| 45 | */
|
---|
| 46 | function exitScope() {
|
---|
| 47 | stack.pop();
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * Reports a given reference.
|
---|
| 52 | * @param {eslint-scope.Reference} reference A reference to report.
|
---|
| 53 | * @param {eslint-scope.Definition} definition A definition for which to report reference.
|
---|
| 54 | * @returns {void}
|
---|
| 55 | */
|
---|
| 56 | function report(reference, definition) {
|
---|
| 57 | const identifier = reference.identifier;
|
---|
| 58 | const definitionPosition = definition.name.loc.start;
|
---|
| 59 |
|
---|
| 60 | context.report({
|
---|
| 61 | node: identifier,
|
---|
| 62 | messageId: "outOfScope",
|
---|
| 63 | data: {
|
---|
| 64 | name: identifier.name,
|
---|
| 65 | definitionLine: definitionPosition.line,
|
---|
| 66 | definitionColumn: definitionPosition.column + 1
|
---|
| 67 | }
|
---|
| 68 | });
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | /**
|
---|
| 72 | * Finds and reports references which are outside of valid scopes.
|
---|
| 73 | * @param {ASTNode} node A node to get variables.
|
---|
| 74 | * @returns {void}
|
---|
| 75 | */
|
---|
| 76 | function checkForVariables(node) {
|
---|
| 77 | if (node.kind !== "var") {
|
---|
| 78 | return;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | // Defines a predicate to check whether or not a given reference is outside of valid scope.
|
---|
| 82 | const scopeRange = stack[stack.length - 1];
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
| 85 | * Check if a reference is out of scope
|
---|
| 86 | * @param {ASTNode} reference node to examine
|
---|
| 87 | * @returns {boolean} True is its outside the scope
|
---|
| 88 | * @private
|
---|
| 89 | */
|
---|
| 90 | function isOutsideOfScope(reference) {
|
---|
| 91 | const idRange = reference.identifier.range;
|
---|
| 92 |
|
---|
| 93 | return idRange[0] < scopeRange[0] || idRange[1] > scopeRange[1];
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | // Gets declared variables, and checks its references.
|
---|
| 97 | const variables = sourceCode.getDeclaredVariables(node);
|
---|
| 98 |
|
---|
| 99 | for (let i = 0; i < variables.length; ++i) {
|
---|
| 100 |
|
---|
| 101 | // Reports.
|
---|
| 102 | variables[i]
|
---|
| 103 | .references
|
---|
| 104 | .filter(isOutsideOfScope)
|
---|
| 105 | .forEach(ref => report(ref, variables[i].defs.find(def => def.parent === node)));
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | return {
|
---|
| 110 | Program(node) {
|
---|
| 111 | stack = [node.range];
|
---|
| 112 | },
|
---|
| 113 |
|
---|
| 114 | // Manages scopes.
|
---|
| 115 | BlockStatement: enterScope,
|
---|
| 116 | "BlockStatement:exit": exitScope,
|
---|
| 117 | ForStatement: enterScope,
|
---|
| 118 | "ForStatement:exit": exitScope,
|
---|
| 119 | ForInStatement: enterScope,
|
---|
| 120 | "ForInStatement:exit": exitScope,
|
---|
| 121 | ForOfStatement: enterScope,
|
---|
| 122 | "ForOfStatement:exit": exitScope,
|
---|
| 123 | SwitchStatement: enterScope,
|
---|
| 124 | "SwitchStatement:exit": exitScope,
|
---|
| 125 | CatchClause: enterScope,
|
---|
| 126 | "CatchClause:exit": exitScope,
|
---|
| 127 | StaticBlock: enterScope,
|
---|
| 128 | "StaticBlock:exit": exitScope,
|
---|
| 129 |
|
---|
| 130 | // Finds and reports references which are outside of valid scope.
|
---|
| 131 | VariableDeclaration: checkForVariables
|
---|
| 132 | };
|
---|
| 133 |
|
---|
| 134 | }
|
---|
| 135 | };
|
---|