[grunt/eslint] fix precommit linting (#9532)

Backports PR #9510

**Commit 1:**
[grunt/eslint] fix precommit linting

 - remove use of `minimatch.makeRe()` because it does not support the entire glob syntax
 - log a warning whenever a js file is excluded by the `lintStagedFiles` task
 - eslint globs are relative to the project root, ensure that we check against relative version

* Original sha: ca45ae2293
* Authored by spalger <spalger@users.noreply.github.com> on 2016-12-15T19:01:21Z

**Commit 2:**
[grunt/eslint] only log warning wtr grunt paths

* Original sha: b152e3543f
* Authored by spalger <spalger@users.noreply.github.com> on 2016-12-15T21:45:40Z
This commit is contained in:
jasper 2016-12-16 15:32:04 -05:00 committed by Spencer
parent 53f1ebdb22
commit aa41e38d38

View file

@ -1,6 +1,7 @@
import { resolve } from 'path';
import { extname, resolve, relative } from 'path';
import { isStaged, getFilename } from './utils/files_to_commit';
import { CLIEngine } from 'eslint';
import { red, blue } from 'ansicolors';
import minimatch from 'minimatch';
const root = resolve(__dirname, '..');
@ -14,20 +15,25 @@ export default function (grunt) {
const eslintSourcePaths = grunt.config.get('eslint.options.paths');
if (!eslintSourcePaths) grunt.fail.warn('eslint.options.paths is not defined');
const sourcePathRegexps = cli.resolveFileGlobPatterns(eslintSourcePaths)
.map(glob => minimatch.makeRe(glob));
const sourcePathGlobs = cli.resolveFileGlobPatterns(eslintSourcePaths);
const files = grunt.config
.get('filesToCommit')
.filter(isStaged)
.map(getFilename)
.map(file => resolve(root, file))
.map(file => relative(root, resolve(file))) // resolve to pwd, then get relative from the root
.filter(file => {
if (!sourcePathRegexps.some(re => file.match(re))) {
if (!sourcePathGlobs.some(glob => minimatch(file, glob))) {
if (extname(file) === '.js') {
grunt.log.writeln(`${red('WARNING:')} ${file} not selected by grunt eslint config`);
}
return false;
}
if (cli.isPathIgnored(file)) {
if (extname(file) === '.js') {
grunt.log.writeln(`${blue('DEBUG:')} ${file} ignored by .eslintignore`);
}
return false;
}