Eslintのメモ
Eslintの設定は基本的には"extends": "eslint:recommended"
などを利用しているので、あまり個別に設定することはないのですが、 少し変更するときに毎回調べているのでメモしておこうと思います。
有効・無効の設定
値 | 内容 |
---|---|
0 | 無効 |
1 | 有効(警告) |
2 | 有効(エラー) |
オプションの利用
ルールごとに独自のオプションを定義できる場合があります。
//rule: [{"error", "warn", "off"},{option}…]
"semi": ["error", "never"]
設定方法
rulesに設定内容を追加していく。
{
"rules": {
"quotes": [2, "double"],
"curly": 2,
}
}
rules
詳しくは公式を確認。
semi
セミコロン無しを許容しない
// Bad
var website = "eslint.org"
// Good
var website = "eslint.org";
option
◎セミコロンの利用を禁止
semi:["error", "never"]
// Bad
var name = "ESLint";
object.method = function() {
// ...
};
◎セミコロン無しを禁止
semi: ["error", "always"]
var name = "ESLint"
object.method = function() {
// ...
}
indent
インデントの設定
"indent": ["error", "tab"] // or "indent": ["error", 2]
// Bad if (a) { b=c; function foo(d) { e=f; } } //Good if (a) { b=c; function foo(d) { e=f; } }
curly
中括弧の規則を指定する。
// Bad
if (foo) foo++;
// Good
if (foo) {
foo++;
}
accessor-pairs
セッターとゲッターはペアで定義する必要がある
// Bad
var o = {
set a(value) {
this.val = value;
}
};
// Good
var o = {
set a(value) {
this.val = value;
},
get a() {
return this.val;
}
};
dot-notation
ドット表記
// Bad
var x = foo[bar];
// Good
var x = foo.bar;
eqeqeq
型まで判定する
// Bad
if (x == 42) { }
if (obj.getStuff() != undefined) { }
// Good
if (x === 42) { }
if (obj.getStuff() !== undefined) { }
no-alert
alert、confirm、promptを利用しない
// Bad
alert("here!");
confirm("Are you sure?");
prompt("What's your name?", "John Doe");
no-multi-spaces
複数のスペースを許容しない
// Bad
if(foo === "bar") {}
var a = 1;
//Good
if(foo === "bar") {}
var a = 1;
no-process-env
process.envの使用を許容しない。
// Bad
if(process.env.NODE_ENV === "development") { /*error Unexpected use of process.env.*/
//...
}
//Good
var config = require("./config");
if(config.env === "development") {
//...
}
no-redeclare
同じ変数を複数回定義を許可しない。
// Bad
var a = 3;
var a = 10; /*error "a" is already defined*/
//Good
var a = 3;
// ...
a = 10;
no-console
コンソールの消し忘れ
// Bad
console.log("Made it here.");
no-empty
空のブロックを許容しない
// Bad
if (foo) {
}
no-extra-parens
不要な()を許容しない。
// Bad
var a = (b * c);
no-extra-semi
不要なセミコロンを許容しない。
// Bad
var x = 5;;
function foo() {
// code
};
valid-jsdoc
JSDocのコメントルールにあっているか
// Bad
// missing type for @param and missing @returns
/** // 2 errors
* A description
* @param num1 The first number.
*/
function foo(num1) {
// ...
}