|
1 | 1 | import assert from "assert" |
2 | | -import eslint from "eslint" |
3 | 2 | import { getInnermostScope } from "../src/index.mjs" |
4 | | -import { getScope } from "./test-lib/get-scope.mjs" |
| 3 | +import { getScope, newCompatLinter } from "./test-lib/eslint-compat.mjs" |
5 | 4 |
|
6 | 5 | describe("The 'getInnermostScope' function", () => { |
7 | 6 | let i = 0 |
8 | | - for (const { code, parserOptions, selectNode, selectScope } of [ |
| 7 | + for (const { code, languageOptions, selectNode, selectScope } of [ |
9 | 8 | { |
10 | 9 | code: "let a = 0", |
11 | | - parserOptions: {}, |
| 10 | + languageOptions: {}, |
12 | 11 | selectNode: (node) => node, |
13 | 12 | selectScope: (scope) => scope, |
14 | 13 | }, |
15 | 14 | { |
16 | 15 | code: "let a = 0", |
17 | | - parserOptions: { ecmaFeatures: { globalReturn: true } }, |
| 16 | + languageOptions: { |
| 17 | + parserOptions: { ecmaFeatures: { globalReturn: true } }, |
| 18 | + }, |
18 | 19 | selectNode: (node) => node, |
19 | 20 | selectScope: (scope) => scope.childScopes[0], |
20 | 21 | }, |
21 | 22 | { |
22 | 23 | code: "let a = 0", |
23 | | - parserOptions: { sourceType: "module" }, |
| 24 | + languageOptions: { sourceType: "module" }, |
24 | 25 | selectNode: (node) => node, |
25 | 26 | selectScope: (scope) => scope.childScopes[0], |
26 | 27 | }, |
27 | 28 | { |
28 | 29 | code: "a; { b; { c; } d; } e;", |
29 | | - parserOptions: {}, |
| 30 | + languageOptions: {}, |
30 | 31 | selectNode: (node) => node.body[0], |
31 | 32 | selectScope: (scope) => scope, |
32 | 33 | }, |
33 | 34 | { |
34 | 35 | code: "a; { b; { c; } d; } e;", |
35 | | - parserOptions: {}, |
| 36 | + languageOptions: {}, |
36 | 37 | selectNode: (node) => node.body[2], |
37 | 38 | selectScope: (scope) => scope, |
38 | 39 | }, |
39 | 40 | { |
40 | 41 | code: "a; { b; { c; } d; } e;", |
41 | | - parserOptions: {}, |
| 42 | + languageOptions: {}, |
42 | 43 | selectNode: (node) => node.body[1].body[0], |
43 | 44 | selectScope: (scope) => scope.childScopes[0], |
44 | 45 | }, |
45 | 46 | { |
46 | 47 | code: "a; { b; { c; } d; } e;", |
47 | | - parserOptions: {}, |
| 48 | + languageOptions: {}, |
48 | 49 | selectNode: (node) => node.body[1].body[2], |
49 | 50 | selectScope: (scope) => scope.childScopes[0], |
50 | 51 | }, |
51 | 52 | { |
52 | 53 | code: "a; { b; { c; } d; } e;", |
53 | | - parserOptions: {}, |
| 54 | + languageOptions: {}, |
54 | 55 | selectNode: (node) => node.body[1].body[1].body[0], |
55 | 56 | selectScope: (scope) => scope.childScopes[0].childScopes[0], |
56 | 57 | }, |
57 | 58 | ]) { |
58 | 59 | it(`should return the innermost scope (${++i})`, () => { |
59 | | - const linter = new eslint.Linter() |
| 60 | + const linter = newCompatLinter() |
60 | 61 |
|
61 | 62 | let actualScope = null |
62 | 63 | let expectedScope = null |
63 | | - linter.defineRule("test", (context) => ({ |
64 | | - Program(node) { |
65 | | - const scope = getScope(context, node) |
66 | | - actualScope = getInnermostScope(scope, selectNode(node)) |
67 | | - expectedScope = selectScope(scope) |
68 | | - }, |
69 | | - })) |
70 | 64 | linter.verify(code, { |
71 | | - parserOptions: { ecmaVersion: 2020, ...parserOptions }, |
72 | | - rules: { test: "error" }, |
| 65 | + languageOptions: { |
| 66 | + ecmaVersion: 2020, |
| 67 | + sourceType: "script", |
| 68 | + ...languageOptions, |
| 69 | + }, |
| 70 | + rules: { "test/test": "error" }, |
| 71 | + plugins: { |
| 72 | + test: { |
| 73 | + rules: { |
| 74 | + test: { |
| 75 | + create(context) { |
| 76 | + return { |
| 77 | + Program(node) { |
| 78 | + const scope = getScope( |
| 79 | + context, |
| 80 | + node, |
| 81 | + ) |
| 82 | + actualScope = getInnermostScope( |
| 83 | + scope, |
| 84 | + selectNode(node), |
| 85 | + ) |
| 86 | + expectedScope = selectScope(scope) |
| 87 | + }, |
| 88 | + } |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
73 | 94 | }) |
74 | 95 |
|
75 | 96 | assert.notStrictEqual(expectedScope, null) |
|
0 commit comments