UNPKG

7.63 kBJavaScriptView Raw
1/**
2 * @fileoverview Rule to flag use of variables before they are defined
3 * @author Ilya Volodin
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Helpers
10//------------------------------------------------------------------------------
11
12const SENTINEL_TYPE = /^(?:(?:Function|Class)(?:Declaration|Expression)|ArrowFunctionExpression|CatchClause|ImportDeclaration|ExportNamedDeclaration)$/u;
13const FOR_IN_OF_TYPE = /^For(?:In|Of)Statement$/u;
14
15/**
16 * Parses a given value as options.
17 *
18 * @param {any} options - A value to parse.
19 * @returns {Object} The parsed options.
20 */
21function parseOptions(options) {
22 let functions = true;
23 let classes = true;
24 let variables = true;
25
26 if (typeof options === "string") {
27 functions = (options !== "nofunc");
28 } else if (typeof options === "object" && options !== null) {
29 functions = options.functions !== false;
30 classes = options.classes !== false;
31 variables = options.variables !== false;
32 }
33
34 return { functions, classes, variables };
35}
36
37/**
38 * Checks whether or not a given variable is a function declaration.
39 *
40 * @param {eslint-scope.Variable} variable - A variable to check.
41 * @returns {boolean} `true` if the variable is a function declaration.
42 */
43function isFunction(variable) {
44 return variable.defs[0].type === "FunctionName";
45}
46
47/**
48 * Checks whether or not a given variable is a class declaration in an upper function scope.
49 *
50 * @param {eslint-scope.Variable} variable - A variable to check.
51 * @param {eslint-scope.Reference} reference - A reference to check.
52 * @returns {boolean} `true` if the variable is a class declaration.
53 */
54function isOuterClass(variable, reference) {
55 return (
56 variable.defs[0].type === "ClassName" &&
57 variable.scope.variableScope !== reference.from.variableScope
58 );
59}
60
61/**
62 * Checks whether or not a given variable is a variable declaration in an upper function scope.
63 * @param {eslint-scope.Variable} variable - A variable to check.
64 * @param {eslint-scope.Reference} reference - A reference to check.
65 * @returns {boolean} `true` if the variable is a variable declaration.
66 */
67function isOuterVariable(variable, reference) {
68 return (
69 variable.defs[0].type === "Variable" &&
70 variable.scope.variableScope !== reference.from.variableScope
71 );
72}
73
74/**
75 * Checks whether or not a given location is inside of the range of a given node.
76 *
77 * @param {ASTNode} node - An node to check.
78 * @param {number} location - A location to check.
79 * @returns {boolean} `true` if the location is inside of the range of the node.
80 */
81function isInRange(node, location) {
82 return node && node.range[0] <= location && location <= node.range[1];
83}
84
85/**
86 * Checks whether or not a given reference is inside of the initializers of a given variable.
87 *
88 * This returns `true` in the following cases:
89 *
90 * var a = a
91 * var [a = a] = list
92 * var {a = a} = obj
93 * for (var a in a) {}
94 * for (var a of a) {}
95 *
96 * @param {Variable} variable - A variable to check.
97 * @param {Reference} reference - A reference to check.
98 * @returns {boolean} `true` if the reference is inside of the initializers.
99 */
100function isInInitializer(variable, reference) {
101 if (variable.scope !== reference.from) {
102 return false;
103 }
104
105 let node = variable.identifiers[0].parent;
106 const location = reference.identifier.range[1];
107
108 while (node) {
109 if (node.type === "VariableDeclarator") {
110 if (isInRange(node.init, location)) {
111 return true;
112 }
113 if (FOR_IN_OF_TYPE.test(node.parent.parent.type) &&
114 isInRange(node.parent.parent.right, location)
115 ) {
116 return true;
117 }
118 break;
119 } else if (node.type === "AssignmentPattern") {
120 if (isInRange(node.right, location)) {
121 return true;
122 }
123 } else if (SENTINEL_TYPE.test(node.type)) {
124 break;
125 }
126
127 node = node.parent;
128 }
129
130 return false;
131}
132
133//------------------------------------------------------------------------------
134// Rule Definition
135//------------------------------------------------------------------------------
136
137module.exports = {
138 meta: {
139 type: "problem",
140
141 docs: {
142 description: "disallow the use of variables before they are defined",
143 category: "Variables",
144 recommended: false,
145 url: "https://eslint.org/docs/rules/no-use-before-define"
146 },
147
148 schema: [
149 {
150 oneOf: [
151 {
152 enum: ["nofunc"]
153 },
154 {
155 type: "object",
156 properties: {
157 functions: { type: "boolean" },
158 classes: { type: "boolean" },
159 variables: { type: "boolean" }
160 },
161 additionalProperties: false
162 }
163 ]
164 }
165 ]
166 },
167
168 create(context) {
169 const options = parseOptions(context.options[0]);
170
171 /**
172 * Determines whether a given use-before-define case should be reported according to the options.
173 * @param {eslint-scope.Variable} variable The variable that gets used before being defined
174 * @param {eslint-scope.Reference} reference The reference to the variable
175 * @returns {boolean} `true` if the usage should be reported
176 */
177 function isForbidden(variable, reference) {
178 if (isFunction(variable)) {
179 return options.functions;
180 }
181 if (isOuterClass(variable, reference)) {
182 return options.classes;
183 }
184 if (isOuterVariable(variable, reference)) {
185 return options.variables;
186 }
187 return true;
188 }
189
190 /**
191 * Finds and validates all variables in a given scope.
192 * @param {Scope} scope The scope object.
193 * @returns {void}
194 * @private
195 */
196 function findVariablesInScope(scope) {
197 scope.references.forEach(reference => {
198 const variable = reference.resolved;
199
200 /*
201 * Skips when the reference is:
202 * - initialization's.
203 * - referring to an undefined variable.
204 * - referring to a global environment variable (there're no identifiers).
205 * - located preceded by the variable (except in initializers).
206 * - allowed by options.
207 */
208 if (reference.init ||
209 !variable ||
210 variable.identifiers.length === 0 ||
211 (variable.identifiers[0].range[1] < reference.identifier.range[1] && !isInInitializer(variable, reference)) ||
212 !isForbidden(variable, reference)
213 ) {
214 return;
215 }
216
217 // Reports.
218 context.report({
219 node: reference.identifier,
220 message: "'{{name}}' was used before it was defined.",
221 data: reference.identifier
222 });
223 });
224
225 scope.childScopes.forEach(findVariablesInScope);
226 }
227
228 return {
229 Program() {
230 findVariablesInScope(context.getScope());
231 }
232 };
233 }
234};