Skip to content

Commit d66cde0

Browse files
G-Rathmichaelfaith
authored andcommitted
[New] support eslint v9
Co-authored-by: Gareth Jones <jones258@gmail.com> Co-authored-by: michael faith <michaelfaith@users.noreply.github.com>
1 parent d27a639 commit d66cde0

File tree

10 files changed

+152
-32
lines changed

10 files changed

+152
-32
lines changed

.github/workflows/node-4+.yml

+27
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ jobs:
3636
- macos-latest
3737
node-version: ${{ fromJson(needs.matrix.outputs.latest) }}
3838
eslint:
39+
- 9
3940
- 8
4041
- 7
4142
- 6
@@ -63,34 +64,58 @@ jobs:
6364
env:
6465
TS_PARSER: 2
6566
exclude:
67+
- node-version: 16
68+
eslint: 9
69+
- node-version: 15
70+
eslint: 9
6671
- node-version: 15
6772
eslint: 8
73+
- node-version: 14
74+
eslint: 9
75+
- node-version: 13
76+
eslint: 9
6877
- node-version: 13
6978
eslint: 8
79+
- node-version: 12
80+
eslint: 9
81+
- node-version: 11
82+
eslint: 9
7083
- node-version: 11
7184
eslint: 8
85+
- node-version: 10
86+
eslint: 9
7287
- node-version: 10
7388
eslint: 8
89+
- node-version: 9
90+
eslint: 9
7491
- node-version: 9
7592
eslint: 8
7693
- node-version: 9
7794
eslint: 7
95+
- node-version: 8
96+
eslint: 9
7897
- node-version: 8
7998
eslint: 8
8099
- node-version: 8
81100
eslint: 7
101+
- node-version: 7
102+
eslint: 9
82103
- node-version: 7
83104
eslint: 8
84105
- node-version: 7
85106
eslint: 7
86107
- node-version: 7
87108
eslint: 6
109+
- node-version: 6
110+
eslint: 9
88111
- node-version: 6
89112
eslint: 8
90113
- node-version: 6
91114
eslint: 7
92115
- node-version: 6
93116
eslint: 6
117+
- node-version: 5
118+
eslint: 9
94119
- node-version: 5
95120
eslint: 8
96121
- node-version: 5
@@ -99,6 +124,8 @@ jobs:
99124
eslint: 6
100125
- node-version: 5
101126
eslint: 5
127+
- node-version: 4
128+
eslint: 9
102129
- node-version: 4
103130
eslint: 8
104131
- node-version: 4

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
77
## [Unreleased]
88

