Skip to content

Commit

Permalink
Fix lint script
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed May 9, 2020
1 parent e66b5f0 commit 0d86839
Showing 1 changed file with 38 additions and 39 deletions.
77 changes: 38 additions & 39 deletions test/lint/lint.js
@@ -1,62 +1,61 @@
#!/usr/bin/env node
'use strict';
const {CLIEngine} = require('eslint');
const {ESLint} = require('eslint');
const unicorn = require('../..');

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({
const eslint = new ESLint({
baseConfig: recommended,
useEslintrc: false,
plugins: {
unicorn
},
fix,
ignorePattern: [
]
overrideConfig: {
ignorePatterns: [
'coverage',
'test/integration/fixtures'
]
}
});

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);
}
(async function main() {
const results = await eslint.lintFiles(files);

const report = cli.executeOnFiles(files);
if (fix) {
await ESLint.outputFixes(results);
}

const {
errorCount,
warningCount,
fixableErrorCount,
fixableWarningCount
} = report;
const errorCount = results.reduce((total, {errorCount}) => total + errorCount, 0);
const warningCount = results.reduce((total, {warningCount}) => total + warningCount, 0);
const fixableErrorCount = results.reduce((total, {fixableErrorCount}) => total + fixableErrorCount, 0);
const fixableWarningCount = results.reduce((total, {fixableWarningCount}) => total + fixableWarningCount, 0);

const hasFixable = fixableErrorCount || fixableWarningCount;
const hasFixable = fixableErrorCount || fixableWarningCount;

if (fix) {
CLIEngine.outputFixes(report);
}
if (errorCount || warningCount) {
const {format} = await eslint.loadFormatter();
console.log(format(results));

if (errorCount || warningCount) {
const formatter = cli.getFormatter();
console.log(formatter(report.results));
console.log();
console.log(`You need to fix the failed test${errorCount + warningCount > 1 ? 's' : ''} above and run \`npm run lint <file>\` to check again.`);

console.log();
console.log(`You need to fix the failed test${errorCount + warningCount > 1 ? 's' : ''} above and run \`npm run lint <file>\` to check again.`);
if (hasFixable) {
console.log();
console.log('You may also want run `npm run lint <file> --fix` to fix fixable problems.');
}

if (hasFixable) {
console.log();
console.log('You may also want run `npm run lint <file> --fix` to fix fixable problems.');
console.log('* If you\'re making a new rule, you can fix this later. *');
} else {
console.log('All tests have passed.');
}

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(errorCount);
process.exit(errorCount);
})().catch(error => {
process.exitCode = 1;
console.error(error);
});

0 comments on commit 0d86839

Please sign in to comment.