forked from mysticatea/eslint-utils
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathget-innermost-scope.mjs
102 lines (98 loc) · 3.58 KB
/
get-innermost-scope.mjs
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import assert from "assert"
import { getInnermostScope } from "../src/index.mjs"
import { getScope, newCompatLinter } from "./test-lib/eslint-compat.mjs"
describe("The 'getInnermostScope' function", () => {
let i = 0
for (const { code, languageOptions, selectNode, selectScope } of [
{
code: "let a = 0",
languageOptions: {},
selectNode: (node) => node,
selectScope: (scope) => scope,
},
{
code: "let a = 0",
languageOptions: {
parserOptions: { ecmaFeatures: { globalReturn: true } },
},
selectNode: (node) => node,
selectScope: (scope) => scope.childScopes[0],
},
{
code: "let a = 0",
languageOptions: { sourceType: "module" },
selectNode: (node) => node,
selectScope: (scope) => scope.childScopes[0],
},
{
code: "a; { b; { c; } d; } e;",
languageOptions: {},
selectNode: (node) => node.body[0],
selectScope: (scope) => scope,
},
{
code: "a; { b; { c; } d; } e;",
languageOptions: {},
selectNode: (node) => node.body[2],
selectScope: (scope) => scope,
},
{
code: "a; { b; { c; } d; } e;",
languageOptions: {},
selectNode: (node) => node.body[1].body[0],
selectScope: (scope) => scope.childScopes[0],
},
{
code: "a; { b; { c; } d; } e;",
languageOptions: {},
selectNode: (node) => node.body[1].body[2],
selectScope: (scope) => scope.childScopes[0],
},
{
code: "a; { b; { c; } d; } e;",
languageOptions: {},
selectNode: (node) => node.body[1].body[1].body[0],
selectScope: (scope) => scope.childScopes[0].childScopes[0],
},
]) {
it(`should return the innermost scope (${++i})`, () => {
const linter = newCompatLinter()
let actualScope = null
let expectedScope = null
linter.verify(code, {
languageOptions: {
ecmaVersion: 2020,
sourceType: "script",
...languageOptions,
},
rules: { "test/test": "error" },
plugins: {
test: {
rules: {
test: {
create(context) {
return {
Program(node) {
const scope = getScope(
context,
node,
)
actualScope = getInnermostScope(
scope,
selectNode(node),
)
expectedScope = selectScope(scope)
},
}
},
},
},
},
},
})
assert.notStrictEqual(expectedScope, null)
// assert.strictEqual makes tooooo large diff.
assert(actualScope === expectedScope)
})
}
})