Skip to content

Commit 959858c

Browse files
authored
Chore: add test for eslint v9 (#2368)
1 parent 7d13ce3 commit 959858c

File tree

376 files changed

+3100
-2504
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

376 files changed

+3100
-2504
lines changed

.github/workflows/CI.yml

+13-1
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,18 @@ jobs:
2626
name: Test
2727
strategy:
2828
matrix:
29-
node: [17, 18, 20]
29+
node: [18, 20]
3030
os: [ubuntu-latest]
31+
eslint: [8]
32+
include:
33+
# On next ESLint version
34+
- eslint: ^9.0.0-0
35+
node: 20
36+
os: ubuntu-latest
37+
# On old Node version
38+
- eslint: 8
39+
node: 17
40+
os: ubuntu-latest
3141

3242
runs-on: ${{ matrix.os }}
3343
steps:
@@ -39,6 +49,8 @@ jobs:
3949
node-version: ${{ matrix.node }}
4050
- name: Install Packages
4151
run: npm install --legacy-peer-deps
52+
- name: Install ESLint v${{ matrix.eslint }}
53+
run: npm install --save-dev eslint@${{ matrix.eslint }} --legacy-peer-deps
4254
- name: Test
4355
run: npm test
4456

lib/utils/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ module.exports = {
673673
*/
674674
wrapCoreRule,
675675
wrapStylisticOrCoreRule,
676+
getCoreRule,
676677
/**
677678
* Checks whether the given value is defined.
678679
* @template T

tests/eslint-compat.js

+151-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,66 @@
11
// @ts-check
22
const eslint = require('eslint')
3+
const semver = require('semver')
4+
5+
let ESLint = eslint.ESLint
6+
let Linter = eslint.Linter
7+
let RuleTester = eslint.RuleTester
8+
if (semver.lt(eslint.Linter.version, '9.0.0-0')) {
9+
ESLint = eslint.ESLint ? getESLintClassForV8() : getESLintClassForV6()
10+
Linter = getLinterClassForV8()
11+
RuleTester = getRuleTesterClassForV8()
12+
}
313

414
module.exports = {
5-
ESLint: eslint.ESLint || getESLintClassForV6(),
6-
RuleTester: eslint.RuleTester
15+
ESLint,
16+
RuleTester,
17+
Linter
718
}
819

20+
/** @returns {typeof eslint.ESLint} */
21+
function getESLintClassForV8(BaseESLintClass = eslint.ESLint) {
22+
return class ESLintForV8 extends BaseESLintClass {
23+
static get version() {
24+
return BaseESLintClass.version
25+
}
26+
constructor(options) {
27+
super(adjustOptions(options))
28+
}
29+
}
30+
31+
// eslint-disable-next-line unicorn/consistent-function-scoping
32+
function adjustOptions(options) {
33+
const newOptions = {
34+
...options,
35+
useEslintrc: false
36+
}
37+
if (newOptions.overrideConfig) {
38+
newOptions.overrideConfig = { ...newOptions.overrideConfig }
39+
let plugins
40+
if (newOptions.overrideConfig.plugins) {
41+
plugins = newOptions.overrideConfig.plugins
42+
delete newOptions.overrideConfig.plugins
43+
}
44+
newOptions.overrideConfig = processCompatibleConfig(
45+
newOptions.overrideConfig
46+
)
47+
if (plugins) {
48+
newOptions.overrideConfig.plugins = Object.keys(plugins)
49+
newOptions.plugins = plugins
50+
}
51+
52+
// adjust
53+
delete newOptions.overrideConfig.files
54+
delete newOptions.overrideConfig.processor
55+
}
56+
return newOptions
57+
}
58+
}
959
/** @returns {typeof eslint.ESLint} */
1060
function getESLintClassForV6() {
1161
class ESLintForV6 {
1262
static get version() {
63+
// @ts-expect-error
1364
return eslint.CLIEngine.version
1465
}
1566

@@ -26,7 +77,7 @@ function getESLintClassForV6() {
2677
plugins: pluginsMap,
2778
...otherOptions
2879
} = options || {}
29-
/** @type {eslint.CLIEngine.Options} */
80+
3081
const newOptions = {
3182
fix: Boolean(fix),
3283
reportUnusedDisableDirectives: reportUnusedDisableDirectives
@@ -47,6 +98,8 @@ function getESLintClassForV6() {
4798
: undefined,
4899
...overrideConfig
49100
}
101+
102+
// @ts-expect-error
50103
this.engine = new eslint.CLIEngine(newOptions)
51104

52105
for (const [name, plugin] of Object.entries(pluginsMap || {})) {
@@ -59,12 +112,105 @@ function getESLintClassForV6() {
59112
* @returns {ReturnType<eslint.ESLint['lintText']>}
60113
*/
61114
async lintText(...params) {
62-
const result = this.engine.executeOnText(params[0], params[1].filePath)
115+
const result = this.engine.executeOnText(params[0], params[1]?.filePath)
63116
return result.results
64117
}
65118
}
66119

67120
/** @type {typeof eslint.ESLint} */
68121
const eslintClass = /** @type {any} */ (ESLintForV6)
69-
return eslintClass
122+
return getESLintClassForV8(eslintClass)
123+
}
124+
125+
/** @returns {typeof eslint.Linter} */
126+
function getLinterClassForV8() {
127+
return class LinterForV8 extends eslint.Linter {
128+
static get version() {
129+
return eslint.Linter.version
130+
}
131+
verify(code, config, option) {
132+
return super.verify(code, processCompatibleConfig(config, this), option)
133+
}
134+
}
135+
}
136+
137+
function getRuleTesterClassForV8() {
138+
return class RuleTesterForV8 extends eslint.RuleTester {
139+
constructor(options) {
140+
const defineRules = []
141+
super(
142+
processCompatibleConfig(options, {
143+
defineRule(...args) {
144+
defineRules.push(args)
145+
}
146+
})
147+
)
148+
for (const args of defineRules) {
149+
// @ts-expect-error
150+
this.linter.defineRule(...args)
151+
}
152+
}
153+
run(name, rule, tests) {
154+
super.run(name, rule, {
155+
valid: (tests.valid || []).map((test) =>
156+
typeof test === 'string' ? test : adjustOptions(test)
157+
),
158+
invalid: (tests.invalid || []).map((test) => adjustOptions(test))
159+
})
160+
}
161+
}
162+
// eslint-disable-next-line unicorn/consistent-function-scoping
163+
function adjustOptions(test) {
164+
return processCompatibleConfig(test)
165+
}
166+
}
167+
168+
function processCompatibleConfig(config, linter) {
169+
const newConfig = { ...config }
170+
if (newConfig.languageOptions) {
171+
const languageOptions = newConfig.languageOptions
172+
delete newConfig.languageOptions
173+
newConfig.parserOptions = {
174+
...newConfig.parserOptions,
175+
...languageOptions,
176+
...languageOptions.parserOptions
177+
}
178+
if (languageOptions.globals) {
179+
newConfig.globals = {
180+
...newConfig.globals,
181+
...languageOptions.globals
182+
}
183+
}
184+
if (languageOptions.parser) {
185+
newConfig.parser = getParserName(languageOptions.parser)
186+
if (!languageOptions.parserOptions?.parser) {
187+
delete newConfig.parserOptions.parser
188+
}
189+
linter?.defineParser?.(newConfig.parser, require(newConfig.parser))
190+
}
191+
}
192+
if (newConfig.plugins) {
193+
const plugins = newConfig.plugins
194+
delete newConfig.plugins
195+
for (const [pluginName, plugin] of Object.entries(plugins)) {
196+
for (const [ruleName, rule] of Object.entries(plugin.rules || {})) {
197+
linter.defineRule(`${pluginName}/${ruleName}`, rule)
198+
}
199+
}
200+
}
201+
newConfig.env = { ...newConfig.env, es6: true }
202+
return newConfig
203+
}
204+
205+
function getParserName(parser) {
206+
const name = parser.meta?.name || parser.name
207+
if (name === 'typescript-eslint/parser') {
208+
return require.resolve('@typescript-eslint/parser')
209+
} else if (
210+
name == null &&
211+
// @ts-expect-error
212+
parser === require('@typescript-eslint/parser')
213+
)
214+
return require.resolve('@typescript-eslint/parser')
215+
return require.resolve(name)
70216
}

tests/fixtures/script-indent/jsx-01.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{ "parserOptions": { "ecmaFeatures": { "jsx": true } } }-->
1+
<!--{ "languageOptions": { "parserOptions": {"ecmaFeatures": { "jsx": true }} } }-->
22
<script>
33
// Not yet supported.
44
const jsx = <

tests/fixtures/script-indent/jsx-02.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{ "parserOptions": { "ecmaFeatures": { "jsx": true } } }-->
1+
<!--{ "languageOptions": { "parserOptions": {"ecmaFeatures": { "jsx": true }} } }-->
22
<script>
33
// Not yet supported.
44
const jsx = <

tests/fixtures/script-indent/ts-abstract-accessor-property-01.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
22
<script lang="ts">
33
abstract class Foo {
44
abstract accessor

tests/fixtures/script-indent/ts-abstract-class-property-01.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
22
<script lang="ts">
33
abstract class A {
44
abstract a;

tests/fixtures/script-indent/ts-abstract-class-property-02.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
22
<script lang="ts">
33
abstract class A {
44
a

tests/fixtures/script-indent/ts-abstract-method-definition-01.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
22
<script lang="ts">
33
abstract class A {
44
abstract a ();

tests/fixtures/script-indent/ts-accessor-property-01.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
22
<script lang="ts">
33
class Foo {
44
accessor

tests/fixtures/script-indent/ts-accessor-property-02.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
22
<script lang="ts">
33
class Foo {
44
accessor

tests/fixtures/script-indent/ts-accessor-property-03.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
22
<script lang="ts">
33
class Foo {
44
declare accessor

tests/fixtures/script-indent/ts-accessor-property-04.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
22
<script lang="ts">
33
class Foo {
44
override accessor

tests/fixtures/script-indent/ts-accessor-property-05.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}, "requirements": { "@typescript-eslint/parser": ">=5.44.1-alpha.15"}}-->
22
<script lang="ts">
33
class Foo {
44
accessor

tests/fixtures/script-indent/ts-as-expression-01.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
22
<script lang="ts">
33
var foo =
44
bar as

tests/fixtures/script-indent/ts-call-expression-01.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
22
<script lang="ts">
33
foo
44
<

tests/fixtures/script-indent/ts-call-signature-declaration-01.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
22
<script lang="ts">
33
interface Foo {
44
(

tests/fixtures/script-indent/ts-call-signature-declaration-02.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
22
<script lang="ts">
33
type Foo = {
44
(

tests/fixtures/script-indent/ts-class-declaration-01.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
22
<script lang="ts">
33
class
44
Foo

tests/fixtures/script-indent/ts-class-declaration-02.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
22
<script lang="ts">
33
class
44
Foo

tests/fixtures/script-indent/ts-class-declaration-03.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
22
<script lang="ts">
33
class
44
Foo

tests/fixtures/script-indent/ts-class-declaration-04.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
22
<script lang="ts">
33
class
44
Foo

tests/fixtures/script-indent/ts-class-declaration-05.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
22
<script lang="ts">
33
class
44
Foo

tests/fixtures/script-indent/ts-class-declaration-06.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
22
<script lang="ts">
33
class
44
Foo

tests/fixtures/script-indent/ts-class-declaration-07.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
22
<script lang="ts">
33
import {Component, Prop, Vue} from "vue-property-decorator";
44

tests/fixtures/script-indent/ts-class-fields-private-methods-01.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
22
<script>
33
class Counter {
44
get

tests/fixtures/script-indent/ts-class-fields-private-properties-01.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
22
<script>
33
class Counter {
44
#x

tests/fixtures/script-indent/ts-class-property-01.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
22
<script lang="ts">
33
class A {
44
a;

tests/fixtures/script-indent/ts-class-property-02.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
22
<script lang="ts">
33
class A {
44
a

tests/fixtures/script-indent/ts-class-property-03.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
22
<script lang="ts">
33
class A {
44
a: number

tests/fixtures/script-indent/ts-conditional-type-01.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
1+
<!--{"languageOptions": {"parserOptions": {"parser":"@typescript-eslint/parser"}}}-->
22
<script lang="ts">
33
type X = A extends B
44
? C

0 commit comments

Comments
 (0)