Skip to content

Commit

Permalink
[New] add eslint 9 support
Browse files Browse the repository at this point in the history
This change adds support for eslint v9. All three example projects have been updated to use the latest eslint, and the root package's dev and peer deps have been updated.

In order to make this work with both v9 and older versions, I had to update the parser options helper to adjust the config passed into the `RuleTester`.
  • Loading branch information
michaelfaith authored and ljharb committed Jul 18, 2024
1 parent 74d5dec commit deac4fd
Show file tree
Hide file tree
Showing 16 changed files with 180 additions and 50 deletions.
1 change: 1 addition & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<PROJECT_ROOT>/lib/.*
<PROJECT_ROOT>/docs/.*
<PROJECT_ROOT>/reports/.*
<PROJECT_ROOT>/examples/.*
[options]
suppress_type=$FlowFixMe
29 changes: 28 additions & 1 deletion .github/workflows/node-4+.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,40 @@ jobs:
matrix:
node-version: ${{ fromJson(needs.matrix.outputs.latest) }}
eslint:
- 9
- 8
- 7
- 6
- 5
- 4
- 3
exclude:
- node-version: 16
eslint: 9
- node-version: 15
eslint: 9
- node-version: 14
eslint: 9
- node-version: 13
eslint: 9
- node-version: 12
eslint: 9
- node-version: 11
eslint: 9
- node-version: 10
eslint: 9
- node-version: 9
eslint: 9
- node-version: 8
eslint: 9
- node-version: 7
eslint: 9
- node-version: 6
eslint: 9
- node-version: 5
eslint: 9
- node-version: 4
eslint: 9
- node-version: 15
eslint: 8
- node-version: 13
Expand Down Expand Up @@ -90,7 +117,7 @@ jobs:
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-depth: 0
- uses: ljharb/actions/node/install@main
name: 'nvm install ${{ matrix.node-version }} && npm install'
env:
Expand Down
3 changes: 3 additions & 0 deletions __tests__/__util__/nodeReexports/assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import assert from 'assert';

export default assert;
3 changes: 3 additions & 0 deletions __tests__/__util__/nodeReexports/fs-promises.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import fs from 'fs/promises';

export default fs;
3 changes: 3 additions & 0 deletions __tests__/__util__/nodeReexports/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import fs from 'fs';

export default fs;
3 changes: 3 additions & 0 deletions __tests__/__util__/nodeReexports/path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import path from 'path';

export default path;
3 changes: 3 additions & 0 deletions __tests__/__util__/nodeReexports/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import url from 'url';

export default url;
3 changes: 3 additions & 0 deletions __tests__/__util__/nodeReexports/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import util from 'util';

export default util;
53 changes: 40 additions & 13 deletions __tests__/__util__/parserOptionsMapper.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
import { version as eslintVersion } from 'eslint/package.json';
import semver from 'semver';

const usingLegacy = semver.major(eslintVersion) < 9;

const defaultParserOptions = {
ecmaVersion: 2018,
ecmaFeatures: {
experimentalObjectRestSpread: true,
jsx: true,
},
};

const defaultLegacyParserOptions = {
...defaultParserOptions,
ecmaVersion: 2018,
};

const defaultLanguageOptions = {
ecmaVersion: 'latest',
parserOptions: {
...defaultParserOptions,
},
};

