]> git.proxmox.com Git - pve-eslint.git/blame - eslint/lib/rules/block-scoped-var.js
import 8.41.0 source
[pve-eslint.git] / eslint / lib / rules / block-scoped-var.js
CommitLineData
eb39fafa
DC
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
34eeec05 11/** @type {import('../shared/types').Rule} */
eb39fafa
DC
12module.exports = {
13 meta: {
14 type: "suggestion",
15
16 docs: {
8f9d1d4d 17 description: "Enforce the use of variables within the scope they are defined",
eb39fafa 18 recommended: false,
f2a92ac6 19 url: "https://eslint.org/docs/latest/rules/block-scoped-var"
eb39fafa
DC
20 },
21
22 schema: [],
23
24 messages: {
25 outOfScope: "'{{name}}' used outside of binding context."
26 }
27 },
28
29 create(context) {
30 let stack = [];
f2a92ac6 31 const sourceCode = context.sourceCode;
eb39fafa
DC
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 * @returns {void}
54 */
55 function report(reference) {
56 const identifier = reference.identifier;
57
58 context.report({ node: identifier, messageId: "outOfScope", data: { name: identifier.name } });
59 }
60
61 /**
62 * Finds and reports references which are outside of valid scopes.
63 * @param {ASTNode} node A node to get variables.
64 * @returns {void}
65 */
66 function checkForVariables(node) {
67 if (node.kind !== "var") {
68 return;
69 }
70
71 // Defines a predicate to check whether or not a given reference is outside of valid scope.
72 const scopeRange = stack[stack.length - 1];
73
74 /**
75 * Check if a reference is out of scope
76 * @param {ASTNode} reference node to examine
77 * @returns {boolean} True is its outside the scope
78 * @private
79 */
80 function isOutsideOfScope(reference) {
81 const idRange = reference.identifier.range;
82
83 return idRange[0] < scopeRange[0] || idRange[1] > scopeRange[1];
84 }
85
86 // Gets declared variables, and checks its references.
f2a92ac6 87 const variables = sourceCode.getDeclaredVariables(node);
eb39fafa
DC
88
89 for (let i = 0; i < variables.length; ++i) {
90
91 // Reports.
92 variables[i]
93 .references
94 .filter(isOutsideOfScope)
95 .forEach(report);
96 }
97 }
98
99 return {
100 Program(node) {
101 stack = [node.range];
102 },
103
104 // Manages scopes.
105 BlockStatement: enterScope,
106 "BlockStatement:exit": exitScope,
107 ForStatement: enterScope,
108 "ForStatement:exit": exitScope,
109 ForInStatement: enterScope,
110 "ForInStatement:exit": exitScope,
111 ForOfStatement: enterScope,
112 "ForOfStatement:exit": exitScope,
113 SwitchStatement: enterScope,
114 "SwitchStatement:exit": exitScope,
115 CatchClause: enterScope,
116 "CatchClause:exit": exitScope,
609c276f
TL
117 StaticBlock: enterScope,
118 "StaticBlock:exit": exitScope,
eb39fafa
DC
119
120 // Finds and reports references which are outside of valid scope.
121 VariableDeclaration: checkForVariables
122 };
123
124 }
125};