如何使用 gulp-eslint 修复文件?
How to fix files with gulp-eslint?
我正在使用 gulp 和 eslint。
没有gulp,我就运行eslint ./src --fix
。我不知道如何用 gulp 实现这一点。我在下面尝试过,将 fix 设置为 true,但它没有修复任何文件:
gulp.task('lint', ['./src/**.js'], () => {
return gulp.src()
.pipe($.eslint({fix:true}))
.pipe($.eslint.format())
.pipe($.eslint.failAfterError());
});
我要修复 ./src
下的所有文件。我该如何实现?
这是我项目中的正确工作方式:
var gulp = require('gulp'),
eslint = require('gulp-eslint'),
gulpIf = require('gulp-if');
function isFixed(file) {
// Has ESLint fixed the file contents?
return file.eslint != null && file.eslint.fixed;
}
gulp.task('lint', function () {
// ESLint ignores files with "node_modules" paths.
// So, it's best to have gulp ignore the directory as well.
// Also, Be sure to return the stream from the task;
// Otherwise, the task may end before the stream has finished.
return gulp.src(['./src/**.js','!node_modules/**'])
// eslint() attaches the lint output to the "eslint" property
// of the file object so it can be used by other modules.
.pipe(eslint({fix:true}))
// eslint.format() outputs the lint results to the console.
// Alternatively use eslint.formatEach() (see Docs).
.pipe(eslint.format())
// if fixed, write the file to dest
.pipe(gulpIf(isFixed, gulp.dest('../test/fixtures')))
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failAfterError
// last.
.pipe(eslint.failAfterError());
});
gulp.task('default', ['lint'], function () {
// This will only run if the lint task is successful...
});
老问题,但添加 .pipe(gulp.dest(file => file.base))
是它对我有用的原因。如:
gulp.task('lint', ['./src/**.js'], () => {
return gulp.src()
.pipe($.eslint({fix:true}))
.pipe($.eslint.format())
.pipe(gulp.dest(file => file.base)) // <-- NEW: Output fixed version of file.
.pipe($.eslint.failAfterError());
});
我使用 gulp-eslint-new 因为 gulp-eslint 没有维护。
它有一个修复文件的方法gulpESLintNew.fix()
,在自述文件中有解释。
Overwrite files with the fixed content provided by ESLint. This should be used in conjunction with the option fix
in gulpESLintNew(options)
. Files without a fix and files that were not processed by ESLint will be left untouched.
还有一个 example:
function lintNFix() {
return src('demo/**/*.js').pipe(gulpESLintNew({ fix: true }))
.pipe(gulpESLintNew.format())
// If a file has a fix, overwrite it.
.pipe(gulpESLintNew.fix());
}
我正在使用 gulp 和 eslint。
没有gulp,我就运行eslint ./src --fix
。我不知道如何用 gulp 实现这一点。我在下面尝试过,将 fix 设置为 true,但它没有修复任何文件:
gulp.task('lint', ['./src/**.js'], () => {
return gulp.src()
.pipe($.eslint({fix:true}))
.pipe($.eslint.format())
.pipe($.eslint.failAfterError());
});
我要修复 ./src
下的所有文件。我该如何实现?
这是我项目中的正确工作方式:
var gulp = require('gulp'),
eslint = require('gulp-eslint'),
gulpIf = require('gulp-if');
function isFixed(file) {
// Has ESLint fixed the file contents?
return file.eslint != null && file.eslint.fixed;
}
gulp.task('lint', function () {
// ESLint ignores files with "node_modules" paths.
// So, it's best to have gulp ignore the directory as well.
// Also, Be sure to return the stream from the task;
// Otherwise, the task may end before the stream has finished.
return gulp.src(['./src/**.js','!node_modules/**'])
// eslint() attaches the lint output to the "eslint" property
// of the file object so it can be used by other modules.
.pipe(eslint({fix:true}))
// eslint.format() outputs the lint results to the console.
// Alternatively use eslint.formatEach() (see Docs).
.pipe(eslint.format())
// if fixed, write the file to dest
.pipe(gulpIf(isFixed, gulp.dest('../test/fixtures')))
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failAfterError
// last.
.pipe(eslint.failAfterError());
});
gulp.task('default', ['lint'], function () {
// This will only run if the lint task is successful...
});
老问题,但添加 .pipe(gulp.dest(file => file.base))
是它对我有用的原因。如:
gulp.task('lint', ['./src/**.js'], () => {
return gulp.src()
.pipe($.eslint({fix:true}))
.pipe($.eslint.format())
.pipe(gulp.dest(file => file.base)) // <-- NEW: Output fixed version of file.
.pipe($.eslint.failAfterError());
});
我使用 gulp-eslint-new 因为 gulp-eslint 没有维护。
它有一个修复文件的方法gulpESLintNew.fix()
,在自述文件中有解释。
Overwrite files with the fixed content provided by ESLint. This should be used in conjunction with the option
fix
ingulpESLintNew(options)
. Files without a fix and files that were not processed by ESLint will be left untouched.
还有一个 example:
function lintNFix() {
return src('demo/**/*.js').pipe(gulpESLintNew({ fix: true }))
.pipe(gulpESLintNew.format())
// If a file has a fix, overwrite it.
.pipe(gulpESLintNew.fix());
}