Skip to content

Commit cefc12c

Browse files
authored
test: add test for ESLint v9 (#186)
1 parent 4f3e738 commit cefc12c

14 files changed

+491
-233
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
object: "context",
1313
property: "getScope",
1414
message:
15-
"If you are using it in a test case, use test/test-lib/get-scope.mjs instead. Other than that, the API should also be compatible with ESLint v9.",
15+
"If you are using it in a test case, use test/test-lib/eslint-compat.mjs#getScope instead. Other than that, the API should also be compatible with ESLint v9.",
1616
},
1717
],
1818
},

.github/workflows/ci.yml

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,33 @@ jobs:
4848
matrix.os }})
4949
strategy:
5050
matrix:
51-
eslint: [8]
52-
node: [12.22.0, 12, 14.17.0, 14, 16, 18, 20, 22]
51+
eslint: [9]
52+
node: [18, 20, 22]
5353
os: [ubuntu-latest]
5454
include:
5555
# On other platforms
5656
- os: windows-latest
57-
eslint: 8
58-
node: 20
57+
eslint: 9
58+
node: 22
5959
- os: macos-latest
60-
eslint: 8
61-
node: 20
60+
eslint: 9
61+
node: 22
62+
# On old Node versions & ESLint v8
63+
- eslint: 8
64+
node: 12.22.0
65+
os: ubuntu-latest
66+
- eslint: 8
67+
node: 12
68+
os: ubuntu-latest
69+
- eslint: 8
70+
node: 14.17.0
71+
os: ubuntu-latest
72+
- eslint: 8
73+
node: 14
74+
os: ubuntu-latest
75+
- eslint: 8
76+
node: 16
77+
os: ubuntu-latest
6278
# On old ESLint versions
6379
- eslint: 7
6480
node: 20

test/find-variable.mjs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
import assert from "assert"
2-
import eslint from "eslint"
32
import { findVariable } from "../src/index.mjs"
4-
import { getScope } from "./test-lib/get-scope.mjs"
3+
import { getScope, newCompatLinter } from "./test-lib/eslint-compat.mjs"
54

