最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Force ESLint to use ES6 - Stack Overflow

programmeradmin5浏览0评论

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 (=>).

.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

var stars = [];
var speed = 20;

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

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

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 (=>).

.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

var stars = [];
var speed = 20;

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

  // Create 1000 stars
  for (var i = 0; i < 1000; i++) {
    stars.push(new Star());
  }
}
Share Improve this question edited Dec 23, 2016 at 19:00 Mr. Polywhirl 48.9k12 gold badges93 silver badges144 bronze badges asked Dec 23, 2016 at 18:51 joncodojoncodo 2,3387 gold badges39 silver badges74 bronze badges 4
  • How is any style checker or linter supposed to know when you should or should not use arrow functions? There is a no-var rule though. – user663031 Commented Dec 23, 2016 at 19:06
  • I wondered if that was the case really. Since Es6 lets you use Es5 things – joncodo Commented Dec 23, 2016 at 20:11
  • 1 Arrow functions don't replace normal functions. – Oriol Commented Dec 23, 2016 at 20:26
  • it's not so much that "ES6 lets you use ES5 things", but more that ES6 is a superset of ES5, so that any valid ES5 program (pretty much) is a valid ES6 program. – user663031 Commented Dec 23, 2016 at 20:34
Add a ment  | 

2 Answers 2

Reset to default 6

You can use the prefer-arrow-callback rule to enforce using arrow functions as callbacks.

Also the prefer-const rule enforces using const whenever possible (i.e. if the variable is never reassigned).

You don't use i in your for loop so it's not an error.

You can use the no-var rule but it will effect everything, not only for loops.

If you would have used i in your for loop, then the no-loop-func rule is what you are looking for.

If you prefer arrow functions as callbacks you can use prefer-arrow-callback.

发布评论

评论列表(0)

  1. 暂无评论