Skip to content

Commit

Permalink
Support for ESLint v8
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Aug 16, 2021
1 parent 8be2ec2 commit 67c403b
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 67 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/node-4+.yml
Expand Up @@ -26,6 +26,7 @@ jobs:
matrix:
node-version: ${{ fromJson(needs.matrix.outputs.latest) }}
eslint:
- ^8.0.0-0
- 7
- 6
- 5
Expand All @@ -39,24 +40,44 @@ jobs:
env:
TS_PARSER: 2
exclude:
- node-version: 15
eslint: ^8.0.0-0
- node-version: 13
eslint: ^8.0.0-0
- node-version: 11
eslint: ^8.0.0-0
- node-version: 10
eslint: ^8.0.0-0
- node-version: 9
eslint: ^8.0.0-0
- node-version: 9
eslint: 7
- node-version: 8
eslint: ^8.0.0-0
- node-version: 8
eslint: 7
- node-version: 7
eslint: ^8.0.0-0
- node-version: 7
eslint: 7
- node-version: 7
eslint: 6
- node-version: 6
eslint: ^8.0.0-0
- node-version: 6
eslint: 7
- node-version: 6
eslint: 6
- node-version: 5
eslint: ^8.0.0-0
- node-version: 5
eslint: 7
- node-version: 5
eslint: 6
- node-version: 5
eslint: 5
- node-version: 4
eslint: ^8.0.0-0
- node-version: 4
eslint: 7
- node-version: 4
Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -69,9 +69,11 @@
"babel-register": "^6.26.0",
"babylon": "^6.18.0",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"coveralls": "^3.1.0",
"cross-env": "^4.0.0",
"eslint": "^2 || ^3 || ^4 || ^5 || ^6 || ^7.2.0",
"escope": "^3.6.0",
"eslint": "^2 || ^3 || ^4 || ^5 || ^6 || ^7.2.0 || ^8.0.0-0",
"eslint-import-resolver-node": "file:./resolvers/node",
"eslint-import-resolver-typescript": "^1.0.2 || ^1.1.1",
"eslint-import-resolver-webpack": "file:./resolvers/webpack",
Expand Down
16 changes: 12 additions & 4 deletions src/rules/no-unused-modules.js
Expand Up @@ -15,9 +15,17 @@ import includes from 'array-includes';

