Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a8683db

Browse files
committed
test: add tests from eslint-scope
1 parent 3759420 commit a8683db

40 files changed

+4718
-12
lines changed

.vscode/launch.json

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@
8383
"sourceMaps": true,
8484
"console": "integratedTerminal",
8585
"internalConsoleOptions": "neverOpen"
86-
}
86+
},
87+
{
88+
"type": "node",
89+
"request": "launch",
90+
"name": "Run currently opened scope-manager test",
91+
"cwd": "${workspaceFolder}/packages/scope-manager/",
92+
"program": "${workspaceFolder}/node_modules/jest/bin/jest.js",
93+
"args": [
94+
"--runInBand",
95+
"--no-cache",
96+
"--no-coverage",
97+
"${fileBasename}"
98+
],
99+
"sourceMaps": true,
100+
"console": "integratedTerminal",
101+
"internalConsoleOptions": "neverOpen"
102+
},
87103
]
88104
}

packages/scope-manager/jest.config.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ module.exports = {
88
},
99
testEnvironment: 'node',
1010
transform: {
11-
'^.+\\.tsx?$': 'ts-jest',
11+
[/^.+\.tsx?$/.source]: 'ts-jest',
1212
},
13-
testRegex: './tests/.+\\.test\\.ts$',
13+
testRegex: [
14+
/.\/tests\/.+\.test\.ts$/.source,
15+
/.\/tests\/eslint-scope\/[^/]+\.test\.ts$/.source,
16+
],
1417
collectCoverage: false,
1518
collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}'],
1619
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],

