/
child-visitor-keys.test.ts
54 lines (44 loc) · 1.39 KB
/
child-visitor-keys.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
import { TSESTree } from '@typescript-eslint/typescript-estree';
import { parse } from '../util';
import { analyze } from '../../src/analyze';
describe('childVisitorKeys option', () => {
it('should not visit to properties which are not given.', () => {
const ast = parse(`
let foo = bar;
`);
const decl = ast.body[0] as TSESTree.VariableDeclaration;
decl.declarations[0].init = {
type: 'TestNode',
argument: decl.declarations[0].init,
} as never;
const result = analyze(ast, {
childVisitorKeys: {
TestNode: [],
},
});
expect(result.scopes).toHaveLength(1);
const globalScope = result.scopes[0];
// `bar` in TestNode has not been visited.
expect(globalScope.through).toHaveLength(0);
});
it('should visit to given properties.', () => {
const ast = parse(`
let foo = bar;
`);
const decl = ast.body[0] as TSESTree.VariableDeclaration;
decl.declarations[0].init = {
type: 'TestNode',
argument: decl.declarations[0].init,
} as never;
const result = analyze(ast, {
childVisitorKeys: {
TestNode: ['argument'],
},
});
expect(result.scopes).toHaveLength(1);
const globalScope = result.scopes[0];
// `bar` in TestNode has been visited.
expect(globalScope.through).toHaveLength(1);
expect(globalScope.through[0].identifier.name).toBe('bar');
});
});