65
describe("The 'findVariable' function", () => {
76
function getVariable(code, selector, withString = null) {
8-
const linter = new eslint.Linter()
7+
const linter = newCompatLinter()
98
let variable = null
109

11-
linter.defineRule("test", (context) => ({
12-
[selector](node) {
13-
variable = findVariable(
14-
getScope(context, node),
15-
withString || node,
16-
)
17-
},
18-
}))
1910
linter.verify(code, {
20-
parserOptions: { ecmaVersion: 2020 },
21-
rules: { test: "error" },
11+
languageOptions: { ecmaVersion: 2020 },
12+
rules: { "test/test": "error" },
13+
plugins: {
14+
test: {
15+
rules: {
16+
test: {
17+
create(context) {
18+
return {
19+
[selector](node) {
20+
variable = findVariable(
21+
getScope(context, node),
22+
withString || node,
23+
)
24+
},
25+
}
26+
},
27+
},
28+
},
29+
},
30+
},
2231
})
2332

2433
return variable

test/get-function-head-location.mjs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import assert from "assert"
22
import eslint from "eslint"
33
import semver from "semver"
44
import { getFunctionHeadLocation } from "../src/index.mjs"
5+
import { newCompatLinter } from "./test-lib/eslint-compat.mjs"
56

67
describe("The 'getFunctionHeadLocation' function", () => {
78
const expectedResults = {
@@ -103,26 +104,37 @@ describe("The 'getFunctionHeadLocation' function", () => {
103104
it(`should return "${JSON.stringify(
104105
expectedLoc,
105106
)}" for "${key}".`, () => {
106-
const linter = new eslint.Linter()
107+
const linter = newCompatLinter()
107108

108109
let actualLoc = null
109-
linter.defineRule("test", (context) => ({
110-
":function"(node) {
111-
actualLoc = getFunctionHeadLocation(
112-
node,
113-
context.getSourceCode(),
114-
)
115-
},
116-
}))
117110
const messages = linter.verify(
118111
key,
119112
{
120-
rules: { test: "error" },
121-
parserOptions: {
113+
rules: { "test/test": "error" },
114+
languageOptions: {
122115
ecmaVersion: semver.gte(eslint.Linter.version, "8.0.0")
123116
? 2022
124117
: 2020,
125118
},
119+
plugins: {
120+
test: {
121+
rules: {
122+
test: {
123+
create(context) {
124+
return {
125+
":function"(node) {
126+
actualLoc =
127+
getFunctionHeadLocation(
128+
node,
129+
context.getSourceCode(),
130+
)
131+
},
132+
}
133+
},
134+
},
135+
},
136+
},
137+
},
126138
},
127139
"test.js",
128140
true,

test/get-function-name-with-kind.mjs

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import assert from "assert"
22
import eslint from "eslint"
33
import semver from "semver"
44
import { getFunctionNameWithKind } from "../src/index.mjs"
5+
import { newCompatLinter } from "./test-lib/eslint-compat.mjs"
56

67
describe("The 'getFunctionNameWithKind' function", () => {
78
const expectedResults = {
@@ -133,22 +134,33 @@ describe("The 'getFunctionNameWithKind' function", () => {
133134
const expectedResult2 = expectedResults[key]
134135

135136
it(`should return "${expectedResult1}" for "${key}".`, () => {
136-
const linter = new eslint.Linter()
137+
const linter = newCompatLinter()
137138

138139
let actualResult = null
139-
linter.defineRule("test", () => ({
140-
":function"(node) {
141-
actualResult = getFunctionNameWithKind(node)
142-
},
143-
}))
144140
const messages = linter.verify(key, {
145-
rules: { test: "error" },
146-
parserOptions: {
141+
rules: { "test/test": "error" },
142+
languageOptions: {
147143
ecmaVersion: semver.gte(eslint.Linter.version, "8.0.0")
148144
? 2022
149145
: 2020,
150146
sourceType: "module",
151147
},
148+
plugins: {
149+
test: {
150+
rules: {
151+
test: {
152+
create(_context) {
153+
return {
154+
":function"(node) {
155+
actualResult =
156+
getFunctionNameWithKind(node)
157+
},
158+
}
159+
},
160+
},
161+
},
162+
},
163+
},
152164
})
153165

154166
assert.strictEqual(
@@ -160,25 +172,36 @@ describe("The 'getFunctionNameWithKind' function", () => {
160172
})
161173

162174
it(`should return "${expectedResult2}" for "${key}" if sourceCode is present.`, () => {
163-
const linter = new eslint.Linter()
175+
const linter = newCompatLinter()
164176

165177
let actualResult = null
166-
linter.defineRule("test", (context) => ({
167-
":function"(node) {
168-
actualResult = getFunctionNameWithKind(
169-
node,
170-
context.getSourceCode(),
171-
)
172-
},
173-
}))
174178
const messages = linter.verify(key, {
175-
rules: { test: "error" },
176-
parserOptions: {
179+
rules: { "test/test": "error" },
180+
languageOptions: {
177181
ecmaVersion: semver.gte(eslint.Linter.version, "8.0.0")
178182
? 2022
179183
: 2020,
180184
sourceType: "module",
181185
},
186+
plugins: {
187+
test: {
188+
rules: {
189+
test: {
190+
create(context) {
191+
return {
192+
":function"(node) {
193+
actualResult =
194+
getFunctionNameWithKind(
195+
node,
196+
context.getSourceCode(),
197+
)
198+
},
199+
}
200+
},
201+
},
202+
},
203+
},
204+
},
182205
})
183206

184207
assert.strictEqual(

test/get-innermost-scope.mjs

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,96 @@
11
import assert from "assert"
2-
import eslint from "eslint"
32
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"
54

65
describe("The 'getInnermostScope' function", () => {
76
let i = 0
8-
for (const { code, parserOptions, selectNode, selectScope } of [
7+
for (const { code, languageOptions, selectNode, selectScope } of [
98
{
109
code: "let a = 0",
11-
parserOptions: {},
10+
languageOptions: {},
1211
selectNode: (node) => node,
1312
selectScope: (scope) => scope,
1413
},
1514
{
1615
code: "let a = 0",
17-
parserOptions: { ecmaFeatures: { globalReturn: true } },
16+
languageOptions: {
17+
parserOptions: { ecmaFeatures: { globalReturn: true } },
18+
},
1819
selectNode: (node) => node,
1920
selectScope: (scope) => scope.childScopes[0],
2021
},
2122
{
2223
code: "let a = 0",
23-
parserOptions: { sourceType: "module" },
24+
languageOptions: { sourceType: "module" },
2425
selectNode: (node) => node,
2526
selectScope: (scope) => scope.childScopes[0],
2627
},
2728
{
2829
code: "a; { b; { c; } d; } e;",
29-
parserOptions: {},
30+
languageOptions: {},
3031
selectNode: (node) => node.body[0],
3132
selectScope: (scope) => scope,
3233
},
3334
{
3435
code: "a; { b; { c; } d; } e;",
35-
parserOptions: {},
36+
languageOptions: {},
3637
selectNode: (node) => node.body[2],
3738
selectScope: (scope) => scope,
3839
},
3940
{
4041
code: "a; { b; { c; } d; } e;",
41-
parserOptions: {},
42+
languageOptions: {},
4243
selectNode: (node) => node.body[1].body[0],
4344
selectScope: (scope) => scope.childScopes[0],
4445
},
4546
{
4647
code: "a; { b; { c; } d; } e;",
47-
parserOptions: {},
48+
languageOptions: {},
4849
selectNode: (node) => node.body[1].body[2],
4950
selectScope: (scope) => scope.childScopes[0],
5051
},
5152
{
5253
code: "a; { b; { c; } d; } e;",
53-
parserOptions: {},
54+
languageOptions: {},
5455
selectNode: (node) => node.body[1].body[1].body[0],
5556
selectScope: (scope) => scope.childScopes[0].childScopes[0],
5657
},
5758
]) {
5859
it(`should return the innermost scope (${++i})`, () => {
59-
const linter = new eslint.Linter()
60+
const linter = newCompatLinter()
6061

6162
let actualScope = null
6263
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-
}))
7064
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+
},
7394
})
7495

7596
assert.notStrictEqual(expectedScope, null)

0 commit comments

Comments
 (0)