Skip to content

Commit

Permalink
replace gruntify-eslint with basic eslint-cli wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger authored and spalger committed Dec 2, 2016
1 parent 054e798 commit 71732e7
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 48 deletions.
7 changes: 4 additions & 3 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/optimize
/src/fixtures/vislib/mock_data
/src/ui/public/angular-bootstrap
/test/fixtures/scenarios
/src/core_plugins/console/public/webpackShims
/src/core_plugins/console/public/tests/webpackShims
/src/core_plugins/timelion/bower_components
/src/core_plugins/timelion/vendor_components
test/fixtures/scenarios
optimize
test/fixtures/scenarios
12 changes: 0 additions & 12 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@ module.exports = function (grunt) {
' * Copyright (c) <%= grunt.template.today("yyyy") %> <%= package.author.company %>;' +
' Licensed <%= package.license %> */\n'
},

lintThese: [
'Gruntfile.js',
'<%= root %>/tasks/**/*.js',
'<%= root %>/test/**/*.js',
'<%= src %>/**/*.js',
'!<%= src %>/ui/public/angular-bootstrap/**/*.js',
'!<%= src %>/core_plugins/timelion/bower_components/**/*.js',
'!<%= src %>/core_plugins/timelion/vendor_components/**/*.js',
'!<%= src %>/fixtures/**/*.js',
'!<%= root %>/test/fixtures/scenarios/**/*.js'
]
};

grunt.config.merge(config);
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@
"grunt-karma": "2.0.0",
"grunt-run": "0.6.0",
"grunt-simple-mocha": "0.4.0",
"gruntify-eslint": "3.0.0",
"gulp-sourcemaps": "1.7.3",
"handlebars": "4.0.5",
"husky": "0.8.1",
Expand Down
57 changes: 30 additions & 27 deletions tasks/config/eslint.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
var resolve = require('path').resolve;
import { resolve } from 'path';
module.exports = grunt => ({
options: {
paths: [
'Gruntfile.js',
'bin',
'config',
'src',
'tasks',
'test',
'utilities',
],
},

module.exports = function (grunt) {
return {
// just lint the source dir
source: {
options: {
cache: resolve(grunt.config.get('root'), '.eslint.fixSource.cache')
},
source: {
options: {
cache: resolve(grunt.config.get('root'), '.eslint.fixSource.cache')
}
},

files: {
src: '<%= lintThese %>'
}
},
fixSource: {
options: {
cache: resolve(grunt.config.get('root'), '.eslint.fixSource.cache'),
fix: true
}
},

// lint the source and fix any fixable errors
fixSource: {
options: {
cache: resolve(grunt.config.get('root'), '.eslint.fixSource.cache'),
fix: true
},

files: {
src: '<%= lintThese %>'
}
},

staged: {}
};
};
staged: {
options: {
paths: null // overridden by lintStagedFiles task
}
}
});
42 changes: 42 additions & 0 deletions tasks/eslint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { CLIEngine } from 'eslint';

const OPTION_DEFAULTS = {
paths: null,
cache: null,
fix: false
};

module.exports = 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'),
});

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(' & ')}`);
});
};
26 changes: 21 additions & 5 deletions tasks/lint_staged_files.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
import { resolve } from 'path';
import { isStaged, getFilename } from './utils/files_to_commit';
import { CLIEngine } from 'eslint';
import minimatch from 'minimatch';

const root = resolve(__dirname, '..');

export default function (grunt) {
grunt.registerTask('lintStagedFiles', function () {
grunt.task.requires('collectFilesToCommit');

// match these patterns
var patterns = grunt.config.get('eslint.source.files.src');
if (!patterns) grunt.fail.warn('eslint file pattern is not defined');
// 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 sourcePathRegexps = cli.resolveFileGlobPatterns(eslintSourcePaths)
.map(glob => minimatch.makeRe(glob));

const files = grunt.config
.get('filesToCommit')
.filter(isStaged)
.map(getFilename)
.map(file => resolve(root, file))
.filter(file => grunt.file.isMatch(patterns, file));
.filter(file => {
if (!sourcePathRegexps.some(re => file.match(re))) {
return false;
}

if (cli.isPathIgnored(file)) {
return false;
}

return true;
});

grunt.config.set('eslint.staged.files.src', files);
grunt.config.set('eslint.staged.options.paths', files);
grunt.task.run(['eslint:staged']);
});
}

0 comments on commit 71732e7

Please sign in to comment.