packages/scope-manager/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
"@typescript-eslint/experimental-utils": "2.29.0",
4040
"esrecurse": "^4.2.1"
4141
},
42+
"devDependencies": {
43+
"@typescript-eslint/parser": "2.29.0"
44+
},
4245
"funding": {
4346
"type": "opencollective",
4447
"url": "https://opencollective.com/typescript-eslint"

packages/scope-manager/src/ScopeManager.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { TSESTree } from '@typescript-eslint/experimental-utils';
12
import assert from 'assert';
23
import { Scope } from './scope';
4+
import { Scopes } from './scope/Scopes';
35
import { ClassScope } from './scope/ClassScope';
46
import { ForScope } from './scope/ForScope';
57
import { FunctionScope } from './scope/FunctionScope';
@@ -10,7 +12,6 @@ import { CatchScope } from './scope/CatchScope';
1012
import { FunctionExpressionNameScope } from './scope/FunctionExpressionNameScope';
1113
import { ModuleScope } from './scope/ModuleScope';
1214
import { GlobalScope } from './scope/GlobalScope';
13-
import { TSESTree } from '@typescript-eslint/experimental-utils';
1415
import { Variable } from './variable';
1516

1617
interface ScopeManagerOptions {
@@ -21,7 +22,7 @@ interface ScopeManagerOptions {
2122
}
2223

2324
class ScopeManager {
24-
public scopes: Scope[];
25+
public scopes: Scopes[];
2526
public globalScope: GlobalScope | null;
2627
private __options: ScopeManagerOptions;
2728
public __currentScope: Scope | null;

packages/scope-manager/src/Visitor.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ interface VisitorKeys {
88
interface VisitorOptions {
99
processRightHandNodes?: boolean;
1010
childVisitorKeys?: VisitorKeys | null;
11-
fallback?: 'iteration' | FallbackFn;
11+
fallback?: 'iteration' | 'none' | FallbackFn;
1212
}
1313

14-
declare class VisitorType {
14+
declare class ESRecurseVisitorType {
1515
__fallback: FallbackFn;
16-
__visitor: VisitorType;
16+
__visitor: ESRecurseVisitorType;
1717
__childVisitorKeys: VisitorKeys;
1818

19-
constructor(visitor: VisitorType | null, options: VisitorOptions);
19+
constructor(visitor: ESRecurseVisitorType | null, options: VisitorOptions);
2020

2121
/**
2222
* Default method for visiting children.
@@ -30,6 +30,6 @@ declare class VisitorType {
3030
*/
3131
visit(node: TSESTree.Node | null): void;
3232
}
33-
const Visitor = ESRecurseVisitor as typeof VisitorType;
33+
class Visitor extends (ESRecurseVisitor as typeof ESRecurseVisitorType) {}
3434

3535
export { Visitor, VisitorOptions, VisitorKeys };

packages/scope-manager/src/scope/ScopeBase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ class ScopeBase {
394394
__referencing(
395395
node: TSESTree.Node,
396396
assign?: ReferenceFlag,
397-
writeExpr?: TSESTree.Node | null,
397+
writeExpr?: TSESTree.Expression | null,
398398
maybeImplicitGlobal?: ReferenceImplicitGlobal | null,
399399
partial?: boolean,
400400
init?: boolean,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# `scope-manager` `eslint-scope` Tests
2+
3+
These tests are taken from the [`eslint-scope`](https://github.com/eslint/eslint-scope) package.
4+
The intention is to help us ensure we do not regress functionality compared to the original package.
5+
6+
They have been modified to:
7+
8+
- be written in TypeScript
9+
- work with jest
10+
- work with our folder structure
11+
- adhere to our formatting and linting style
12+
13+
[The code was forked from `dbddf14d5771b21b5da704213e4508c660ca1c64`](https://github.com/eslint/eslint-scope/blob/dbddf14d5771b21b5da704213e4508c660ca1c64/tests/).
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expectToBeFunctionScope, expectToBeGlobalScope } from './util/expect';
2+
import { parse } from './util/parse';
3+
import { analyze } from '../../src/analyze';
4+
5+
describe('arguments', () => {
6+
it('arguments are correctly materialized', () => {
7+
const ast = parse(`
8+
(function () {
9+
arguments;
10+
}());
11+
`);
12+
13+
const scopeManager = analyze(ast);
14+
15+
expect(scopeManager.scopes).toHaveLength(2);
16+
17+
let scope = scopeManager.scopes[0];
18+
expectToBeGlobalScope(scope);
19+
expect(scope.variables).toHaveLength(0);
20+
expect(scope.references).toHaveLength(0);
21+
22+
scope = scopeManager.scopes[1];
23+
expectToBeFunctionScope(scope);
24+
expect(scope.variables).toHaveLength(1);
25+
expect(scope.variables[0].name).toBe('arguments');
26+
expect(scope.isArgumentsMaterialized()).toBeTruthy();
27+
expect(scope.references).toHaveLength(1);
28+
expect(scope.references[0].resolved).toBe(scope.variables[0]);
29+
});
30+
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {
2+
expectToBeBlockScope,
3+
expectToBeCatchScope,
4+
expectToBeFunctionScope,
5+
expectToBeGlobalScope,
6+
} from './util/expect';
7+
import { parse } from './util/parse';
8+
import { analyze } from '../../src/analyze';
9+
10+
describe('catch', () => {
11+
it('creates scope', () => {
12+
const ast = parse(`
13+
(function () {
14+
try {
15+
} catch (e) {
16+
}
17+
}());
18+
`);
19+
20+
const scopeManager = analyze(ast);
21+
22+
expect(scopeManager.scopes).toHaveLength(5);
23+
24+
let scope = scopeManager.scopes[0];
25+
expectToBeGlobalScope(scope);
26+
expect(scope.variables).toHaveLength(0);
27+
expect(scope.references).toHaveLength(0);
28+
29+
scope = scopeManager.scopes[1];
30+
expectToBeFunctionScope(scope);
31+
expect(scope.variables).toHaveLength(1);
32+
expect(scope.variables[0].name).toBe('arguments');
33+
expect(scope.isArgumentsMaterialized()).toBeFalsy();
34+
expect(scope.references).toHaveLength(0);
35+
36+
scope = scopeManager.scopes[2];
37+
expectToBeBlockScope(scope);
38+
expect(scope.variables).toHaveLength(0);
39+
expect(scope.isArgumentsMaterialized()).toBeTruthy();
40+
expect(scope.references).toHaveLength(0);
41+
42+
scope = scopeManager.scopes[3];
43+
expectToBeCatchScope(scope);
44+
expect(scope.variables).toHaveLength(1);
45+
expect(scope.variables[0].name).toBe('e');
46+
expect(scope.isArgumentsMaterialized()).toBeTruthy();
47+
expect(scope.references).toHaveLength(0);
48+
49+
scope = scopeManager.scopes[4];
50+
expectToBeBlockScope(scope);
51+
expect(scope.variables).toHaveLength(0);
52+
expect(scope.isArgumentsMaterialized()).toBeTruthy();
53+
expect(scope.references).toHaveLength(0);
54+
});
55+
});
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { TSESTree } from '@typescript-eslint/experimental-utils';
2+
import { parse } from './util/parse';
3+
import { analyze } from '../../src/analyze';
4+
5+
describe('childVisitorKeys option', () => {
6+
it('should handle as a known node if the childVisitorKeys option was given.', () => {
7+
const ast = parse(`
8+
var foo = 0;
9+
`);
10+
11+
const decl = ast.body[0] as TSESTree.VariableDeclaration;
12+
decl.declarations[0].init!.type = 'NumericLiteral' as never;
13+
14+
// should no error
15+
analyze(ast, {
16+
fallback: 'none',
17+
childVisitorKeys: {
18+
NumericLiteral: [],
19+
},
20+
});
21+
});
22+
23+
it('should not visit to properties which are not given.', () => {
24+
const ast = parse(`
25+
let foo = bar;
26+
`);
27+
28+
const decl = ast.body[0] as TSESTree.VariableDeclaration;
29+
decl.declarations[0].init = {
30+
type: 'TestNode',
31+
argument: decl.declarations[0].init,
32+
} as never;
33+
34+
const result = analyze(ast, {
35+
childVisitorKeys: {
36+
TestNode: [],
37+
},
38+
});
39+
40+
expect(result.scopes).toHaveLength(1);
41+
const globalScope = result.scopes[0];
42+
43+
// `bar` in TestNode has not been visited.
44+
expect(globalScope.through).toHaveLength(0);
45+
});
46+
47+
it('should visit to given properties.', () => {
48+
const ast = parse(`
49+
let foo = bar;
50+
`);
51+
52+
const decl = ast.body[0] as TSESTree.VariableDeclaration;
53+
decl.declarations[0].init = {
54+
type: 'TestNode',
55+
argument: decl.declarations[0].init,
56+
} as never;
57+
58+
const result = analyze(ast, {
59+
childVisitorKeys: {
60+
TestNode: ['argument'],
61+
},
62+
});
63+
64+
expect(result.scopes).toHaveLength(1);
65+
const globalScope = result.scopes[0];
66+
67+
// `bar` in TestNode has been visited.
68+
expect(globalScope.through).toHaveLength(1);
69+
expect(globalScope.through[0].identifier.name).toBe('bar');
70+
});
71+
});

0 commit comments

Comments
 (0)