npm.io
6.0.0 • Published 7 years ago

gulp-eslint

Licence
MIT
Version
6.0.0
Deps
3
Size
30 kB
Vulns
0
Weekly
0
Stars
565

gulp-eslint Build Status Coverage Status

A gulp plugin for ESLint

Installation

Use npm.

npm install gulp-eslint

Usage

const {src, task} = require('gulp');
const eslint = require('gulp-eslint');

task('default', () => {
    return src(['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',
			'

For additional examples, look through the example directory.

API

eslint()

No explicit configuration. A .eslintrc file may be resolved relative to each linted file.

eslint(options)

See ESlint CLIEngine options.

options.rules

Type: Object

Set configuration of rules.

{
	"rules":{
		"camelcase": 1,
		"comma-dangle": 2,
		"quotes": 0
	}
}
options.globals

Type: Array

Specify global variables to declare.

{
	"globals":[
		"jQuery",
		"$"
	]
}
options.fix

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 using gulp.dest (See example/fix.js). Rules that are fixable can be found in ESLint's rules list.

When fixes are applied, a "fixed" property is set to true on the fixed file's ESLint result.

options.quiet

Type: Boolean

When true, this option will filter warning messages from ESLint results. This mimics the ESLint CLI quiet 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 a true (or truthy) value.

options.envs

Type: Array

Specify a list of environments to be applied.

options.rulePaths

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's rulesdir option.

options.configFile

Type: String

Path to the ESLint rules configuration file. For more information, see the ESLint CLI config option and Using Configuration Files.

options.warnFileIgnored

Type: Boolean

When true, add a result warning when ESLint ignores a file. This can be used to file files that are needlessly being loaded by gulp.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.

options.useEslintrc

Type: Boolean

When false, ESLint will not load .eslintrc files.

eslint(configFilePath)

Type: String

Shorthand for defining options.configFile.

eslint.result(action)

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.

eslint.results(action)

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.

eslint.failOnError()

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 directory
gulp.src(['**/*.js','!node_modules/**'])
	.pipe(eslint())
	.pipe(eslint.failOnError());
eslint.failAfterError()

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());
eslint.format(formatter, output)

Format all linted files once. This should be used in the stream after piping through eslint; otherwise, this will find no ESLint results to format.

The formatter argument may be a String, Function, or undefined. As a String, a formatter module by that name or path will be resolved as a module, relative to process.cwd(), or as one of the ESLint-provided formatters. If undefined, the ESLint “stylish” formatter will be resolved. A Function will be called with an Array of file linting results to format.

// use the default "stylish" ESLint formatter
eslint.format()

// use the "checkstyle" ESLint formatter
eslint.format('checkstyle')

// use the "eslint-path-formatter" module formatter
// (@see https://github.com/Bartvds/eslint-path-formatter)
eslint.format('node_modules/eslint-path-formatter')

The output argument may be a WritableStream, Function, or undefined. As a WritableStream, the formatter results will be written to the stream. If undefined, the formatter results will be written to gulp’s log. A Function will be called with the formatter results as the only parameter.

// write to gulp's log (default)
eslint.format();

// write messages to stdout
eslint.format('junit', process.stdout)
eslint.formatEach(formatter, output)

Format each linted file individually. This should be used in the stream after piping through eslint; otherwise, this will find no ESLint results to format.

The arguments for formatEach are the same as the arguments for format.

Configuration

ESLint may be configured explicity by using any of the following plugin options: config, rules, globals, or env. If the useEslintrc option is not set to false, 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.

Ignore Files

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 the ESLint docs to learn how to construct this file.

Extensions

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 initial eslint stream. The eslint.result and eslint.results methods are made available to support extensions and custom handling of ESLint results.

Gulp-Eslint Extensions:
], envs: [ 'browser' ] })) .pipe(eslint.formatEach('compact', process.stderr));

For additional examples, look through the example directory.

API

eslint()

No explicit configuration. A __INLINE_CODE_0__ file may be resolved relative to each linted file.

eslint(options)

See ESlint CLIEngine options.

options.rules

Type: __INLINE_CODE_1__

Set configuration of rules.

__CODE_BLOCK_3__
options.globals

Type: __INLINE_CODE_2__

Specify global variables to declare.

__CODE_BLOCK_4__
options.fix

