[6.x] [eslint] add eslint dev script (#14889) (#14941)

This commit is contained in:
Spencer 2017-11-14 10:24:00 -07:00 committed by GitHub
parent 81b9e727bd
commit 242c6fae2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 64 additions and 93 deletions

View file

@ -57,8 +57,8 @@
"precommit": "grunt precommit",
"karma": "karma start",
"elasticsearch": "grunt esvm:dev:keepalive",
"lint": "grunt eslint:source",
"lintroller": "grunt eslint:fixSource",
"lint": "echo 'use `node scripts/eslint`' && false",
"lintroller": "echo 'use `node scripts/eslint --fix`' && false",
"makelogs": "echo 'use `node scripts/makelogs`' && false",
"mocha": "echo 'use `node scripts/mocha`' && false",
"sterilize": "grunt sterilize",

2
scripts/eslint.js Normal file
View file

@ -0,0 +1,2 @@
require('../src/babel-register');
require('../src/dev/run_eslint');

View file

@ -0,0 +1,14 @@
export const DEFAULT_ESLINT_PATHS = [
'Gruntfile.js',
'bin',
'config',
'src',
'scripts',
'tasks',
'test',
'ui_framework/src',
'ui_framework/doc_site/src',
'ui_framework/generator_kui',
'utilities',
'webpackShims'
];

15
src/dev/run_eslint.js Normal file
View file

@ -0,0 +1,15 @@
import { parse } from 'eslint/lib/options';
import { DEFAULT_ESLINT_PATHS } from './default_eslint_paths';
const options = parse(process.argv);
if (!options._.length && !options.printConfig) {
process.argv.push(...DEFAULT_ESLINT_PATHS);
}
if (!process.argv.includes('--no-cache')) {
process.argv.push('--cache');
}
// common-js is requires to that logic before this executes before loading eslint
require('eslint/bin/eslint');

View file

@ -1,38 +0,0 @@
import { resolve } from 'path';
export default grunt => ({
options: {
paths: [
'Gruntfile.js',
'bin',
'config',
'src',
'scripts',
'tasks',
'test',
'ui_framework/src',
'ui_framework/doc_site/src',
'ui_framework/generator_kui',
'utilities',
'webpackShims',
],
},
source: {
options: {
cache: resolve(grunt.config.get('root'), '.eslint.fixSource.cache')
}
},
fixSource: {
options: {
cache: resolve(grunt.config.get('root'), '.eslint.fixSource.cache'),
fix: true
}
},
staged: {
options: {
paths: null // overridden by lintStagedFiles task
}
}
});

View file

@ -33,6 +33,22 @@ module.exports = function (grunt) {
}, []);
return {
eslint: {
cmd: process.execPath,
args: [
require.resolve('../../scripts/eslint'),
'--no-cache'
]
},
eslintStaged: {
cmd: process.execPath,
args: [
require.resolve('../../scripts/eslint'),
// staged paths are written here by lintStagedFiles task
]
},
testServer: {
options: {
wait: false,

View file

@ -1,44 +0,0 @@
import { CLIEngine } from 'eslint';
const OPTION_DEFAULTS = {
paths: null,
cache: null,
fix: false
};
export default grunt => {
grunt.registerMultiTask('eslint', function () {
const options = this.options(OPTION_DEFAULTS);
if (!options.paths) {
grunt.fatal(new Error('No eslint.options.paths specified'));
return;
}
const cli = new CLIEngine({
cache: options.cache,
fix: options.fix,
cwd: grunt.config.get('root'),
});
// report includes an entire list of files checked and the
// fixes, errors, and warning for each.
const report = cli.executeOnFiles(options.paths);
// output fixes to disk
if (options.fix) {
CLIEngine.outputFixes(report);
}
// log the formatted linting report
const formatter = cli.getFormatter();
const errTypes = [];
if (report.errorCount > 0) errTypes.push('errors');
if (report.warningCount > 0) errTypes.push('warning');
if (!errTypes.length) return;
grunt.log.write(formatter(report.results));
grunt.fatal(`eslint ${errTypes.join(' & ')}`);
});
};

View file

@ -29,7 +29,7 @@ module.exports = function (grunt) {
'jenkins:env',
'rejectRejFiles',
'eslint:source',
'run:eslint',
'licenses',
'test:server',
'test:jest',

View file

@ -4,6 +4,8 @@ import { CLIEngine } from 'eslint';
import { red, blue } from 'ansicolors';
import minimatch from 'minimatch';
import { DEFAULT_ESLINT_PATHS } from '../src/dev/default_eslint_paths';
const root = resolve(__dirname, '..');
export default function (grunt) {
@ -12,10 +14,7 @@ export default function (grunt) {
// convert eslint paths to globs
const cli = new CLIEngine();
const eslintSourcePaths = grunt.config.get('eslint.options.paths');
if (!eslintSourcePaths) grunt.fail.warn('eslint.options.paths is not defined');
const sourcePathGlobs = cli.resolveFileGlobPatterns(eslintSourcePaths);
const sourcePathGlobs = cli.resolveFileGlobPatterns(DEFAULT_ESLINT_PATHS);
const files = grunt.config
.get('filesToCommit')
@ -25,7 +24,7 @@ export default function (grunt) {
.filter(file => {
if (!sourcePathGlobs.some(glob => minimatch(file, glob))) {
if (extname(file) === '.js') {
grunt.log.writeln(`${red('WARNING:')} ${file} not selected by grunt eslint config`);
grunt.log.writeln(`${red('WARNING:')} ${file} not selected by src/eslint/default_eslint_paths`);
}
return false;
}
@ -40,7 +39,14 @@ export default function (grunt) {
return true;
});
grunt.config.set('eslint.staged.options.paths', files);
grunt.task.run(['eslint:staged']);
if (files.length) {
const args = grunt.config.get('run.eslintStaged.args');
grunt.config.set('run.eslintStaged.args', [
...args,
...files
]);
grunt.task.run(['run:eslintStaged']);
}
});
}

View file

@ -97,7 +97,7 @@ module.exports = function (grunt) {
if (subTask) grunt.fail.fatal(`invalid task "test:${subTask}"`);
grunt.task.run(_.compact([
!grunt.option('quick') && 'eslint:source',
!grunt.option('quick') && 'run:eslint',
'licenses',
'test:quick'
]));