// eslint/lib/util/glob-util has been moved to eslint/lib/util/glob-utils with version 5.3
// and has been moved to eslint/lib/cli-engine/file-enumerator in version 6
// and has been moved to eslint/use-at-your-own-risk in version 8
let listFilesToProcess;
try {
const FileEnumerator = require('eslint/lib/cli-engine/file-enumerator').FileEnumerator;
// eslint/lib/cli-engine/file-enumerator has been moved to eslint/use-at-your-own-risk
// in version 8
let FileEnumerator;
try {
FileEnumerator = require('eslint/use-at-your-own-risk').FileEnumerator;
} catch (e) {
FileEnumerator = require('eslint/lib/cli-engine/file-enumerator').FileEnumerator;
}
listFilesToProcess = function (src, extensions) {
const e = new FileEnumerator({
extensions: extensions,
Expand Down Expand Up @@ -228,7 +236,7 @@ const prepareImportsAndExports = (srcFiles, context) => {
}
const localImport = imports.get(key) || new Set();
value.declarations.forEach(({ importedSpecifiers }) =>
importedSpecifiers.forEach(specifier => localImport.add(specifier))
importedSpecifiers.forEach(specifier => localImport.add(specifier)),
);
imports.set(key, localImport);
});
Expand Down Expand Up @@ -544,13 +552,13 @@ module.exports = {
if (exportStatement.whereUsed.size < 1) {
context.report(
node,
`exported declaration '${value}' not used within other modules`
`exported declaration '${value}' not used within other modules`,
);
}
} else {
context.report(
node,
`exported declaration '${value}' not used within other modules`
`exported declaration '${value}' not used within other modules`,
);
}
};
Expand Down
30 changes: 17 additions & 13 deletions tests/dep-time-travel.sh
Expand Up @@ -8,19 +8,23 @@ export NPM_CONFIG_LEGACY_PEER_DEPS=true

npm install --no-save "eslint@${ESLINT_VERSION}" --ignore-scripts

# completely remove the new TypeScript parser for ESLint < v5
if [[ "$ESLINT_VERSION" -lt "5" ]]; then
echo "Removing @typescript-eslint/parser..."
npm uninstall --no-save @typescript-eslint/parser
fi

# use these alternate TypeScript dependencies for ESLint < v4
if [[ "$ESLINT_VERSION" -lt "4" ]]; then
echo "Downgrading babel-eslint..."
npm i --no-save babel-eslint@8.0.3

echo "Downgrading TypeScript dependencies..."
npm i --no-save typescript-eslint-parser@15 typescript@2.8.1
re="^[0-9]+$"

if [[ "$ESLINT_VERSION" =~ $re ]] ; then
# completely remove the new TypeScript parser for ESLint < v5
if [[ "$ESLINT_VERSION" -lt "5" ]]; then
echo "Removing @typescript-eslint/parser..."
npm uninstall --no-save @typescript-eslint/parser
fi

# use these alternate TypeScript dependencies for ESLint < v4
if [[ "$ESLINT_VERSION" -lt "4" ]]; then
echo "Downgrading babel-eslint..."
npm i --no-save babel-eslint@8.0.3

echo "Downgrading TypeScript dependencies..."
npm i --no-save typescript-eslint-parser@15 typescript@2.8.1
fi
fi

# typescript-eslint-parser 1.1.1+ is not compatible with node 6
Expand Down
119 changes: 72 additions & 47 deletions tests/src/cli.js
Expand Up @@ -3,82 +3,107 @@
*/
import path from 'path';

import { expect } from 'chai';
import { CLIEngine } from 'eslint';
import { expect, use } from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { CLIEngine, ESLint as ESLintClass } from 'eslint';
import eslintPkg from 'eslint/package.json';
import semver from 'semver';
import * as importPlugin from '../../src/index';

use(chaiAsPromised);

const ESLint = ESLintClass || class {
constructor(options) {
options = Object.assign({},options);
const overrideConfig = options.overrideConfig;
delete options.overrideConfig;
const overrideConfigFile = options.overrideConfigFile;
delete options.overrideConfigFile;
const pluginsMap = options.plugins;
delete options.plugins;
this.engine = new CLIEngine(Object.assign(options, overrideConfig, overrideConfigFile ? { configFile:overrideConfigFile }: {}));

for (const [name, plugin] of Object.entries(pluginsMap || {})) {
this.engine.addPlugin(name, plugin);
}
}
lintFiles(params) {
const result = this.engine.executeOnFiles(params);
return Promise.resolve(result.results);
}
};

describe('CLI regression tests', function () {
describe('issue #210', function () {
let cli;
let eslint;
before(function () {
cli = new CLIEngine({
eslint = new ESLint({
useEslintrc: false,
configFile: './tests/files/issue210.config.js',
overrideConfigFile: './tests/files/issue210.config.js',
rulePaths: ['./src/rules'],
rules: {
'named': 2,
overrideConfig: {
rules: {
'named': 2,
},
},
plugins: { 'eslint-plugin-import': importPlugin },
});
});
it("doesn't throw an error on gratuitous, erroneous self-reference", function () {
expect(() => cli.executeOnFiles(['./tests/files/issue210.js'])).not.to.throw();
return expect(eslint.lintFiles(['./tests/files/issue210.js'])).not.to.rejected;
});
});

describe('issue #1645', function () {
let cli;
let eslint;
beforeEach(function () {
if (semver.satisfies(eslintPkg.version, '< 6')) {
this.skip();
} else {
cli = new CLIEngine({
eslint = new ESLint({
useEslintrc: false,
configFile: './tests/files/just-json-files/.eslintrc.json',
overrideConfigFile: './tests/files/just-json-files/.eslintrc.json',
rulePaths: ['./src/rules'],
ignore: false,
plugins: { 'eslint-plugin-import': importPlugin },
});
}
});

it('throws an error on invalid JSON', () => {
const invalidJSON = './tests/files/just-json-files/invalid.json';
const results = cli.executeOnFiles([invalidJSON]);
expect(results).to.eql({
results: [
{
filePath: path.resolve(invalidJSON),
messages: [
{
column: 2,
endColumn: 3,
endLine: 1,
line: 1,
message: 'Expected a JSON object, array or literal.',
nodeType: results.results[0].messages[0].nodeType, // we don't care about this one
ruleId: 'json/*',
severity: 2,
source: results.results[0].messages[0].source, // NewLine-characters might differ depending on git-settings
},
],
errorCount: 1,
...(semver.satisfies(eslintPkg.version, '>= 7.32') && {
fatalErrorCount: 0,
}),
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
source: results.results[0].source, // NewLine-characters might differ depending on git-settings
},
],
...(semver.satisfies(eslintPkg.version, '>= 7.32') && {
fatalErrorCount: 0,
}),
errorCount: 1,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
usedDeprecatedRules: results.usedDeprecatedRules, // we don't care about this one
return eslint.lintFiles([invalidJSON]).then(results => {
expect(results).to.eql(
[
{
filePath: path.resolve(invalidJSON),
messages: [
{
column: 2,
endColumn: 3,
endLine: 1,
line: 1,
message: 'Expected a JSON object, array or literal.',
nodeType: results[0].messages[0].nodeType, // we don't care about this one
ruleId: 'json/*',
severity: 2,
source: results[0].messages[0].source, // NewLine-characters might differ depending on git-settings
},
],
errorCount: 1,
...(semver.satisfies(eslintPkg.version, '>= 7.32 || ^8.0.0-0') && {
fatalErrorCount: 0,
}),
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
source: results[0].source, // NewLine-characters might differ depending on git-settings
...(semver.satisfies(eslintPkg.version, '>= 7.0.0') && {
usedDeprecatedRules: results[0].usedDeprecatedRules, // we don't care about this one
}),
},
],
);
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion tests/src/rules/no-amd.js
Expand Up @@ -2,7 +2,7 @@ import { RuleTester } from 'eslint';
import eslintPkg from 'eslint/package.json';
import semver from 'semver';

const ruleTester = new RuleTester();
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015, sourceType: 'module' } });

ruleTester.run('no-amd', require('rules/no-amd'), {
valid: [
Expand Down
2 changes: 1 addition & 1 deletion tests/src/rules/no-commonjs.js
Expand Up @@ -5,7 +5,7 @@ import semver from 'semver';
const EXPORT_MESSAGE = 'Expected "export" or "export default"';
const IMPORT_MESSAGE = 'Expected "import" instead of "require()"';

const ruleTester = new RuleTester();
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015, sourceType: 'module' } });

ruleTester.run('no-commonjs', require('rules/no-commonjs'), {
valid: [
Expand Down

0 comments on commit 67c403b

Please sign in to comment.