From ca45ae2293f90de9599b5927c6b1874e5ab66b42 Mon Sep 17 00:00:00 2001 From: spalger Date: Thu, 15 Dec 2016 12:01:21 -0700 Subject: [PATCH 1/2] [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 --- tasks/lint_staged_files.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/tasks/lint_staged_files.js b/tasks/lint_staged_files.js index d8117b62c2772..d9accbeea9420 100644 --- a/tasks/lint_staged_files.js +++ b/tasks/lint_staged_files.js @@ -1,11 +1,18 @@ -import { resolve } from 'path'; +import { extname, resolve, relative } from 'path'; import { isStaged, getFilename } from './utils/files_to_commit'; import { CLIEngine } from 'eslint'; +import { red } from 'ansicolors'; import minimatch from 'minimatch'; const root = resolve(__dirname, '..'); export default function (grunt) { + + function skippingFile(file, reason) { + if (extname(file) !== '.js') return; + grunt.log.writeln(`${red('WARNING:')}: Not linting "${file}" because it is ${reason}`); + } + grunt.registerTask('lintStagedFiles', function () { grunt.task.requires('collectFilesToCommit'); @@ -14,20 +21,21 @@ 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))) { + skippingFile(file, 'not selected by grunt eslint config'); return false; } if (cli.isPathIgnored(file)) { + skippingFile(file, 'ignored by .eslintignore'); return false; } From b152e3543fde38d2dafec669394ac6275442a049 Mon Sep 17 00:00:00 2001 From: spalger Date: Thu, 15 Dec 2016 14:45:40 -0700 Subject: [PATCH 2/2] [grunt/eslint] only log warning wtr grunt paths --- tasks/lint_staged_files.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/tasks/lint_staged_files.js b/tasks/lint_staged_files.js index d9accbeea9420..edb312317d676 100644 --- a/tasks/lint_staged_files.js +++ b/tasks/lint_staged_files.js @@ -1,18 +1,12 @@ import { extname, resolve, relative } from 'path'; import { isStaged, getFilename } from './utils/files_to_commit'; import { CLIEngine } from 'eslint'; -import { red } from 'ansicolors'; +import { red, blue } from 'ansicolors'; import minimatch from 'minimatch'; const root = resolve(__dirname, '..'); export default function (grunt) { - - function skippingFile(file, reason) { - if (extname(file) !== '.js') return; - grunt.log.writeln(`${red('WARNING:')}: Not linting "${file}" because it is ${reason}`); - } - grunt.registerTask('lintStagedFiles', function () { grunt.task.requires('collectFilesToCommit'); @@ -30,12 +24,16 @@ export default function (grunt) { .map(file => relative(root, resolve(file))) // resolve to pwd, then get relative from the root .filter(file => { if (!sourcePathGlobs.some(glob => minimatch(file, glob))) { - skippingFile(file, 'not selected by grunt eslint config'); + if (extname(file) === '.js') { + grunt.log.writeln(`${red('WARNING:')} ${file} not selected by grunt eslint config`); + } return false; } if (cli.isPathIgnored(file)) { - skippingFile(file, 'ignored by .eslintignore'); + if (extname(file) === '.js') { + grunt.log.writeln(`${blue('DEBUG:')} ${file} ignored by .eslintignore`); + } return false; }