Type: __INLINE_CODE_3__

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 using __INLINE_CODE_4__ (See example/fix.js). Rules that are fixable can be found in ESLint's rules list.

When fixes are applied, a "fixed" property is set to __INLINE_CODE_5__ on the fixed file's ESLint result.

options.quiet

Type: __INLINE_CODE_6__

When __INLINE_CODE_7__, this option will filter warning messages from ESLint results. This mimics the ESLint CLI quiet option.

Type: __INLINE_CODE_8__

When provided a function, it will be used to filter ESLint result messages, removing any messages that do not return a __INLINE_CODE_9__ (or truthy) value.

options.envs

Type: __INLINE_CODE_10__

Specify a list of environments to be applied.

options.rulePaths

Type: __INLINE_CODE_11__

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's rulesdir option.

options.configFile

Type: __INLINE_CODE_12__

Path to the ESLint rules configuration file. For more information, see the ESLint CLI config option and Using Configuration Files.

options.warnFileIgnored

Type: __INLINE_CODE_13__

When __INLINE_CODE_14__, add a result warning when ESLint ignores a file. This can be used to file files that are needlessly being loaded by __INLINE_CODE_15__. 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.

options.useEslintrc

Type: __INLINE_CODE_16__

When __INLINE_CODE_17__, ESLint will not load .eslintrc files.

eslint(configFilePath)

Type: __INLINE_CODE_18__

Shorthand for defining __INLINE_CODE_19__.

eslint.result(action)

Type: __INLINE_CODE_20__

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.

__CODE_BLOCK_5__

Type: __INLINE_CODE_21__

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.

eslint.results(action)

Type: __INLINE_CODE_22__

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.

__CODE_BLOCK_6__

Type: __INLINE_CODE_23__

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.

eslint.failOnError()

Stop a task/stream if an ESLint error has been reported for any file.

__CODE_BLOCK_7__
eslint.failAfterError()

Stop a task/stream if an ESLint error has been reported for any file, but wait for all of them to be processed first.

__CODE_BLOCK_8__
eslint.format(formatter, output)

Format all linted files once. This should be used in the stream after piping through __INLINE_CODE_24__; otherwise, this will find no ESLint results to format.

The __INLINE_CODE_25__ argument may be a __INLINE_CODE_26__, __INLINE_CODE_27__, or __INLINE_CODE_28__. As a __INLINE_CODE_29__, a formatter module by that name or path will be resolved as a module, relative to __INLINE_CODE_30__, or as one of the ESLint-provided formatters. If __INLINE_CODE_31__, the ESLint “stylish” formatter will be resolved. A __INLINE_CODE_32__ will be called with an __INLINE_CODE_33__ of file linting results to format.

__CODE_BLOCK_9__

The __INLINE_CODE_34__ argument may be a __INLINE_CODE_35__, __INLINE_CODE_36__, or __INLINE_CODE_37__. As a __INLINE_CODE_38__, the formatter results will be written to the stream. If __INLINE_CODE_39__, the formatter results will be written to gulp’s log. A __INLINE_CODE_40__ will be called with the formatter results as the only parameter.

__CODE_BLOCK_10__
eslint.formatEach(formatter, output)

Format each linted file individually. This should be used in the stream after piping through __INLINE_CODE_41__; otherwise, this will find no ESLint results to format.

The arguments for __INLINE_CODE_42__ are the same as the arguments for __INLINE_CODE_43__.

Configuration

ESLint may be configured explicity by using any of the following plugin options: __INLINE_CODE_44__, __INLINE_CODE_45__, __INLINE_CODE_46__, or __INLINE_CODE_47__. If the useEslintrc option is not set to __INLINE_CODE_48__, ESLint will attempt to resolve a file by the name of __INLINE_CODE_49__ within the same directory as the file to be linted. If not found there, parent directories will be searched until __INLINE_CODE_50__ is found or the directory root is reached.

Ignore Files

ESLint will ignore files that do not have a __INLINE_CODE_51__ 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 __INLINE_CODE_52__ file at the cwd or a parent directory. See the ESLint docs to learn how to construct this file.

Extensions

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 initial __INLINE_CODE_53__ stream. The eslint.result and eslint.results methods are made available to support extensions and custom handling of ESLint results.

Gulp-Eslint Extensions:

Keywords