- Notifications
You must be signed in to change notification settings - Fork117
A Gulp plugin for identifying and reporting on patterns found in ECMAScript/JavaScript code.
License
adametry/gulp-eslint
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
npm install gulp-eslint
const{src, task}=require('gulp');consteslint=require('gulp-eslint');task('default',()=>{returnsrc(['scripts/*.js'])// eslint() attaches the lint output to the "eslint" property// of the file object so it can be used by other modules..pipe(eslint())// eslint.format() outputs the lint results to the console.// Alternatively use eslint.formatEach() (see Docs)..pipe(eslint.format())// To have the process exit with an error code (1) on// lint error, return the stream and pipe to failAfterError last..pipe(eslint.failAfterError());});
Or use the plugin API to do things like:
gulp.src(['**/*.js','!node_modules/**']).pipe(eslint({rules:{'my-custom-rule':1,'strict':2},globals:['jQuery','$'],envs:['browser']})).pipe(eslint.formatEach('compact',process.stderr));
For additional examples, look through theexample directory.
No explicit configuration. A.eslintrc
file may be resolved relative to each linted file.
Type:Object
Setconfiguration ofrules.
{"rules":{"camelcase":1,"comma-dangle":2,"quotes":0}}
Type:Array
Specify global variables to declare.
{"globals":["jQuery","$"]}
Type:Boolean
This option instructs ESLint to try to fix as many issues as possible. The fixes are applied to the gulp stream. The fixed content can be saved to file usinggulp.dest
(Seeexample/fix.js). Rules that are fixable can be found in ESLint'srules list.
When fixes are applied, a "fixed" property is set totrue
on the fixed file's ESLint result.
Type:Boolean
Whentrue
, this option will filter warning messages from ESLint results. This mimics the ESLint CLIquiet option.
Type:function (message, index, list) { return Boolean(); }
When provided a function, it will be used to filter ESLint result messages, removing any messages that do not return atrue
(or truthy) value.
Type:Array
Specify a list ofenvironments to be applied.
Type:Array
This option allows you to specify additional directories from which to load rules files. This is useful when you have custom rules that aren't suitable for being bundled with ESLint. This option works much like the ESLint CLI'srulesdir option.
Type:String
Path to the ESLint rules configuration file. For more information, see the ESLint CLIconfig option andUsing Configuration Files.
Type:Boolean
Whentrue
, add a result warning when ESLint ignores a file. This can be used to file files that are needlessly being loaded bygulp.src
. For example, since ESLint automatically ignores "node_modules" file paths and gulp.src does not, a gulp task may take seconds longer just reading files from the "node_modules" directory.
Type:Boolean
Whenfalse
, ESLint will not load.eslintrc files.
Type:String
Shorthand for definingoptions.configFile
.
Type:function (result) {}
Call a function for each ESLint file result. No returned value is expected. If an error is thrown, it will be wrapped in a Gulp PluginError and emitted from the stream.
gulp.src(['**/*.js','!node_modules/**']).pipe(eslint()).pipe(eslint.result(result=>{// Called for each ESLint result.console.log(`ESLint result:${result.filePath}`);console.log(`# Messages:${result.messages.length}`);console.log(`# Warnings:${result.warningCount}`);console.log(`# Errors:${result.errorCount}`);}));
Type:function (result, callback) { callback(error); }
Call an asynchronous function for each ESLint file result. The callback must be called for the stream to finish. If a value is passed to the callback, it will be wrapped in a Gulp PluginError and emitted from the stream.
Type:function (results) {}
Call a function once for all ESLint file results before a stream finishes. No returned value is expected. If an error is thrown, it will be wrapped in a Gulp PluginError and emitted from the stream.
The results list has a "warningCount" property that is the sum of warnings in all results; likewise, an "errorCount" property is set to the sum of errors in all results.
gulp.src(['**/*.js','!node_modules/**']).pipe(eslint()).pipe(eslint.results(results=>{// Called once for all ESLint results.console.log(`Total Results:${results.length}`);console.log(`Total Warnings:${results.warningCount}`);console.log(`Total Errors:${results.errorCount}`);}));
Type:function (results, callback) { callback(error); }
Call an asynchronous function once for all ESLint file results before a stream finishes. The callback must be called for the stream to finish. If a value is passed to the callback, it will be wrapped in a Gulp PluginError and emitted from the stream.
Stop a task/stream if an ESLint error has been reported for any file.
// Cause the stream to stop(/fail) before copying an invalid JS file to the output directorygulp.src(['**/*.js','!node_modules/**']).pipe(eslint()).pipe(eslint.failOnError());
Stop a task/stream if an ESLint error has been reported for any file, but wait for all of them to be processed first.
// Cause the stream to stop(/fail) when the stream ends if any ESLint error(s) occurred.gulp.src(['**/*.js','!node_modules/**']).pipe(eslint()).pipe(eslint.failAfterError());
Format all linted files once. This should be used in the stream after piping througheslint
; otherwise, this will find no ESLint results to format.
Theformatter
argument may be aString
,Function
, orundefined
. As aString
, a formatter module by that name or path will be resolved as a module, relative toprocess.cwd()
, or as one of theESLint-provided formatters. Ifundefined
, the ESLint “stylish” formatter will be resolved. AFunction
will be called with anArray
of file linting results to format.
// use the default "stylish" ESLint formattereslint.format()// use the "checkstyle" ESLint formattereslint.format('checkstyle')// use the "eslint-path-formatter" module formatter// (@see https://github.com/Bartvds/eslint-path-formatter)eslint.format('node_modules/eslint-path-formatter')
Theoutput
argument may be aWritableStream
,Function
, orundefined
. As aWritableStream
, the formatter results will be written to the stream. Ifundefined
, the formatter results will be written togulp’s log. AFunction
will be called with the formatter results as the only parameter.
// write to gulp's log (default)eslint.format();// write messages to stdouteslint.format('junit',process.stdout)
Format each linted file individually. This should be used in the stream after piping througheslint
; otherwise, this will find no ESLint results to format.
The arguments forformatEach
are the same as the arguments forformat
.
ESLint may be configured explicity by using any of the following plugin options:config
,rules
,globals
, orenv
. If theuseEslintrc option is not set tofalse
, ESLint will attempt to resolve a file by the name of.eslintrc
within the same directory as the file to be linted. If not found there, parent directories will be searched until.eslintrc
is found or the directory root is reached.
ESLint will ignore files that do not have a.js
file extension at the point of linting (some plugins may change file extensions mid-stream). This avoids unintentional linting of non-JavaScript files.
ESLint will also detect an.eslintignore
file at the cwd or a parent directory. See theESLint docs to learn how to construct this file.
ESLint results are attached as an "eslint" property to the vinyl files that pass through a Gulp.js stream pipeline. This is available to streams that follow the initialeslint
stream. Theeslint.result andeslint.results methods are made available to support extensions and custom handling of ESLint results.
About
A Gulp plugin for identifying and reporting on patterns found in ECMAScript/JavaScript code.