Skip to content

Commit

Permalink
breaking: support ESLint 8.x
Browse files Browse the repository at this point in the history
Co-authored-by: Michaël De Boey <[email protected]>
Co-authored-by: Jordan Harband <[email protected]>
Co-authored-by: 华 <[email protected]>

chore: simplify process.exit mock
  • Loading branch information
MichaelDeBoey authored and ljharb committed Aug 29, 2021
1 parent 6584103 commit 378751b
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 208 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 run cover
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@
"babel-preset-env": "^1.7.0",
"codecov": "^2.3.1",
"commitizen": "^2.10.1",
"create-require": "^1.1.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 @@ -64,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
18 changes: 14 additions & 4 deletions src/bin/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ const getSortedRules = require('../lib/sort-rules');
const flattenRulesDiff = require('../lib/flatten-rules-diff');
const stringifyRuleConfig = require('../lib/stringify-rule-config');

(async function () {

const files = [argv._[0], argv._[1]];
const collectedRules = getFilesToCompare(files).map(compareConfigs);
const collectedRules = await Promise.all(getFilesToCompare(files).map(compareConfigs));

const rulesCount = collectedRules.reduce(
(prev, curr) => {
Expand Down Expand Up @@ -66,13 +68,14 @@ function getFilesToCompare(allFiles) {
return filesToCompare;
}

function compareConfigs(currentFiles) {
async function compareConfigs(currentFiles) {
const ruleFinders = await Promise.all(currentFiles.slice(0, 2).map(getRuleFinder));
return {
config1: path.basename(currentFiles[0]),
config2: path.basename(currentFiles[1]),
rules: rulesDifference(
getRuleFinder(currentFiles[0]),
getRuleFinder(currentFiles[1])
ruleFinders[0],
ruleFinders[1]
)
};
}
Expand All @@ -94,3 +97,10 @@ function rulesDifference(a, b) {
)
);
}

process.exit(0);

})().catch(/* istanbul ignore next */(e) => {
console.error(e);
process.exit(1);
});
9 changes: 8 additions & 1 deletion src/bin/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ const getRuleURI = require('eslint-rule-documentation');
const getRuleFinder = require('../lib/rule-finder');
const cli = require('../lib/cli-util');

(async function () {

const specifiedFile = argv._[0];
const finderOptions = {
omitCore: !argv.core,
includeDeprecated: argv.include === 'deprecated',
ext: argv.ext
};
const ruleFinder = getRuleFinder(specifiedFile, finderOptions);
const ruleFinder = await getRuleFinder(specifiedFile, finderOptions);
const errorOut = argv.error && !argv.n;
let processExitCode = 0;

Expand Down Expand Up @@ -76,3 +78,8 @@ if (!argv.c && !argv.p && !argv.a && !argv.u && !argv.d) {
cli.write();
}
process.exit(processExitCode);

})().catch(/* istanbul ignore next */(e) => {
console.error(e);
process.exit(1);
});
68 changes: 46 additions & 22 deletions src/lib/rule-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,38 @@ function _getConfigFile(specifiedFile) {
return require(path.join(process.cwd(), 'package.json')).main;
}

function _getConfigs(configFile, files) {
const cliEngine = new eslint.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));
async function _getConfigs(configFile, files) {
let isPathIgnored;
let getConfigForFile;

if (eslint.ESLint) {
const esLint = new eslint.ESLint({
// Ignore any config applicable depending on the location on the filesystem
useEslintrc: false,
// Point to the particular config
overrideConfigFile: configFile
});
isPathIgnored = esLint.isPathIgnored.bind(esLint);
getConfigForFile = esLint.calculateConfigForFile.bind(esLint);
} else {
const cliEngine = new eslint.CLIEngine({
// Ignore any config applicable depending on the location on the filesystem
useEslintrc: false,
// Point to the particular config
configFile
});
isPathIgnored = cliEngine.isPathIgnored.bind(cliEngine);
getConfigForFile = cliEngine.getConfigForFile.bind(cliEngine);
}

const configs = files.map(async filePath => (
await isPathIgnored(filePath) ? false : getConfigForFile(filePath)
));
return new Set((await Promise.all(configs)).filter(Boolean));
}

