]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/block-scoped-var.js
d98250b062bcb22a2ef9aeec78e62eed7e6255a5
2 * @fileoverview Rule to check for "block scoped" variables by binding context
3 * @author Matt DuVall <http://www.mattduvall.com>
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
16 description
: "enforce the use of variables within the scope they are defined",
18 url
: "https://eslint.org/docs/rules/block-scoped-var"
24 outOfScope
: "'{{name}}' used outside of binding context."
32 * Makes a block scope.
33 * @param {ASTNode} node A node of a scope.
36 function enterScope(node
) {
37 stack
.push(node
.range
);
41 * Pops the last block scope.
44 function exitScope() {
49 * Reports a given reference.
50 * @param {eslint-scope.Reference} reference A reference to report.
53 function report(reference
) {
54 const identifier
= reference
.identifier
;
56 context
.report({ node
: identifier
, messageId
: "outOfScope", data
: { name
: identifier
.name
} });
60 * Finds and reports references which are outside of valid scopes.
61 * @param {ASTNode} node A node to get variables.
64 function checkForVariables(node
) {
65 if (node
.kind
!== "var") {
69 // Defines a predicate to check whether or not a given reference is outside of valid scope.
70 const scopeRange
= stack
[stack
.length
- 1];
73 * Check if a reference is out of scope
74 * @param {ASTNode} reference node to examine
75 * @returns {boolean} True is its outside the scope
78 function isOutsideOfScope(reference
) {
79 const idRange
= reference
.identifier
.range
;
81 return idRange
[0] < scopeRange
[0] || idRange
[1] > scopeRange
[1];
84 // Gets declared variables, and checks its references.
85 const variables
= context
.getDeclaredVariables(node
);
87 for (let i
= 0; i
< variables
.length
; ++i
) {
92 .filter(isOutsideOfScope
)
103 BlockStatement
: enterScope
,
104 "BlockStatement:exit": exitScope
,
105 ForStatement
: enterScope
,
106 "ForStatement:exit": exitScope
,
107 ForInStatement
: enterScope
,
108 "ForInStatement:exit": exitScope
,
109 ForOfStatement
: enterScope
,
110 "ForOfStatement:exit": exitScope
,
111 SwitchStatement
: enterScope
,
112 "SwitchStatement:exit": exitScope
,
113 CatchClause
: enterScope
,
114 "CatchClause:exit": exitScope
,
115 StaticBlock
: enterScope
,
116 "StaticBlock:exit": exitScope
,
118 // Finds and reports references which are outside of valid scope.
119 VariableDeclaration
: checkForVariables