export default function parserOptionsMapper({
code,
errors,
options = [],
parserOptions = {},
settings,
languageOptions = {},
settings = {},
}) {
return {
code,
errors,
options,
parserOptions: {
...defaultParserOptions,
...parserOptions,
},
settings,
};
return usingLegacy
? {
code,
errors,
options,
parserOptions: {
...defaultLegacyParserOptions,
...languageOptions,
},
settings,
}
: {
code,
errors,
options,
languageOptions: {
...defaultLanguageOptions,
...languageOptions,
},
settings,
};
}
4 changes: 2 additions & 2 deletions __tests__/src/rules/img-redundant-alt-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ ruleTester.run('img-redundant-alt', rule, {
{ code: '<img alt={imageAlt} />' },
{ code: '<img alt={imageAlt.name} />' },
semver.satisfies(eslintVersion, '>= 6') ? [
{ code: '<img alt={imageAlt?.name} />', parserOptions: { ecmaVersion: 2020 } },
{ code: '<img alt="Doing cool things" aria-hidden={foo?.bar}/>', parserOptions: { ecmaVersion: 2020 } },
{ code: '<img alt={imageAlt?.name} />', languageOptions: { ecmaVersion: 2020 } },
{ code: '<img alt="Doing cool things" aria-hidden={foo?.bar}/>', languageOptions: { ecmaVersion: 2020 } },
] : [],
{ code: '<img alt="Photography" />;' },
{ code: '<img alt="ImageMagick" />;' },
Expand Down
88 changes: 65 additions & 23 deletions __tests__/src/util/parserOptionsMapper-test.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,88 @@
import { version as eslintVersion } from 'eslint/package.json';
import expect from 'expect';
import semver from 'semver';
import parserOptionsMapper from '../../__util__/parserOptionsMapper';

const usingLegacy = semver.major(eslintVersion) < 9;

describe('parserOptionsMapper', () => {
it('should return an test case object', () => {
const testCase = {
code: '<div />',
errors: [],
options: {},
};
expect(parserOptionsMapper(testCase)).toEqual({
code: '<div />',
errors: [],
options: {},
parserOptions: {
ecmaVersion: 2018,
ecmaFeatures: {
experimentalObjectRestSpread: true,
jsx: true,

const expectedResult = usingLegacy
? {
code: '<div />',
errors: [],
options: {},
parserOptions: {
ecmaVersion: 2018,
ecmaFeatures: {
experimentalObjectRestSpread: true,
jsx: true,
},
},
},
});
settings: {},
}
: {
code: '<div />',
errors: [],
options: {},
languageOptions: {
ecmaVersion: 'latest',
parserOptions: {
ecmaFeatures: {
experimentalObjectRestSpread: true,
jsx: true,
},
},
},
settings: {},
};
expect(parserOptionsMapper(testCase)).toEqual(expectedResult);
});
it('should allow for overriding parserOptions', () => {
const testCase = {
code: '<div />',
errors: [],
options: {},
parserOptions: {
languageOptions: {
ecmaVersion: 5,
},
};
expect(parserOptionsMapper(testCase)).toEqual({
code: '<div />',
errors: [],
options: {},
parserOptions: {
ecmaVersion: 5,
ecmaFeatures: {
experimentalObjectRestSpread: true,
jsx: true,

const expectedResult = usingLegacy
? {
code: '<div />',
errors: [],
options: {},
parserOptions: {
ecmaVersion: 5,
ecmaFeatures: {
experimentalObjectRestSpread: true,
jsx: true,
},
},
},
});
settings: {},
}
: {
code: '<div />',
errors: [],
options: {},
languageOptions: {
ecmaVersion: 5,
parserOptions: {
ecmaFeatures: {
experimentalObjectRestSpread: true,
jsx: true,
},
},
},
settings: {},
};
expect(parserOptionsMapper(testCase)).toEqual(expectedResult);
});
});
4 changes: 2 additions & 2 deletions examples/flat-cjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"@eslint/js": "^9.5.0",
"@eslint/js": "^9.9.1",
"cross-env": "^7.0.3",
"eslint": "^8.57.0",
"eslint": "^9.9.1",
"eslint-plugin-jsx-a11y": "file:../..",
"globals": "^15.6.0"
}
Expand Down
4 changes: 2 additions & 2 deletions examples/flat-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"@eslint/js": "^9.5.0",
"@eslint/js": "^9.9.1",
"cross-env": "^7.0.3",
"eslint": "^8.57.0",
"eslint": "^9.9.1",
"eslint-plugin-jsx-a11y": "file:../..",
"globals": "^15.6.0"
}
Expand Down
2 changes: 1 addition & 1 deletion examples/legacy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"devDependencies": {
"cross-env": "^7.0.3",
"eslint": "^8.57.0",
"eslint": "^9.9.1",
"eslint-plugin-jsx-a11y": "file:../.."
}
}
25 changes: 19 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"create": "node ./scripts/create-rule",
"flow": "flow",
"lint:fix": "npm run lint -- --fix",
"lint": "eslint --ext=js,mjs,cjs,ts,tsx .",
"lint": "npx eslint@8 --ext=js,mjs,cjs,ts,tsx .",
"prepublish": "not-in-publish || npm run prepublishOnly",
"prepublishOnly": "safe-publish-latest && npm run lint && npm run flow && npm run jest",
"pretest": "npm run lint:fix && npm run flow",
Expand All @@ -34,7 +34,7 @@
"test-example:legacy": "cd examples/legacy && npm install && npm run lint",
"test-example:flat-esm": "cd examples/flat-esm && npm install && npm run lint",
"test-example:flat-cjs": "cd examples/flat-cjs && npm install && npm run lint",
"jest": "jest --coverage __tests__/**/*",
"jest": "jest --coverage __tests__",
"pregenerate-list-of-rules": "npm run build",
"generate-list-of-rules": "eslint-doc-generator --rule-doc-title-format prefix-name --rule-doc-section-options false --config-emoji recommended,☑️ --ignore-config flat/recommended --ignore-config flat/strict",
"generate-list-of-rules:check": "npm run generate-list-of-rules -- --check",
Expand All @@ -51,7 +51,8 @@
"babel-jest": "^24.9.0",
"babel-plugin-add-module-exports": "^1.0.4",
"babel-preset-airbnb": "^5.0.0",
"eslint": "^3 || ^4 || ^5 || ^6 || ^7 || ^8",
"core-js": "^3.38.1",
"eslint": "^3 || ^4 || ^5 || ^6 || ^7 || ^8 || ^9",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-doc-generator": "^1.7.1",
"eslint-plugin-eslint-plugin": "^4.3.0",
Expand Down Expand Up @@ -96,7 +97,7 @@
"string.prototype.includes": "^2.0.0"
},
"peerDependencies": {
"eslint": "^3 || ^4 || ^5 || ^6 || ^7 || ^8"
"eslint": "^3 || ^4 || ^5 || ^6 || ^7 || ^8 || ^9"
},
"jest": {
"coverageReporters": [
Expand All @@ -111,7 +112,18 @@
"testPathIgnorePatterns": [
"__tests__/__util__/"
],
"testEnvironment": "node"
"testEnvironment": "node",
"moduleNameMapper": {
"@eslint/config-array": "<rootDir>/node_modules/@eslint/config-array/dist/cjs/index.cjs",
"@eslint/object-schema": "<rootDir>/node_modules/@eslint/object-schema/dist/cjs/index.cjs",
"node:assert": "<rootDir>/__tests__/__util__/nodeReexports/assert.js",
"node:fs/promises": "<rootDir>/__tests__/__util__/nodeReexports/fs-promises.js",
"node:fs": "<rootDir>/__tests__/__util__/nodeReexports/fs.js",
"node:path": "<rootDir>/__tests__/__util__/nodeReexports/path.js",
"node:url": "<rootDir>/__tests__/__util__/nodeReexports/url.js",
"node:util": "<rootDir>/__tests__/__util__/nodeReexports/util.js"
},
"setupFilesAfterEnv": ["<rootDir>/setup.jest.js"]
},
"auto-changelog": {
"output": "CHANGELOG.md",
Expand All @@ -132,7 +144,8 @@
"/flow",
"scripts/",
"CONTRIBUTING.md",
"/examples"
"/examples",
"setup.jest.js"
]
}
}
2 changes: 2 additions & 0 deletions setup.jest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import 'core-js/stable/structured-clone';

0 comments on commit deac4fd

Please sign in to comment.