Skip to content

Commit

Permalink
feat: support ESLint 8.x
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDeBoey committed Oct 11, 2021
1 parent 7e4c01b commit 2d740b7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 25 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/node.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
matrix:
node-version: ${{ fromJson(needs.matrix.outputs.majors) }}
eslint:
- 8
- 7
- 6
- 5
Expand Down Expand Up @@ -52,6 +53,15 @@ jobs:
eslint: 4
- node-version: 4
eslint: 3
exclude:
- node-version: 15
eslint: 8
- node-version: 13
eslint: 8
- node-version: 11
eslint: 8
- node-version: 10
eslint: 8

steps:
- uses: actions/checkout@v2
Expand All @@ -67,6 +77,8 @@ jobs:
if: ${{ !matrix.ajv && matrix.eslint == 4 }}
- run: npm install --no-save "ajv@6"
if: ${{ !matrix.ajv && matrix.eslint == 5 }}
- run: npm install --no-save @eslint/eslintrc@0
if: ${{ matrix.eslint != 8 }}
- run: npm prune > /dev/null
- run: npm ls > /dev/null
- run: npm uninstall --no-save @eslint/eslintrc
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
],
"license": "MIT",
"dependencies": {
"@eslint/eslintrc": "^0.1.3",
"@eslint/eslintrc": "^0.4.3 || ^1.0.2",
"cliui": "^3.2.0",
"eslint-rule-documentation": "^1.0.23",
"glob": "^7.1.6",
Expand All @@ -50,8 +50,8 @@
"codecov": "^2.3.1",
"commitizen": "^2.10.1",
"cz-conventional-changelog": "^2.1.0",
"eslint": "^3.12.0 || ^4 || ^5 || ^6 || ^7",
"eslint-plugin-json": "^2.1.2",
"eslint": "^3.12.0 || ^4 || ^5 || ^6 || ^7 || ^8",
"eslint-plugin-json": "^3.1.0",
"ghooks": "^2.0.4",
"in-publish": "^2.0.1",
"mocha": "^3.5.3",
Expand All @@ -65,7 +65,7 @@
"validate-commit-msg": "^2.14.0"
},
"peerDependencies": {
"eslint": "^3.12.0 || ^4 || ^5 || ^6 || ^7"
"eslint": "^3.12.0 || ^4 || ^5 || ^6 || ^7 || ^8"
},
"nyc": {
"exclude": [
Expand Down
24 changes: 19 additions & 5 deletions src/lib/rule-finder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path');

const eslint = require('eslint');
const { CLIEngine, ESLint, Linter } = require('eslint');
const glob = require('glob');
const isAbsolute = require('path-is-absolute');
const difference = require('./array-diff');
Expand All @@ -19,15 +19,29 @@ function _getConfigFile(specifiedFile) {
}

function _getConfigs(configFile, files) {
const cliEngine = new eslint.CLIEngine({
if (ESLint) { // ESLint 7+
const cliEngine = new ESLint({
// Ignore any config applicable depending on the location on the filesystem
useEslintrc: false,
// Point to the particular config
overrideConfigFile: configFile
});

return new Set(files
.map(filePath => cliEngine.isPathIgnored(filePath) ? false : cliEngine.calculateConfigForFile(filePath))
.filter(Boolean));
}

const cliEngine = new CLIEngine({
// Ignore any config applicable depending on the location on the filesystem
useEslintrc: false,
// Point to the particular config
configFile
});

return new Set(files
.map(filePath => cliEngine.isPathIgnored(filePath) ? false : cliEngine.getConfigForFile(filePath))
.filter(Boolean));
.map(filePath => cliEngine.isPathIgnored(filePath) ? false : cliEngine.getConfigForFile(filePath))
.filter(Boolean));
}

function _getConfig(configFile, files) {
Expand Down Expand Up @@ -67,7 +81,7 @@ function _getPluginRules(config) {
}

function _getCoreRules() {
return eslint.linter.getRules();
return new Linter().getRules();
}

function _filterRuleNames(ruleNames, rules, predicate) {
Expand Down
34 changes: 18 additions & 16 deletions test/lib/rule-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ const proxyquire = require('proxyquire');

let ModuleResolver;
try {
// eslint 7.12+
ModuleResolver = require('@eslint/eslintrc/lib/shared/relative-module-resolver');
// ESLint 7.12+
ModuleResolver = require('@eslint/eslintrc').Legacy.ModuleResolver;
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') {
throw err;
}

try {
// eslint v6 - v7.11: load the actual module
// ESLint v6 - v7.11: load the actual module
ModuleResolver = require('eslint/lib/shared/relative-module-resolver');
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') {
throw err;
}
// eslint < 6: ModuleResolver is `undefined`, which is okay. The proxyquire

// ESLint < 6: ModuleResolver is `undefined`, which is okay. The proxyquire
// override for ../shared/relative-module-resolver won't be used because
// eslint < 6 does not have that module and so does not try to load it.
// ESLint < 6 does not have that module and so does not try to load it.
ModuleResolver = undefined;
}
}
Expand Down Expand Up @@ -54,27 +56,27 @@ const moduleResolver = {

const getRuleFinder = proxyquire('../../src/lib/rule-finder', {
eslint: {
linter: {
Linter: () => ({
getRules() {
return new Map()
.set('foo-rule', {})
.set('old-rule', {meta: {deprecated: true}})
.set('bar-rule', {})
.set('baz-rule', {});
}
}
})
},
//
// This following module override is needed for eslint v6 and over. The module
// path that we pass here is literally the one used in eslint (specifically in
// This following module override is needed for ESLint v6 and over. The module
// path that we pass here is literally the one used in ESLint (specifically in
// eslint/lib/cli-engine/config-array-factory.js)
//
// The stock `resolve` method attempts to resolve to a file path the module
// name passed in `name` relative to the path in `relative`. We have to
// override that function, otherwise eslint fails to "load" our plugins.
// override that function, otherwise ESLint fails to "load" our plugins.
//
'../shared/relative-module-resolver': moduleResolver, // in eslint < 7.12, from eslint/lib/cli-engine/config-array-factory.js
'./shared/relative-module-resolver': moduleResolver, // in eslint 7.12+, from @eslint/eslintrc/lib/config-array-factory.js
'../shared/relative-module-resolver': moduleResolver, // in ESLint < 7.12, from eslint/lib/cli-engine/config-array-factory.js
'./shared/relative-module-resolver': moduleResolver, // in ESLint 7.12+, from @eslint/eslintrc/lib/config-array-factory.js
'eslint-plugin-plugin': {
rules: {
'foo-rule': {},
Expand Down Expand Up @@ -156,19 +158,19 @@ const dedupeModuleResolver = {
};
const getRuleFinderForDedupeTests = proxyquire('../../src/lib/rule-finder', {
eslint: {
linter: {
Linter: () => ({
getRules() {
return new Map()
.set('foo-rule', {})
.set('bar-rule', {})
.set('plugin/duplicate-foo-rule', {})
.set('plugin/duplicate-bar-rule', {});
}
}
})
},
// See the long comment in `getRuleFinder` above to learn what the point of this override is.
'../shared/relative-module-resolver': dedupeModuleResolver, // in eslint < 7.12, from eslint/lib/cli-engine/config-array-factory.js
'./shared/relative-module-resolver': dedupeModuleResolver, // in eslint 7.12+, from @eslint/eslintrc/lib/config-array-factory.js
'../shared/relative-module-resolver': dedupeModuleResolver, // in ESLint < 7.12, from eslint/lib/cli-engine/config-array-factory.js
'./shared/relative-module-resolver': dedupeModuleResolver, // in ESLint 7.12+, from @eslint/eslintrc/lib/config-array-factory.js
'eslint-plugin-plugin': {
rules: {
'duplicate-foo-rule': {},
Expand Down

0 comments on commit 2d740b7

Please sign in to comment.