Skip to content

Commit e08692c

Browse files
committed
add more tests
1 parent fadd015 commit e08692c

File tree

1 file changed

+273
-1
lines changed

1 file changed

+273
-1
lines changed

packages/eslint-scope/tests/add-globals.js

Lines changed: 273 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import espree from "./util/espree.js";
88
import { analyze } from "../lib/index.js";
99

1010
describe("ScopeManager#addGlobals", () => {
11-
it("adds variables to the global scope and closes references from the global scope", () => {
11+
it("adds variables to the global scope and resolves references from the global scope", () => {
1212
const ast = espree(`
1313
foo = bar + bar;
1414
`);
@@ -25,10 +25,13 @@ describe("ScopeManager#addGlobals", () => {
2525
expect(globalScope.set.size).to.be.equal(0);
2626
expect(globalScope.references).to.have.length(3);
2727
expect(globalScope.references[0].identifier.name).to.be.equal("foo");
28+
expect(globalScope.references[0].from).to.be.equal(globalScope);
2829
expect(globalScope.references[0].resolved).to.be.null;
2930
expect(globalScope.references[1].identifier.name).to.be.equal("bar");
31+
expect(globalScope.references[1].from).to.be.equal(globalScope);
3032
expect(globalScope.references[1].resolved).to.be.null;
3133
expect(globalScope.references[2].identifier.name).to.be.equal("bar");
34+
expect(globalScope.references[2].from).to.be.equal(globalScope);
3235
expect(globalScope.references[2].resolved).to.be.null;
3336
expect(globalScope.references[1]).to.not.be.equal(globalScope.references[2]);
3437
expect(globalScope.through).to.have.length(3);
@@ -37,6 +40,9 @@ describe("ScopeManager#addGlobals", () => {
3740
expect(globalScope.through[2]).to.be.equal(globalScope.references[2]);
3841
expect(globalScope.implicit.variables).to.have.length(1);
3942
expect(globalScope.implicit.variables[0].name).to.be.equal("foo");
43+
expect(globalScope.implicit.variables[0].references).to.have.length(0);
44+
expect(globalScope.implicit.variables[0].defs).to.have.length(1);
45+
expect(globalScope.implicit.variables[0].identifiers).to.have.length(1);
4046
expect(globalScope.implicit.set.size).to.be.equal(1);
4147
expect(globalScope.implicit.set.get("foo")).to.be.equal(globalScope.implicit.variables[0]);
4248
expect(globalScope.implicit.left).to.have.length(3);
@@ -60,12 +66,278 @@ describe("ScopeManager#addGlobals", () => {
6066
expect(globalScope.set.get("bar")).to.be.equal(globalScope.variables[1]);
6167
expect(globalScope.references).to.have.length(3);
6268
expect(globalScope.references[0].identifier.name).to.be.equal("foo");
69+
expect(globalScope.references[0].from).to.be.equal(globalScope);
6370
expect(globalScope.references[0].resolved).to.be.equal(globalScope.variables[0]);
6471
expect(globalScope.variables[0].references).to.have.length(1);
6572
expect(globalScope.variables[0].references[0]).to.be.equal(globalScope.references[0]);
6673
expect(globalScope.references[1].identifier.name).to.be.equal("bar");
74+
expect(globalScope.references[1].from).to.be.equal(globalScope);
6775
expect(globalScope.references[1].resolved).to.be.equal(globalScope.variables[1]);
6876
expect(globalScope.references[2].identifier.name).to.be.equal("bar");
77+
expect(globalScope.references[2].from).to.be.equal(globalScope);
78+
expect(globalScope.references[2].resolved).to.be.equal(globalScope.variables[1]);
79+
expect(globalScope.variables[1].references).to.have.length(2);
80+
expect(globalScope.variables[1].references[0]).to.be.equal(globalScope.references[1]);
81+
expect(globalScope.variables[1].references[1]).to.be.equal(globalScope.references[2]);
82+
expect(globalScope.through).to.have.length(0);
83+
expect(globalScope.implicit.variables).to.have.length(0);
84+
expect(globalScope.implicit.set.size).to.be.equal(0);
85+
expect(globalScope.implicit.left).to.have.length(0);
86+
});
87+
88+
it("adds variables to the global scope and resolves references from inner scopes", () => {
89+
const ast = espree(`
90+
() => foo = bar + bar;
91+
`);
92+
93+
const scopeManager = analyze(ast);
94+
95+
expect(scopeManager.scopes).to.have.length(2);
96+
97+
const globalScope = scopeManager.scopes[0];
98+
99+
expect(globalScope.type).to.be.equal("global");
100+
101+
const functionScope = scopeManager.scopes[1];
102+
103+
expect(functionScope.type).to.be.equal("function");
104+
105+
expect(functionScope.variables).to.have.length(0);
106+
expect(functionScope.set.size).to.be.equal(0);
107+
expect(functionScope.references).to.have.length(3);
108+
expect(functionScope.references[0].identifier.name).to.be.equal("foo");
109+
expect(functionScope.references[0].from).to.be.equal(functionScope);
110+
expect(functionScope.references[0].resolved).to.be.null;
111+
expect(functionScope.references[1].identifier.name).to.be.equal("bar");
112+
expect(functionScope.references[1].from).to.be.equal(functionScope);
113+
expect(functionScope.references[1].resolved).to.be.null;
114+
expect(functionScope.references[2].identifier.name).to.be.equal("bar");
115+
expect(functionScope.references[2].from).to.be.equal(functionScope);
116+
expect(functionScope.references[2].resolved).to.be.null;
117+
expect(functionScope.references[1]).to.not.be.equal(functionScope.references[2]);
118+
expect(functionScope.through).to.have.length(3);
119+
expect(functionScope.through[0]).to.be.equal(functionScope.references[0]);
120+
expect(functionScope.through[1]).to.be.equal(functionScope.references[1]);
121+
expect(functionScope.through[2]).to.be.equal(functionScope.references[2]);
122+
expect(globalScope.variables).to.have.length(0);
123+
expect(globalScope.set.size).to.be.equal(0);
124+
expect(globalScope.references).to.have.length(0);
125+
expect(globalScope.through).to.have.length(3);
126+
expect(globalScope.through[0]).to.be.equal(functionScope.references[0]);
127+
expect(globalScope.through[1]).to.be.equal(functionScope.references[1]);
128+
expect(globalScope.through[2]).to.be.equal(functionScope.references[2]);
129+
expect(globalScope.implicit.variables).to.have.length(1);
130+
expect(globalScope.implicit.variables[0].name).to.be.equal("foo");
131+
expect(globalScope.implicit.variables[0].references).to.have.length(0);
132+
expect(globalScope.implicit.variables[0].defs).to.have.length(1);
133+
expect(globalScope.implicit.variables[0].identifiers).to.have.length(1);
134+
expect(globalScope.implicit.set.size).to.be.equal(1);
135+
expect(globalScope.implicit.set.get("foo")).to.be.equal(globalScope.implicit.variables[0]);
136+
expect(globalScope.implicit.left).to.have.length(3);
137+
expect(globalScope.implicit.left[0]).to.be.equal(functionScope.references[0]);
138+
expect(globalScope.implicit.left[1]).to.be.equal(functionScope.references[1]);
139+
expect(globalScope.implicit.left[2]).to.be.equal(functionScope.references[2]);
140+
141+
scopeManager.addGlobals(["foo", "bar"]);
142+
143+
expect(globalScope.variables).to.have.length(2);
144+
expect(globalScope.variables[0].name).to.be.equal("foo");
145+
expect(globalScope.variables[0].scope).to.be.equal(globalScope);
146+
expect(globalScope.variables[0].defs).to.have.length(0);
147+
expect(globalScope.variables[0].identifiers).to.have.length(0);
148+
expect(globalScope.variables[1].name).to.be.equal("bar");
149+
expect(globalScope.variables[1].scope).to.be.equal(globalScope);
150+
expect(globalScope.variables[1].defs).to.have.length(0);
151+
expect(globalScope.variables[1].identifiers).to.have.length(0);
152+
expect(globalScope.set.size).to.be.equal(2);
153+
expect(globalScope.set.get("foo")).to.be.equal(globalScope.variables[0]);
154+
expect(globalScope.set.get("bar")).to.be.equal(globalScope.variables[1]);
155+
expect(functionScope.variables).to.have.length(0);
156+
expect(functionScope.set.size).to.be.equal(0);
157+
expect(functionScope.references).to.have.length(3);
158+
expect(functionScope.references[0].identifier.name).to.be.equal("foo");
159+
expect(functionScope.references[0].from).to.be.equal(functionScope);
160+
expect(functionScope.references[0].resolved).to.be.equal(globalScope.variables[0]);
161+
expect(globalScope.variables[0].references).to.have.length(1);
162+
expect(globalScope.variables[0].references[0]).to.be.equal(functionScope.references[0]);
163+
expect(functionScope.references[1].identifier.name).to.be.equal("bar");
164+
expect(functionScope.references[1].from).to.be.equal(functionScope);
165+
expect(functionScope.references[1].resolved).to.be.equal(globalScope.variables[1]);
166+
expect(functionScope.references[2].identifier.name).to.be.equal("bar");
167+
expect(functionScope.references[2].from).to.be.equal(functionScope);
168+
expect(functionScope.references[2].resolved).to.be.equal(globalScope.variables[1]);
169+
expect(functionScope.references[1]).to.not.be.equal(functionScope.references[2]);
170+
expect(globalScope.variables[1].references).to.have.length(2);
171+
expect(globalScope.variables[1].references[0]).to.be.equal(functionScope.references[1]);
172+
expect(globalScope.variables[1].references[1]).to.be.equal(functionScope.references[2]);
173+
expect(functionScope.through).to.have.length(3);
174+
expect(functionScope.through[0]).to.be.equal(functionScope.references[0]);
175+
expect(functionScope.through[1]).to.be.equal(functionScope.references[1]);
176+
expect(functionScope.through[2]).to.be.equal(functionScope.references[2]);
177+
expect(globalScope.references).to.have.length(0);
178+
expect(globalScope.through).to.have.length(0);
179+
expect(globalScope.implicit.variables).to.have.length(0);
180+
expect(globalScope.implicit.set.size).to.be.equal(0);
181+
expect(globalScope.implicit.left).to.have.length(0);
182+
});
183+
184+
it("adds variables to the global scope and doesn't affect unrelated references", () => {
185+
const ast = espree(`
186+
foo = bar + bar;
187+
`);
188+
189+
const scopeManager = analyze(ast);
190+
191+
expect(scopeManager.scopes).to.have.length(1);
192+
193+
const globalScope = scopeManager.scopes[0];
194+
195+
expect(globalScope.type).to.be.equal("global");
196+
197+
expect(globalScope.variables).to.have.length(0);
198+
expect(globalScope.set.size).to.be.equal(0);
199+
expect(globalScope.references).to.have.length(3);
200+
expect(globalScope.references[0].identifier.name).to.be.equal("foo");
201+
expect(globalScope.references[0].from).to.be.equal(globalScope);
202+
expect(globalScope.references[0].resolved).to.be.null;
203+
expect(globalScope.references[1].identifier.name).to.be.equal("bar");
204+
expect(globalScope.references[1].from).to.be.equal(globalScope);
205+
expect(globalScope.references[1].resolved).to.be.null;
206+
expect(globalScope.references[2].identifier.name).to.be.equal("bar");
207+
expect(globalScope.references[2].from).to.be.equal(globalScope);
208+
expect(globalScope.references[2].resolved).to.be.null;
209+
expect(globalScope.references[1]).to.not.be.equal(globalScope.references[2]);
210+
expect(globalScope.through).to.have.length(3);
211+
expect(globalScope.through[0]).to.be.equal(globalScope.references[0]);
212+
expect(globalScope.through[1]).to.be.equal(globalScope.references[1]);
213+
expect(globalScope.through[2]).to.be.equal(globalScope.references[2]);
214+
expect(globalScope.implicit.variables).to.have.length(1);
215+
expect(globalScope.implicit.variables[0].name).to.be.equal("foo");
216+
expect(globalScope.implicit.variables[0].references).to.have.length(0);
217+
expect(globalScope.implicit.variables[0].defs).to.have.length(1);
218+
expect(globalScope.implicit.variables[0].identifiers).to.have.length(1);
219+
expect(globalScope.implicit.set.size).to.be.equal(1);
220+
expect(globalScope.implicit.set.get("foo")).to.be.equal(globalScope.implicit.variables[0]);
221+
expect(globalScope.implicit.left).to.have.length(3);
222+
expect(globalScope.implicit.left[0]).to.be.equal(globalScope.references[0]);
223+
expect(globalScope.implicit.left[1]).to.be.equal(globalScope.references[1]);
224+
expect(globalScope.implicit.left[2]).to.be.equal(globalScope.references[2]);
225+
226+
scopeManager.addGlobals(["baz", "qux"]);
227+
228+
expect(globalScope.variables).to.have.length(2);
229+
expect(globalScope.variables[0].name).to.be.equal("baz");
230+
expect(globalScope.variables[0].scope).to.be.equal(globalScope);
231+
expect(globalScope.variables[0].defs).to.have.length(0);
232+
expect(globalScope.variables[0].identifiers).to.have.length(0);
233+
expect(globalScope.variables[0].references).to.have.length(0);
234+
expect(globalScope.variables[1].name).to.be.equal("qux");
235+
expect(globalScope.variables[1].scope).to.be.equal(globalScope);
236+
expect(globalScope.variables[1].defs).to.have.length(0);
237+
expect(globalScope.variables[1].identifiers).to.have.length(0);
238+
expect(globalScope.variables[1].references).to.have.length(0);
239+
expect(globalScope.set.size).to.be.equal(2);
240+
expect(globalScope.set.get("baz")).to.be.equal(globalScope.variables[0]);
241+
expect(globalScope.set.get("qux")).to.be.equal(globalScope.variables[1]);
242+
expect(globalScope.references).to.have.length(3);
243+
expect(globalScope.references[0].identifier.name).to.be.equal("foo");
244+
expect(globalScope.references[0].from).to.be.equal(globalScope);
245+
expect(globalScope.references[0].resolved).to.be.null;
246+
expect(globalScope.references[1].identifier.name).to.be.equal("bar");
247+
expect(globalScope.references[1].from).to.be.equal(globalScope);
248+
expect(globalScope.references[1].resolved).to.be.null;
249+
expect(globalScope.references[2].identifier.name).to.be.equal("bar");
250+
expect(globalScope.references[2].from).to.be.equal(globalScope);
251+
expect(globalScope.references[2].resolved).to.be.null;
252+
expect(globalScope.references[1]).to.not.be.equal(globalScope.references[2]);
253+
expect(globalScope.through).to.have.length(3);
254+
expect(globalScope.through[0]).to.be.equal(globalScope.references[0]);
255+
expect(globalScope.through[1]).to.be.equal(globalScope.references[1]);
256+
expect(globalScope.through[2]).to.be.equal(globalScope.references[2]);
257+
expect(globalScope.implicit.variables).to.have.length(1);
258+
expect(globalScope.implicit.variables[0].name).to.be.equal("foo");
259+
expect(globalScope.implicit.variables[0].references).to.have.length(0);
260+
expect(globalScope.implicit.variables[0].defs).to.have.length(1);
261+
expect(globalScope.implicit.variables[0].identifiers).to.have.length(1);
262+
expect(globalScope.implicit.set.size).to.be.equal(1);
263+
expect(globalScope.implicit.set.get("foo")).to.be.equal(globalScope.implicit.variables[0]);
264+
expect(globalScope.implicit.left).to.have.length(3);
265+
expect(globalScope.implicit.left[0]).to.be.equal(globalScope.references[0]);
266+
expect(globalScope.implicit.left[1]).to.be.equal(globalScope.references[1]);
267+
expect(globalScope.implicit.left[2]).to.be.equal(globalScope.references[2]);
268+
});
269+
270+
it("doesn't affect already declared global variables", () => {
271+
const ast = espree(`
272+
let foo = bar + bar;
273+
var bar;
274+
`);
275+
276+
const scopeManager = analyze(ast);
277+
278+
expect(scopeManager.scopes).to.have.length(1);
279+
280+
const globalScope = scopeManager.scopes[0];
281+
282+
expect(globalScope.type).to.be.equal("global");
283+
284+
expect(globalScope.variables).to.have.length(2);
285+
expect(globalScope.variables[0].name).to.be.equal("foo");
286+
expect(globalScope.variables[0].scope).to.be.equal(globalScope);
287+
expect(globalScope.variables[0].defs).to.have.length(1);
288+
expect(globalScope.variables[0].identifiers).to.have.length(1);
289+
expect(globalScope.variables[1].name).to.be.equal("bar");
290+
expect(globalScope.variables[1].scope).to.be.equal(globalScope);
291+
expect(globalScope.variables[1].defs).to.have.length(1);
292+
expect(globalScope.variables[1].identifiers).to.have.length(1);
293+
expect(globalScope.set.size).to.be.equal(2);
294+
expect(globalScope.set.get("foo")).to.be.equal(globalScope.variables[0]);
295+
expect(globalScope.set.get("bar")).to.be.equal(globalScope.variables[1]);
296+
expect(globalScope.references).to.have.length(3);
297+
expect(globalScope.references[0].identifier.name).to.be.equal("foo");
298+
expect(globalScope.references[0].from).to.be.equal(globalScope);
299+
expect(globalScope.references[0].resolved).to.be.equal(globalScope.variables[0]);
300+
expect(globalScope.variables[0].references).to.have.length(1);
301+
expect(globalScope.variables[0].references[0]).to.be.equal(globalScope.references[0]);
302+
expect(globalScope.references[1].identifier.name).to.be.equal("bar");
303+
expect(globalScope.references[1].from).to.be.equal(globalScope);
304+
expect(globalScope.references[1].resolved).to.be.equal(globalScope.variables[1]);
305+
expect(globalScope.references[2].identifier.name).to.be.equal("bar");
306+
expect(globalScope.references[2].from).to.be.equal(globalScope);
307+
expect(globalScope.references[2].resolved).to.be.equal(globalScope.variables[1]);
308+
expect(globalScope.variables[1].references).to.have.length(2);
309+
expect(globalScope.variables[1].references[0]).to.be.equal(globalScope.references[1]);
310+
expect(globalScope.variables[1].references[1]).to.be.equal(globalScope.references[2]);
311+
expect(globalScope.through).to.have.length(0);
312+
expect(globalScope.implicit.variables).to.have.length(0);
313+
expect(globalScope.implicit.set.size).to.be.equal(0);
314+
expect(globalScope.implicit.left).to.have.length(0);
315+
316+
scopeManager.addGlobals(["foo", "bar"]);
317+
318+
expect(globalScope.variables).to.have.length(2);
319+
expect(globalScope.variables[0].name).to.be.equal("foo");
320+
expect(globalScope.variables[0].scope).to.be.equal(globalScope);
321+
expect(globalScope.variables[0].defs).to.have.length(1);
322+
expect(globalScope.variables[0].identifiers).to.have.length(1);
323+
expect(globalScope.variables[1].name).to.be.equal("bar");
324+
expect(globalScope.variables[1].scope).to.be.equal(globalScope);
325+
expect(globalScope.variables[1].defs).to.have.length(1);
326+
expect(globalScope.variables[1].identifiers).to.have.length(1);
327+
expect(globalScope.set.size).to.be.equal(2);
328+
expect(globalScope.set.get("foo")).to.be.equal(globalScope.variables[0]);
329+
expect(globalScope.set.get("bar")).to.be.equal(globalScope.variables[1]);
330+
expect(globalScope.references).to.have.length(3);
331+
expect(globalScope.references[0].identifier.name).to.be.equal("foo");
332+
expect(globalScope.references[0].from).to.be.equal(globalScope);
333+
expect(globalScope.references[0].resolved).to.be.equal(globalScope.variables[0]);
334+
expect(globalScope.variables[0].references).to.have.length(1);
335+
expect(globalScope.variables[0].references[0]).to.be.equal(globalScope.references[0]);
336+
expect(globalScope.references[1].identifier.name).to.be.equal("bar");
337+
expect(globalScope.references[1].from).to.be.equal(globalScope);
338+
expect(globalScope.references[1].resolved).to.be.equal(globalScope.variables[1]);
339+
expect(globalScope.references[2].identifier.name).to.be.equal("bar");
340+
expect(globalScope.references[2].from).to.be.equal(globalScope);
69341
expect(globalScope.references[2].resolved).to.be.equal(globalScope.variables[1]);
70342
expect(globalScope.variables[1].references).to.have.length(2);
71343
expect(globalScope.variables[1].references[0]).to.be.equal(globalScope.references[1]);

0 commit comments

Comments
 (0)