ESlint 检测 gulpfile.js

ESlint linting gulpfile.js

你好,我使用 Atom 和名为 lint-eslint 的插件来检查我的 javascript 代码,它确实工作正常,但我的 gulpfile.js

上有一个非常烦人的检查错误

这是触发 linting 错误的代码,我正在使用 airbnb .eslintrc ESlinter 配置文件。

gulp.task('lint', () => {
 return gulp.src(['**/*.js', '!node_modules/**', '!src/**'])
 .pipe(gulpif(args.verbose, gprint()))
 .pipe(eslint())
 .pipe(eslint.format())
 .pipe(eslint.failAfterError());
});

请注意,我正在尝试使用箭头语法。 我收到以下错误 箭头主体周围的意外块语句。 当我删除 return 它消失了。

它与来自 gulp src 的早期 return 流有关是否有其他方法可以 return 它或者我如何更正我知道我可以忽略的错误文件,但我想知道是否有另一种方法 return gulp.src()

由于您的函数只是 return 一个值,您可以省略大括号 {}return 语句,这使代码更轻便,更易于阅读。

涉及的规则是arrow-body-style其中"enforces the consistent use of braces in arrow functions".

gulp.task('lint', () => 
  gulp.src(['**/*.js', '!node_modules/**', '!src/**'])
   .pipe(gulpif(args.verbose, gprint()))
   .pipe(eslint())
   .pipe(eslint.format())
   .pipe(eslint.failAfterError())
);

ES6 箭头函数可以 return 一个没有单词的对象 'return' 像这样:

let func = () => ({key: 'value'});
let a = func();   // and a will be an object {key: 'value'}

这是 ES6 标准。

并且 eslint-airbnb 风格指南认为,如果你的箭头函数除了 return 一个对象什么都不做,那么 'return' 就没有必要了。所以你的代码可以是这样的:

gulp.task('lint', () => (
 gulp.src(['**/*.js', '!node_modules/**', '!src/**'])
  .pipe(gulpif(args.verbose, gprint()))
  .pipe(eslint())
  .pipe(eslint.format())
  .pipe(eslint.failAfterError())
));

查看更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions 关于返回对象文字。