99
### Added
10+
- support eslint v9 ([#2996], thanks [@G-Rath] [@michaelfaith])
1011
- [`order`]: allow validating named imports ([#3043], thanks [@manuth])
1112

1213
### Fixed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"chai": "^4.3.10",
7777
"cross-env": "^4.0.0",
7878
"escope": "^3.6.0",
79-
"eslint": "^2 || ^3 || ^4 || ^5 || ^6 || ^7.2.0 || ^8",
79+
"eslint": "^2 || ^3 || ^4 || ^5 || ^6 || ^7.2.0 || ^8 || ^9",
8080
"eslint-doc-generator": "^1.6.1",
8181
"eslint-import-resolver-node": "file:./resolvers/node",
8282
"eslint-import-resolver-typescript": "^1.0.2 || ^1.1.1",
@@ -106,7 +106,7 @@
106106
"typescript-eslint-parser": "^15 || ^20 || ^22"
107107
},
108108
"peerDependencies": {
109-
"eslint": "^2 || ^3 || ^4 || ^5 || ^6 || ^7.2.0 || ^8"
109+
"eslint": "^2 || ^3 || ^4 || ^5 || ^6 || ^7.2.0 || ^8 || ^9"
110110
},
111111
"dependencies": {
112112
"@rtsao/scc": "^1.1.0",

tests/files/issue210.config.flat.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exports.languageOptions = {
2+
sourceType: 'module',
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var jsonPlugin = require('eslint-plugin-json');
2+
3+
if (!jsonPlugin.processors.json) {
4+
jsonPlugin.processors.json = jsonPlugin.processors['.json'];
5+
}
6+
7+
module.exports = [
8+
{
9+
files: ['tests/files/just-json-files/*.json'],
10+
plugins:{
11+
json: jsonPlugin,
12+
},
13+
processor: 'json/json',
14+
rules: Object.assign(
15+
{},
16+
{
17+
'import/no-unused-modules': [
18+
'error',
19+
{
20+
'missingExports': false,
21+
'unusedExports': true,
22+
},
23+
],
24+
},
25+
jsonPlugin.configs.recommended.rules
26+
)
27+
},
28+
];

tests/src/cli.js

+36-17
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,29 @@ describe('CLI regression tests', function () {
1515
let cli;
1616
before(function () {
1717
if (ESLint) {
18-
eslint = new ESLint({
19-
useEslintrc: false,
20-
overrideConfigFile: './tests/files/issue210.config.js',
21-
rulePaths: ['./src/rules'],
22-
overrideConfig: {
23-
rules: {
24-
named: 2,
18+
if (semver.satisfies(eslintPkg.version, '>= 9')) {
19+
eslint = new ESLint({
20+
overrideConfigFile: './tests/files/issue210.config.flat.js',
21+
overrideConfig: {
22+
rules: {
23+
'import/named': 2,
24+
},
2525
},
26-
},
27-
plugins: { 'eslint-plugin-import': importPlugin },
28-
});
26+
plugins: { 'eslint-plugin-import': importPlugin },
27+
});
28+
} else {
29+
eslint = new ESLint({
30+
useEslintrc: false,
31+
overrideConfigFile: './tests/files/issue210.config.js',
32+
rulePaths: ['./src/rules'],
33+
overrideConfig: {
34+
rules: {
35+
named: 2,
36+
},
37+
},
38+
plugins: { 'eslint-plugin-import': importPlugin },
39+
});
40+
}
2941
} else {
3042
cli = new CLIEngine({
3143
useEslintrc: false,
@@ -56,13 +68,20 @@ describe('CLI regression tests', function () {
5668
this.skip();
5769
} else {
5870
if (ESLint) {
59-
eslint = new ESLint({
60-
useEslintrc: false,
61-
overrideConfigFile: './tests/files/just-json-files/.eslintrc.json',
62-
rulePaths: ['./src/rules'],
63-
ignore: false,
64-
plugins: { 'eslint-plugin-import': importPlugin },
65-
});
71+
if (semver.satisfies(eslintPkg.version, '>= 9')) {
72+
eslint = new ESLint({
73+
overrideConfigFile: './tests/files/just-json-files/eslint.config.js',
74+
plugins: { 'eslint-plugin-import': importPlugin },
75+
});
76+
} else {
77+
eslint = new ESLint({
78+
useEslintrc: false,
79+
overrideConfigFile: './tests/files/just-json-files/.eslintrc.json',
80+
rulePaths: ['./src/rules'],
81+
ignore: false,
82+
plugins: { 'eslint-plugin-import': importPlugin },
83+
});
84+
}
6685
} else {
6786
cli = new CLIEngine({
6887
useEslintrc: false,

tests/src/rule-tester.js

+44-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,47 @@
1+
import { RuleTester } from 'eslint';
2+
import { version as eslintVersion } from 'eslint/package.json';
3+
import semver from 'semver';
4+
5+
export const usingFlatConfig = semver.major(eslintVersion) >= 9;
6+
17
export function withoutAutofixOutput(test) {
2-
return { ...test, output: test.code };
8+
return { ...test, ...usingFlatConfig || { output: test.code } };
9+
}
10+
11+
class FlatCompatRuleTester extends RuleTester {
12+
constructor(testerConfig = { parserOptions: { sourceType: 'script' } }) {
13+
super(FlatCompatRuleTester._flatCompat(testerConfig));
14+
}
15+
16+
run(ruleName, rule, tests) {
17+
super.run(ruleName, rule, {
18+
valid: tests.valid.map((t) => FlatCompatRuleTester._flatCompat(t)),
19+
invalid: tests.invalid.map((t) => FlatCompatRuleTester._flatCompat(t)),
20+
});
21+
}
22+
23+
static _flatCompat(config) {
24+
if (!config || !usingFlatConfig || typeof config !== 'object') {
25+
return config;
26+
}
27+
28+
const { parser, parserOptions = {}, languageOptions = {}, ...remainingConfig } = config;
29+
const { ecmaVersion, sourceType, ...remainingParserOptions } = parserOptions;
30+
const parserObj = typeof parser === 'string' ? require(parser) : parser;
31+
32+
return {
33+
...remainingConfig,
34+
languageOptions: {
35+
...languageOptions,
36+
...parserObj ? { parser: parserObj } : {},
37+
...ecmaVersion ? { ecmaVersion } : {},
38+
...sourceType ? { sourceType } : {},
39+
parserOptions: {
40+
...remainingParserOptions,
41+
},
42+
},
43+
};
44+
}
345
}
446

5-
export { RuleTester } from 'eslint';
47+
export { FlatCompatRuleTester as RuleTester };

tests/src/rules/named.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test, SYNTAX_CASES, getTSParsers, testFilePath, testVersion, parsers } from '../utils';
2-
import { RuleTester } from '../rule-tester';
2+
import { RuleTester, usingFlatConfig } from '../rule-tester';
33
import path from 'path';
44

55
import { CASE_SENSITIVE_FS } from 'eslint-module-utils/resolve';
@@ -32,7 +32,7 @@ ruleTester.run('named', rule, {
3232
settings: { 'import/resolve': { extensions: ['.js', '.jsx'] } } }),
3333

3434
// validate that eslint-disable-line silences this properly
35-
test({ code: 'import {a, b, d} from "./common"; // eslint-disable-line named' }),
35+
test({ code: `import {a, b, d} from "./common"; // eslint-disable-line ${usingFlatConfig ? 'rule-to-test/' : ''}named` }),
3636

3737
test({ code: 'import { foo, bar } from "./re-export-names"' }),
3838

tests/src/rules/namespace.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { test, SYNTAX_CASES, getTSParsers, testVersion, testFilePath, parsers }
22
import { RuleTester } from '../rule-tester';
33
import flatMap from 'array.prototype.flatmap';
44

5-
const ruleTester = new RuleTester({ env: { es6: true } });
5+
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
66
const rule = require('rules/namespace');
77

88
function error(name, namespace) {

tests/src/rules/no-unused-modules.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ describe('dynamic imports', function () {
288288

289289
// test for unused exports with `import()`
290290
ruleTester.run('no-unused-modules', rule, {
291-
valid: [
292-
test({
291+
valid: [].concat(
292+
testVersion('< 9', () => ({
293293
options: unusedExportsOptions,
294294
code: `
295295
export const a = 10
@@ -300,10 +300,10 @@ describe('dynamic imports', function () {
300300
`,
301301
parser: parsers.BABEL_OLD,
302302
filename: testFilePath('./no-unused-modules/exports-for-dynamic-js.js'),
303-
}),
304-
],
305-
invalid: [
306-
test({
303+
})),
304+
),
305+
invalid: [].concat(
306+
testVersion('< 9', () => ({
307307
options: unusedExportsOptions,
308308
code: `
309309
export const a = 10
@@ -319,8 +319,8 @@ describe('dynamic imports', function () {
319319
error(`exported declaration 'b' not used within other modules`),
320320
error(`exported declaration 'c' not used within other modules`),
321321
error(`exported declaration 'default' not used within other modules`),
322-
] }),
323-
],
322+
] })),
323+
),
324324
});
325325
typescriptRuleTester.run('no-unused-modules', rule, {
326326
valid: [

0 commit comments

Comments
 (0)