简体   繁体   English

ESlint linting gulpfile.js

[英]ESlint linting gulpfile.js

Hi i use Atom and plugin called lint-eslint for linting my javascript code and it does work fine but i have one really annoying linting error on my gulpfile.js 嗨,我使用Atom和名为lint-eslint的插件来整理我的javascript代码,它的确工作正常,但我的gulpfile.js上确实有一个令人讨厌的整理错误

Here is the code that triggers linting error, i'm using airbnb .eslintrc configuration file for ESlinter. 这是触发掉毛错误的代码,我正在使用ESlinter的airbnb .eslintrc配置文件。

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

Note that i'm trying to use arrow syntax. 请注意,我正在尝试使用箭头语法。 I'm getting following error Unexpected block statement surrounding arrow body. 我收到以下错误, 箭头主体周围出现意外的块语句。 And when i remove the return it goes away. 当我删除return它就消失了。

It has to do with the early return stream from gulp src is there some other way to return it or how do i correct the error i know i can ignore the file but i want to know if there is another way to return gulp.src() 它与gulp src的早期返回流有关,是否还有其他方法可以将其返回,或者我如何纠正错误,我知道我可以忽略该文件,但我想知道是否还有另一种返回gulp.src()

Since your function simply return a value, you can omit the curly braces {} and the return statement, which makes the code lighter and easier to read. 由于函数仅返回一个值,因此可以省略花括号{}return语句,这使代码更轻便且更易于阅读。

The rule involved is arrow-body-style which "enforces the consistent use of braces in arrow functions" . 涉及的规则是arrow-body-style ,它“在花键功能中强制使用花括号”

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

ES6 arrow function can return an object without word 'return' like this: ES6箭头功能可以返回不带单词“ return”的对象,如下所示:

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

This is ES6 standard. 这是ES6标准。

And eslint-airbnb style guide believes that if your arrow function does nothing but return an object, the 'return' will not be necessary. eslint-airbnb样式指南认为,如果箭头功能除了返回对象外什么也不做,则不需要“返回”。 So your code could go like this: 因此您的代码可以像这样:

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

See more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions about Returning object literals. 查看更多:有关返回对象文字的https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_functions

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM