/
global-return.test.ts
67 lines (56 loc) · 2.15 KB
/
global-return.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
62
63
64
65
66
67
import { AST_NODE_TYPES } from '@typescript-eslint/typescript-estree';
import {
expectToBeGlobalScope,
expectToBeFunctionScope,
expectToBeModuleScope,
expectToBeImportBindingDefinition,
} from '../util';
import { parseAndAnalyze } from '../util/parse';
describe('gloablReturn option', () => {
it('creates a function scope following the global scope immediately', () => {
const { scopeManager } = parseAndAnalyze(
`
"use strict";
var hello = 20;
`,
{ globalReturn: true },
);
expect(scopeManager.scopes).toHaveLength(2);
let scope = scopeManager.scopes[0];
expectToBeGlobalScope(scope);
expect(scope.block.type).toBe(AST_NODE_TYPES.Program);
expect(scope.isStrict).toBeFalsy();
expect(scope.variables).toHaveLength(0);
scope = scopeManager.scopes[1];
expectToBeFunctionScope(scope);
expect(scope.block.type).toBe(AST_NODE_TYPES.Program);
expect(scope.isStrict).toBeTruthy();
expect(scope.variables).toHaveLength(2);
expect(scope.variables[0].name).toBe('arguments');
expect(scope.variables[1].name).toBe('hello');
});
it('creates a function scope following the global scope immediately and creates module scope', () => {
const { scopeManager } = parseAndAnalyze("import {x as v} from 'mod';", {
sourceType: 'module',
globalReturn: true,
});
expect(scopeManager.scopes).toHaveLength(3);
let scope = scopeManager.scopes[0];
expectToBeGlobalScope(scope);
expect(scope.block.type).toBe(AST_NODE_TYPES.Program);
expect(scope.isStrict).toBeFalsy();
expect(scope.variables).toHaveLength(0);
scope = scopeManager.scopes[1];
expectToBeFunctionScope(scope);
expect(scope.block.type).toBe(AST_NODE_TYPES.Program);
expect(scope.isStrict).toBeFalsy();
expect(scope.variables).toHaveLength(1);
expect(scope.variables[0].name).toBe('arguments');
scope = scopeManager.scopes[2];
expectToBeModuleScope(scope);
expect(scope.variables).toHaveLength(1);
expect(scope.variables[0].name).toBe('v');
expectToBeImportBindingDefinition(scope.variables[0].defs[0]);
expect(scope.references).toHaveLength(0);
});
});