function _getConfig(configFile, files) {
return Array.from(_getConfigs(configFile, files)).reduce((prev, item) => {
async function _getConfig(configFile, files) {
return Array.from(await _getConfigs(configFile, files)).reduce((prev, item) => {
return Object.assign(prev, item, {rules: Object.assign({}, prev.rules, item.rules)});
}, {});
}
Expand Down Expand Up @@ -67,7 +85,7 @@ function _getPluginRules(config) {
}

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

function _filterRuleNames(ruleNames, rules, predicate) {
Expand All @@ -90,14 +108,7 @@ function _createExtensionRegExp(extensions) {
return new RegExp(`.\\.(?:${normalizedExts.join("|")})$`);
}

function RuleFinder(specifiedFile, {omitCore, includeDeprecated, ext = ['.js']}) {
const configFile = _getConfigFile(specifiedFile);

const extensionRegExp = _createExtensionRegExp(ext);
const files = glob.sync(`**/*`, {dot: true, matchBase: true})
.filter(file => extensionRegExp.test(file));

const config = _getConfig(configFile, files);
function RuleFinder(config, {omitCore, includeDeprecated}) {
let currentRuleNames = _getCurrentNamesRules(config);
if (omitCore) {
currentRuleNames = currentRuleNames.filter(_isNotCore);
Expand Down Expand Up @@ -135,6 +146,19 @@ function RuleFinder(specifiedFile, {omitCore, includeDeprecated, ext = ['.js']})
this.getDeprecatedRules = () => getSortedRules(deprecatedRuleNames);
}

async function createRuleFinder(specifiedFile, options) {
const configFile = _getConfigFile(specifiedFile);

const {ext = ['.js']} = options;
const extensionRegExp = _createExtensionRegExp(ext);
const files = glob.sync(`**/*`, {dot: true, matchBase: true})
.filter(file => extensionRegExp.test(file));

const config = await _getConfig(configFile, files);

return new RuleFinder(config, options);
}

module.exports = function (specifiedFile, options = {}) {
return new RuleFinder(specifiedFile, options);
return createRuleFinder(specifiedFile, options);
};
15 changes: 12 additions & 3 deletions test/bin/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ const proxyquire = require('proxyquire');
const sinon = require('sinon');

const consoleLog = console.log; // eslint-disable-line no-console
const processExit = process.exit;

const stub = {
'../lib/rule-finder'() {
async '../lib/rule-finder'() {
return {
getCurrentRules() {}, // Noop
getCurrentRulesDetailed() {} // Noop
Expand All @@ -15,6 +16,8 @@ const stub = {
'../lib/object-diff': sinon.stub().returns([{'test-rule': {config1: 'foo-config', config2: 'bar-config'}}])
};

let exitStatus;

describe('diff', () => {
beforeEach(() => {
process.argv = process.argv.slice(0, 2);
Expand All @@ -24,18 +27,23 @@ describe('diff', () => {
consoleLog(...args);
}
});
exitStatus = new Promise(resolve => {
process.exit = resolve;
});
});

afterEach(() => {
console.log.restore(); // eslint-disable-line no-console
process.exit = processExit;
// purge yargs cache
delete require.cache[require.resolve('yargs')];
});

it('logs diff', () => {
it('logs diff', async () => {
process.argv[2] = './foo';
process.argv[3] = './bar';
proxyquire('../../src/bin/diff', stub);
assert.strictEqual(await exitStatus, 0);
assert.ok(
console.log.calledWith( // eslint-disable-line no-console
sinon.match(
Expand All @@ -45,11 +53,12 @@ describe('diff', () => {
);
});

it('logs diff verbosely', () => {
it('logs diff verbosely', async () => {
process.argv[2] = '--verbose';
process.argv[3] = './foo';
process.argv[4] = './bar';
proxyquire('../../src/bin/diff', stub);
assert.strictEqual(await exitStatus, 0);
assert.ok(
console.log.calledWith( // eslint-disable-line no-console
sinon.match(
Expand Down
Loading

0 comments on commit 378751b

Please sign in to comment.