├── .npmrc ├── test └── fixture │ ├── 1.js │ └── 2.js ├── .gitignore ├── .gitattributes ├── .travis.yml ├── screenshot.png ├── conf ├── eslint.json └── rules │ └── no-alert.js ├── .editorconfig ├── gruntfile.js ├── package.json ├── license ├── tasks └── eslint.js └── readme.md /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /test/fixture/1.js: -------------------------------------------------------------------------------- 1 | alert('hello'); 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /test/fixture/2.js: -------------------------------------------------------------------------------- 1 | var hello_world = 'foo'; 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.js text eol=lf 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '8' 4 | - '6' 5 | - '4' 6 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hirokith/grunt-eslint/master/screenshot.png -------------------------------------------------------------------------------- /conf/eslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | "no-alert": 1, 4 | "camelcase": 2 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /conf/rules/no-alert.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | module.exports = function (context) { 3 | return { 4 | CallExpression(node) { 5 | if (node.callee.name === 'alert') { 6 | context.report(node, 'testing custom rules.'); 7 | } 8 | } 9 | }; 10 | }; 11 | -------------------------------------------------------------------------------- /gruntfile.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | module.exports = grunt => { 3 | grunt.initConfig({ 4 | eslint: { 5 | options: { 6 | configFile: 'conf/eslint.json', 7 | rulePaths: ['conf/rules'], 8 | quiet: true 9 | }, 10 | validate: ['test/fixture/{1,2}.js'] 11 | }, 12 | shell: { 13 | eslint: { 14 | command: 'grunt eslint', 15 | options: { 16 | callback: (err, stdout, stderr, cb) => { 17 | if (/test\/fixture\/2\.js/.test(stdout)) { 18 | if (/camelcase/.test(stdout) && !/\swarning\s/.test(stdout)) { 19 | cb(); 20 | } else { 21 | cb(false); 22 | } 23 | } else { 24 | cb(false); 25 | } 26 | } 27 | } 28 | } 29 | } 30 | }); 31 | 32 | grunt.loadTasks('tasks'); 33 | grunt.loadNpmTasks('grunt-shell'); 34 | grunt.registerTask('default', ['shell:eslint']); 35 | }; 36 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "grunt-eslint", 3 | "version": "20.1.0", 4 | "description": "Validate files with ESLint", 5 | "license": "MIT", 6 | "repository": "sindresorhus/grunt-eslint", 7 | "author": { 8 | "name": "Sindre Sorhus", 9 | "email": "sindresorhus@gmail.com", 10 | "url": "sindresorhus.com" 11 | }, 12 | "engines": { 13 | "node": ">=4" 14 | }, 15 | "scripts": { 16 | "test": "grunt" 17 | }, 18 | "files": [ 19 | "tasks" 20 | ], 21 | "keywords": [ 22 | "gruntplugin", 23 | "lint", 24 | "validate", 25 | "report", 26 | "jshint", 27 | "jslint", 28 | "ecmascript", 29 | "esprima" 30 | ], 31 | "dependencies": { 32 | "chalk": "^2.1.0", 33 | "eslint": "^4.0.0" 34 | }, 35 | "devDependencies": { 36 | "grunt": "^1.0.1", 37 | "grunt-cli": "^1.2.0", 38 | "grunt-shell": "^2.1.0" 39 | }, 40 | "peerDependencies": { 41 | "grunt": ">=0.4.0" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /tasks/eslint.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const chalk = require('chalk'); 3 | const eslint = require('eslint'); 4 | 5 | module.exports = grunt => { 6 | grunt.registerMultiTask('eslint', 'Validate files with ESLint', function () { 7 | const opts = this.options({ 8 | outputFile: false, 9 | quiet: false, 10 | maxWarnings: -1 11 | }); 12 | 13 | if (this.filesSrc.length === 0) { 14 | grunt.log.writeln(chalk.magenta('Could not find any files to validate')); 15 | return true; 16 | } 17 | 18 | const formatter = eslint.CLIEngine.getFormatter(opts.format); 19 | 20 | if (!formatter) { 21 | grunt.warn(`Could not find formatter ${opts.format}`); 22 | return false; 23 | } 24 | 25 | const engine = new eslint.CLIEngine(opts); 26 | 27 | let report; 28 | try { 29 | report = engine.executeOnFiles(this.filesSrc); 30 | } catch (err) { 31 | grunt.warn(err); 32 | return false; 33 | } 34 | 35 | if (opts.fix) { 36 | eslint.CLIEngine.outputFixes(report); 37 | } 38 | 39 | let results = report.results; 40 | 41 | if (opts.quiet) { 42 | results = eslint.CLIEngine.getErrorResults(results); 43 | } 44 | 45 | const output = formatter(results); 46 | 47 | if (opts.outputFile) { 48 | grunt.file.write(opts.outputFile, output); 49 | } else if (output) { 50 | console.log(output); 51 | } 52 | 53 | const tooManyWarnings = opts.maxWarnings >= 0 && report.warningCount > opts.maxWarnings; 54 | 55 | if (report.errorCount === 0 && tooManyWarnings) { 56 | grunt.warn(`ESLint found too many warnings (maximum: ${opts.maxWarnings})`); 57 | } 58 | 59 | return report.errorCount === 0; 60 | }); 61 | }; 62 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # grunt-eslint [![Build Status](https://travis-ci.org/sindresorhus/grunt-eslint.svg?branch=master)](https://travis-ci.org/sindresorhus/grunt-eslint) 2 | 3 | > Validate files with [ESLint](https://eslint.org) 4 | 5 | ![](screenshot.png) 6 | 7 | 8 | ## Install 9 | 10 | ``` 11 | $ npm install --save-dev grunt-eslint 12 | ``` 13 | 14 | 15 | ## Usage 16 | 17 | ```js 18 | require('load-grunt-tasks')(grunt); // npm install --save-dev load-grunt-tasks 19 | 20 | grunt.initConfig({ 21 | eslint: { 22 | target: ['file.js'] 23 | } 24 | }); 25 | 26 | grunt.registerTask('default', ['eslint']); 27 | ``` 28 | 29 | 30 | ## Examples 31 | 32 | ### Custom config and rules 33 | 34 | ```js 35 | grunt.initConfig({ 36 | eslint: { 37 | options: { 38 | configFile: 'conf/eslint.json', 39 | rulePaths: ['conf/rules'] 40 | }, 41 | target: ['file.js'] 42 | } 43 | }); 44 | ``` 45 | 46 | ### Custom formatter 47 | 48 | ```js 49 | grunt.initConfig({ 50 | eslint: { 51 | options: { 52 | format: require('eslint-tap') 53 | }, 54 | target: ['file.js'] 55 | } 56 | }); 57 | ``` 58 | 59 | 60 | ## Options 61 | 62 | See the [ESLint options](http://eslint.org/docs/developer-guide/nodejs-api#cliengine). 63 | 64 | In addition the following options are supported: 65 | 66 | ### format 67 | 68 | Type: `string`
69 | Default: `'stylish'` 70 | 71 | Name of a [built-in formatter](https://github.com/nzakas/eslint/tree/master/lib/formatters) or path to a custom one. 72 | 73 | Some formatters you might find useful: [eslint-json](https://github.com/sindresorhus/eslint-json), [eslint-tap](https://github.com/sindresorhus/eslint-tap). 74 | 75 | ### outputFile 76 | 77 | Type: `string`
78 | Default: `''` 79 | 80 | Output the report to a file. 81 | 82 | ### quiet 83 | 84 | Type: `boolean`
85 | Default: `false` 86 | 87 | Report errors only. 88 | 89 | ### maxWarnings 90 | 91 | Type: `number`
92 | Default: `-1` *(means no limit)* 93 | 94 | Number of warnings to trigger non-zero exit code. 95 | 96 | 97 | ## License 98 | 99 | MIT © [Sindre Sorhus](https://sindresorhus.com) 100 | --------------------------------------------------------------------------------