简体   繁体   English

强制ESLint使用ES6

[英]Force ESLint to use ES6

I have my ESLint all set up and working, but I want it to throw errors whenever I don't use ES6 stuff like let , const or arrow functions ( => ). 我的ESLint都设置和工作,但我希望它在我不使用像letconst或箭头函数( => )这样的ES6时抛出错误。

.eslintrc .eslintrc

{
  "env": {
    "node": true,
    "es6": true,
    "mocha": true
  },
  "rules": {
    "semi": 2
  },
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "script",
    "ecmaFeatures": {
      "arrowFunctions": true,
      "binaryLiterals": true,
      "blockBindings": true,
      "classes": true
    }
  }
}

Currently, this will not throw errors for: 目前,这不会抛出错误:

main.js main.js

var stars = [];
var speed = 20;

function setup() {
  createCanvas(windowWidth, windowHeight);

  // Create 1000 stars
  for (var i = 0; i < 1000; i++) {
    stars.push(new Star());
  }
}

You can use the prefer-arrow-callback rule to enforce using arrow functions as callbacks. 您可以使用prefer-arrow-callback规则强制使用箭头函数作为回调。

Also the prefer-const rule enforces using const whenever possible (ie if the variable is never reassigned). 此外, prefer-const规则强制尽可能使用const (即,如果永远不重新赋值变量)。

You don't use i in your for loop so it's not an error. 你不要在for循环中使用i ,所以这不是错误。

You can use the no-var rule but it will effect everything, not only for loops. 您可以使用no-var规则但它会影响所有内容,而不仅仅是循环。

If you would have used i in your for loop, then the no-loop-func rule is what you are looking for. 如果您在for循环中使用了i ,那么no-loop-func规则就是您要寻找的。

If you prefer arrow functions as callbacks you can use prefer-arrow-callback . 如果您更喜欢箭头函数作为回调,则可以使用prefer-arrow-callback

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

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