Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve lint script #426

Merged
merged 15 commits into from Nov 2, 2019
42 changes: 33 additions & 9 deletions test/lint/lint.js
Expand Up @@ -3,16 +3,15 @@
const {CLIEngine} = require('eslint');
const unicorn = require('../..');

const {configs: {recommended}} = unicorn;
const {rules} = recommended;
const {recommended} = unicorn.configs;
const files = [process.argv[2] || '.'];
const fix = process.argv.includes('--fix');
const ruleIds = Object.keys(unicorn.rules);
const unicornRules = new Map(Object.entries(unicorn.rules));

const cli = new CLIEngine({
...recommended,
baseConfig: recommended,
rules: {
...rules,

// TODO: remove this override, when #391 is fixed
'unicorn/consistent-function-scoping': 'off'
},
Expand All @@ -22,14 +21,39 @@ const cli = new CLIEngine({

cli.addPlugin('eslint-plugin-unicorn', unicorn);

// Make sure rules are loaded from codebase
const loadedRules = cli.getRules();
if (!ruleIds.every(ruleId => unicornRules.get(ruleId) === loadedRules.get(`unicorn/${ruleId}`))) {
console.error('`eslint-plugin-unicorn` rules are not loaded from codebase.');
process.exit(1);
}

const report = cli.executeOnFiles(files);

if (fix) {
const {errorCount, warningCount, fixableErrorCount, fixableWarningCount} = report;

const hasFixable = fixableErrorCount || fixableWarningCount;

if (fix && hasFixable) {
CLIEngine.outputFixes(report);
}

const formatter = cli.getFormatter();
if (errorCount || warningCount) {
const formatter = cli.getFormatter();
console.log(formatter(report.results));

console.log();
console.log(`You need fix failed test${errorCount + warningCount > 1 ? 's' : ''} above and run \`npm run lint <file>\` to check again.`);
fisker marked this conversation as resolved.
Show resolved Hide resolved

console.log(formatter(report.results));
if (hasFixable) {
console.log();
console.log('You may also want run `npm run lint <file> --fix` to fix fixable problems.');
}

console.log();
console.log('* If you\'re making a new rule, you can fix this later. *');
} else {
console.log('All tests have passed.');
}

process.exit(report.errorCount);
process.exit(errorCount);