-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
label.test.ts
61 lines (51 loc) · 1.78 KB
/
label.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import {
expectToBeBlockScope,
expectToBeFunctionScope,
expectToBeGlobalScope,
parseAndAnalyze,
} from '../util';
describe('label', () => {
it('should not create variables', () => {
const { scopeManager } = parseAndAnalyze(
'function bar() { q: for(;;) { break q; } }',
);
expect(scopeManager.scopes).toHaveLength(3);
let scope = scopeManager.scopes[0];
expectToBeGlobalScope(scope);
expect(scope.variables).toHaveLength(1);
expect(scope.variables[0].name).toBe('bar');
expect(scope.references).toHaveLength(0);
scope = scopeManager.scopes[1];
expectToBeFunctionScope(scope);
expect(scope.variables).toHaveLength(1);
expect(scope.variables[0].name).toBe('arguments');
expect(scope.references).toHaveLength(0);
scope = scopeManager.scopes[2];
expectToBeBlockScope(scope);
expect(scope.variables).toHaveLength(0);
expect(scope.references).toHaveLength(0);
});
it('should count child node references', () => {
const { scopeManager } = parseAndAnalyze(`
var foo = 5;
label: while (true) {
console.log(foo);
break;
}
`);
expect(scopeManager.scopes).toHaveLength(2);
let scope = scopeManager.scopes[0];
expectToBeGlobalScope(scope);
expect(scope.variables).toHaveLength(1);
expect(scope.variables[0].name).toBe('foo');
expect(scope.through).toHaveLength(3);
expect(scope.through[2].identifier.name).toBe('foo');
expect(scope.through[2].isRead()).toBeTruthy();
scope = scopeManager.scopes[1];
expectToBeBlockScope(scope);
expect(scope.variables).toHaveLength(0);
expect(scope.references).toHaveLength(2);
expect(scope.references[0].identifier.name).toBe('console');
expect(scope.references[1].identifier.name).toBe('foo');
});
});