说明:
- 语气:强制 > 要求 == !禁止 > 尽量 > 推荐 == !不推荐;
- 🔧表示ESLint可以自动修复。
indent, no-tabs, no-mixed-spaces-and-tabs
// ✗ bad
function () {
console.log('Hello');
}
// ✗ bad
function () {
——console.log('Hello');
}
// ✓ good
function () {
console.log('Hello');
}
// ✗ bad
(function (global) {
// ...stuff...
})(this);
// ✗ bad
(function (global) {
// ...stuff...
})(this);↵
↵
// ✓ good
(function (global) {
// ...stuff...
})(this);↵
// ✗ bad
const value = 'Hello';
console.log(value);
// ✓ good
const value = 'Hello';
console.log(value);
// ✗ bad
function bar() {
console.log(foo);
}
// ✓ good
function bar() {
console.log(foo);
}
// ✗ bad
'use strict';
let foo;
// ✓ good
'use strict';
let foo;
// ✗ bad
let a = 1;
if(foo === 'bar') {}
a << b
let arr = [1, 2];
a ? b: c;
// ✓ good
let a = 1;
if(foo === 'bar') {}
a << b
let arr = [1, 2];
a ? b: c;
semi-spacing, comma-spacing, key-spacing
// ✗ bad
const arr = [1,2,3,4];
const obj = { id:1,name:'Alice' };
foo(a,b,c);
for (let i = 0;i < 10;i++)
// ✓ good
const arr = [1, 2];
const obj = { id: 1, name: 'Alice' };
foo(a, b, c);
for (let i = 0; i < 10; i++)
no-whitespace-before-property, rest-spread-spacing
// ✗ bad
foo. bar. baz();
fn(... args);
[... arr, 4, 5, 6];
// ✓ good
foo.bar.baz();
fn(...args);
[...arr, 4, 5, 6];
space-unary-ops, space-infix-ops, arrow-spacing
// ✗ bad
i ++;
let x=-y*5;
let message='Hello, '+name+'!';
const func=(x)=>{};
// ✓ good
i++;
let x = -y * 5;
let message = 'Hello, ' + name + '!';
const func = (x) => {};
space-in-parens, array-bracket-spacing, computed-property-spacing, object-curly-spacing, block-spacing
// ✗ bad
const num = 3 * ( 2 + 5 );
function hello( name ) {
console.log( 'Hi,', name );
}
if ( test ) {
thing();
}
// ✓ good
const num = 3 * (2 + 5);
function hello(name) {
console.log('Hi,', name);
}
if (test) {
thing();
}
// ✗ bad
const arr = [ 1, 2, 3 ];
const [ x, y ] = z;
obj[ key ] = 'test';
user[ 'name' ] = 'John';
// ✓ good
const arr = [1, 2, 3];
const [x, y] = z;
obj[key] = 'test';
user['name'] = 'John';
// ✗ bad
const obj = {id: 1, name: 'Alice'};
const {x, y} = z;
function foo() {return true;}
if (foo) { bar = 0;}
// ✓ good
const obj = { id: 1, name: 'Alice' };
const { x, y } = z;
function foo() { return true; }
if (foo) { bar = 0; }
// ✗ bad
product.attr({price: 10.6, tags: ['food', 'sweet']});
product.attr( { price: 10.6, tags: [ 'food', 'sweet' ] } );
// ✓ good
product.attr({ price: 10.6, tags: ['food', 'sweet'] });
// ✗ bad
function test(){
console.log('test');
}
// ✓ good
function test() {
console.log('test');
}
// ✗ bad
dog.set('attr',{
age: '1 year',
breed: 'Bernese Mountain Dog',
});
// ✓ good
dog.set('attr', {
age: '1 year',
breed: 'Bernese Mountain Dog',
});
keyword-spacing, func-call-spacing, space-before-function-paren
保持一致是最好的,你不需要在添加/删除函数名时,考虑要不要添加/删除空格。
// ✗ bad
if(isJedi) {
fight ();
} else {
chat ();
}
// ✓ good
if (isJedi) {
fight();
} else {
chat();
}
// ✗ bad
function fight () {
console.log ('Swooosh!');
}
run(function() {
console.log ('Swooosh!');
});
// ✓ good
function fight() {
console.log('Swooosh!');
}
run(function () {
console.log('Swooosh!');
});
// ✗ bad
if (cond) {
}
while (cond) { }
// ✓ good
if (cond) {
// TODO
}
while (cond) { /* TODO */ }
// ✗ bad
function foo() {}
const foo = function () {};
const foo = () => {};
// ✓ good
function foo() { /* noop */ }
const foo = function () { /* noop */ };
const foo = () => { /* noop */ };
// ✗ bad
function foo()
{
return true;
}
if (foo) {
bar();
}
else {
baz();
}
try
{
somethingRisky();
} catch(e)
{
handleError();
}
// ✓ good
function foo() {
return true;
}
if (foo) {
bar();
} else {
baz();
}
try {
somethingRisky();
} catch(e) {
handleError();
}
function func() { return true; }
if (foo) { bar(); }
// ✗ bad
if (!obj)
obj = {
id: 1,
name: 'alice',
};
while (cond)
if (cond2)
doSomething();
else
doSomethingElse();
if (foo) {
foo++;
}
while (cond) {
doSomething();
}
for (let i = 0; i < count; i++) {
doSomething();
}
// ✓ good
if (!obj) {
obj = {
id: 1,
name: 'alice',
};
}
while (cond) {
if (cond2)
doSomething();
else
doSomethingElse();
}
if (foo)
foo++;
while (cond)
doSomething();
for (let i = 0; i < count; i++)
doSomething();
semi, no-extra-semi, no-unexpected-multiline
// ✗ bad
(function () {
const name = 'Skywalker'
return name;;
})()
// ✓ good
(function () {
const name = 'Skywalker';
return name;
})();
// ✗ bad
const story = [
once
, upon
, aTime
];
// ✓ good
const story = [
once,
upon,
aTime,
];
// ✗ bad
foo = doSomething(), val;
if (doSomething(), !!test);
while (val = foo(), val < 42);
// ✓ good
doSomething();
foo = val;
if (!!test);
while ((val = foo()) < 42);
这会使git diffs更简洁,使编辑器的上下移动快捷键更方便。不用担心IE8会报错,Babel等编译器会自动去除拖尾逗号。
// ✗ bad - git diff without trailing comma const hero = { firstName: 'Florence', - lastName: 'Nightingale' + lastName: 'Nightingale', + inventorOf: ['coxcomb chart', 'modern nursing'] }; // ✓ good - git diff with trailing comma const hero = { firstName: 'Florence', lastName: 'Nightingale', + inventorOf: ['coxcomb chart', 'modern nursing'], };
// ✗ bad
const hero = {
firstName: 'Dana',
lastName: 'Scully'
};
const heroes = [
'Batman',
'Superman'
];
// ✓ good
const hero = {
firstName: 'Dana',
lastName: 'Scully',
};
const heroes = [
'Batman',
'Superman',
];
// ✗ bad
const hero = { firstName, lastName, };
const arr = [1, 2, 3, ];
// ✓ good
const hero = { firstName, lastName };
const arr = [1, 2, 3];
// ✗ bad
const hello = "Hello, Bob!";
const element = `<div class="box"></div>`;
const message = 'I don\'t like quotes.';
// ✓ good
const hello = 'Hello, Bob!';
const element = `<div class="${className}"></div>`;
const message = "I don't like quotes.";
// ✗ bad
const a = 'some' + 'string';
const a = '1' + '0';
// ✓ good
const c = 'somestring';
const a = '10';
// ✗ bad
const foo = '\"This\" \i\s \'quoted\'';
// ✓ good
const foo = '"This" is \'quoted\'';
const foo = `"This" is 'quoted'`;
// ✗ bad
const message = 'Hello \
world';
// ✓ good
const message = `Hello
world`;
prefer-template, template-curly-spacing
// ✗ bad
return 'How are you, ' + name + '?';
return ['How are you, ', name, '?'].join();
return `How are you, ${ name }?`;
// ✓ good
return `How are you, ${name}?`;
// ✗ bad
$('#items').
find('.selected').
highlight().
end().
find('.open').
updateCount();
// ✓ good
$('#items')
.find('.selected')
.highlight()
.end()
.find('.open')
.updateCount();
$('#items').find('.selected').highlight().end().find('.open').updateCount();
// ✗ bad
const p = promise.
then(function() {
// code
}).
catch(function() {
// code
});
// ✓ good
const p = promise.then(function() {
// code
}).catch(function() {
// code
});
// ✗ bad
const result = luke['jedi'];
// ✓ good
const result = luke.jedi;
const result = luke.class;
const result = luke[key];
// ✗ bad
const user = { ['name']: 'John Doe' };
// ✓ good
const user = { name: 'John Doe' };
// ✗ bad
const atom = {
value: value,
add: function (value) {
return atom.value + value;
},
};
// ✓ good
const atom = {
value,
add(value) {
return atom.value + value;
},
};
// ✗ bad
const bad = {
'foo': 3,
'bar': 4,
'data-blah': 5,
};
// ✓ good
const good = {
foo: 3,
bar: 4,
'data-blah': 5,
};
该规则旨在消除非类型安全的相等操作符。例如以下语句均被认为是
true
:
[] == false
[] == ![]
3 == "03"
// ✗ bad
if (a == b)
if (foo == true)
if (bananas != 1)
if (value == undefined)
if (typeof foo == 'undefined')
if ('hello' != 'world')
if (0 == 0)
if (true == true)
if (foo == null)
// ✓ good
if (a === b)
if (foo === true)
if (bananas !== 1)
if (value === undefined)
if (typeof foo === 'undefined')
if ('hello' !== 'world')
if (0 === 0)
if (true === true)
if (foo === null)
// ✗ bad
if ('red' === color)
if (true == flag)
if (-1 < str.indexOf(substr))
// ✓ good
if (color === 'red')
if (flag)
if (0 <= x && x < 1)
// ✗ bad
let fullHeight = borderTop +
innerHeight +
borderBottom;
if (someCodition ||
otherCondition) {
// ...
}
// ✓ good
let fullHeight = borderTop
+ innerHeight
+ borderBottom;
if (someCodition
|| otherCondition) {
// ...
}
// ✗ bad
if (true) { /* ... */ }
while (x = -1) { /* ... */ }
let result = 0 ? a : b;
// ✓ good
if (x === 0) { /* ... */ }
while (x) { /* ... */ }
let result = x !== 0 ? a : b;
// ✗ bad
if (count = 0) { /* code */ }
while (node = node.parentNode) { /* code */ }
// ✓ good
if (count === 0) { /* code */ }
while ((node = node.parentNode)) { /* code */ }
// ✗ bad
let result = !!x ? a : b;
if (!!obj) {
// ...
}
while (Boolean(obj)) {
// ...
}
// ✓ good
let result = x ? a : b;
if (obj) {
// ...
}
while (obj) {
// ...
}
// ✗ bad
const foo = a ? a : b;
const bar = c ? true : false;
const baz = c ? false : true;
const bazz = num > 1 ? true : false;
// ✓ good
const foo = a || b;
const bar = !!c;
const baz = !c;
const bazz = num > 1;
no-nested-ternary, multiline-ternary
会使代码难以理解,请使用
if
语句。
// ✗ bad
let location = env.development
? 'localhost'
: 'www.api.com';
// ✓ good
let location = env.development ? 'localhost' : 'www.api.com';
// ✗ bad
let thing = foo ? bar : baz === qux ? quxx : foobar;
// ✓ good
let thing;
if (foo)
thing = bar;
else
thing = baz === qux ? quxx : foobar;
// ✗ bad
if (!condition)
doSomething();
else
doSomethingElse();
if (a !== b)
doSomething();
else
doSomethingElse();
// ✓ good
if (condition)
doSomething();
else
doSomethingElse();
if (a === b)
doSomething();
else
doSomethingElse();
// ✗ bad
if (!key in obj) { /* code */ }
if (!obj instanceof Person) { /* code */ }
// ✓ good
if (!(key in obj)) { /* code */ }
if (!(obj instanceof Person)) { /* code */ }
if (('' + !key) in object) {/* code */ }
no-undef, prefer-const, no-var
// ✗ bad
hero = new Hero();
var hero = new Hero();
// ✓ good
const hero = new Hero();
// ✗ bad
const bar = true,
num = 20,
items = getItems();
let index = 0,
silent = false,
hero = new Hero();
// ✗ bad
const bar = true;
let silent = false;
let index = 0;
const num = 20;
const items = getItems();
let hero = new Hero();
// ✓ good
const bar = true;
const num = 20;
const items = getItems();
let index = 0;
let silent = false;
let hero = new Hero();
// ✗ bad
let bar = undefined;
// ✓ good
let bar;
const baz = undefined;
// ✗ bad
const score = 100;
score = 125;
let name = 'John';
let name = 'Jane';
// ✓ good
let score = 100;
score = 125;
let name = 'John';
name = 'Jane';
delete
只用于删除属性。
// ✗ bad
let x;
delete x;
// ✓ good
delete obj.x;
// ✗ bad
alert(a);
const a = 10;
f();
function f() {}
function g() {
return b;
}
const b = 1;
// ✓ good
const a = 10;
alert(a);
function f() {}
f(1);
const b = 1;
function g() {
return b;
}
// ✗ bad
function func() {
let result = something();
}
// ✗ bad
const OBJEcttsssss = {};
const this_is_my_object = {};
function c() {}
// ✓ good
const thisIsMyObject = {};
function thisIsMyFunction() {}
// ✗ bad
function user(options) {
this.name = options.name;
}
const bad = new user({
name: 'nope',
});
// ✓ good
class User {
constructor(options) {
this.name = options.name;
}
}
const good = new User({
name: 'yup',
});
命名是为了可读性,而不是为了机械地去贴近一个算法。
// ✗ bad
import SmsContainer from './containers/SmsContainer';
// ✗ bad
const HttpRequests = [
// ...
];
// ✓ good
import SMSContainer from './containers/SMSContainer';
// ✓ good
const HTTPRequests = [
// ...
];
no-new-wrappers, no-new-symbol, no-array-constructor, no-new-object, no-new-func
// ✗ bad
const str = new String('Hello world');
const num = new Number(33);
const bool = new Boolean(false);
const sym = new Symbol('sym');
const arr = new Array();
const obj = new Object();
const func = new Function('a', 'b', 'return a + b');
// ✓ good
const str = String(value);
const num = Number(value);
const bool = Boolean(value);
const sym = Symbol('sym');
const arr = [];
const obj = {};
const func = (a, b) => a + b;
// ✗ bad
const date = new Date
const dog = new Animal;
// ✓ good
const date = new Date();
const dog = new Animal();
// ✗ bad
typeof x === 'strnig';
typeof x === 'undefimed';
typeof x === 'nunber';
typeof x === 'fucntion';
// ✓ good
typeof x === 'string';
typeof x === 'undefined';
typeof x === type;
typeof x === typeof y;
// ✗ bad
let b = Boolean(foo);
let b = foo.indexOf('.') !== -1;
let n = Number(foo);
let s = String(foo);
foo = String(foo);
// ✓ good
let b = !!foo;
let b = ~foo.indexOf('.');
let n = +foo;
let s = '' + foo;
foo += '';
// ✗ bad
const foo = Symbol();
// ✓ good
const foo = Symbol('a symbol');
// ✗ bad
let num = .5;
let num = 2.;
let num = -.7;
// ✓ good
let num = 0.5;
let num = 2.0;
let num = -0.7;
// ✗ bad
if (num === NaN) { /* ... */ }
// ✓ good
if (isNaN(num)) { /* ... */ }
func-style, no-inner-declarations, no-func-assign
函数声明很容易被提升到当前作用域的顶部,这意味着可以把调用它的语句放在函数声明之前。
// ✗ bad
function foo() {
// ...
}
// ✓ good
const foo = function () {
// ...
}
const foo = () => { /* ... */ }
// ✗ bad
function foo() { return; }
function foo() {
doSomething();
return;
}
// ✓ good
function foo() { return 5; }
function foo() {
return doSomething();
}
// ✗ bad
function () {
console.log('Hello');
}();
(function () {
console.log('Hello');
}());
// ✓ good
(function () {
console.log('Hello');
})();
// ✗ bad
function joinAll() {
const args = Array.prototype.slice.call(arguments);
return args.join('');
}
// ✓ good
function joinAll(...args) {
return args.join('');
}
// ✗ bad
const x = [1, 2, 3, 4, 5];
console.log.apply(console, x);
// ✓ good
const x = [1, 2, 3, 4, 5];
console.log(...x);
// ✗ bad
new (Function.prototype.bind.apply(Date, [null, 2016, 8, 5]));
// ✓ good
new Date(...[2016, 8, 5]);
// ✗ bad
// These are same as `foo(1, 2, 3);`
foo.call(undefined, 1, 2, 3);
foo.apply(undefined, [1, 2, 3]);
foo.call(null, 1, 2, 3);
foo.apply(null, [1, 2, 3]);
// These are same as `obj.foo(1, 2, 3);`
obj.foo.call(obj, 1, 2, 3);
obj.foo.apply(obj, [1, 2, 3]);
// ✓ good
foo(1, 2, 3);
obj.foo(1, 2, 3):
// ✗ bad
function handle(options) {
options = options || {};
// ...
}
// ✓ good
function handle(options = {}) {
// ...
}
// ✗ bad
function handle(options = {}, name) {
// ...
}
// ✓ good
function handle(name, options = {}) {
// ...
}
arguments.caller
和arguments.callee
,在JavaScript的新版本中它们已被弃用,同时在 ECMAScript 5的严格模式下,它们也是被禁用的。
// ✗ bad
function factorial(n) {
return !(n > 1) ? 1 : arguments.callee(n - 1) * n;
}
// ✓ good
function factorial(n) {
return !(n > 1) ? 1 : factorial(n - 1) * n;
}
// ✗ bad
[1, 2, 3].map(function (x) {
const y = x + 1;
return x * y;
});
// ✓ good
[1, 2, 3].map((x) => {
const y = x + 1;
return x * y;
});
// ✗ bad
[1, 2, 3].map(number => `A string containing the ${number}.`);
// ✓ good
[1, 2, 3].map((number) => `A string containing the ${number}.`);
// ✗ bad
[1, 2, 3].map((number) => {
const nextNumber = number + 1;
`A string containing the ${nextNumber}.`;
});
// ✓ good
[1, 2, 3].map(number => `A string containing the ${number}.`);
// ✗ bad
const x = function () {
foo();
}.bind(bar);
const x = (() => {
foo();
}).bind(bar);
const x = (() => {
this.foo();
}).bind(bar);
const x = function () {
(function () {
this.foo();
}());
}.bind(bar);
const x = function () {
function foo() {
this.bar();
}
}.bind(baz);
// ✓ good
const x = function () {
this.foo();
}.bind(bar);
const x = function (a) {
return a + 1;
}.bind(foo, bar);
// ✗ bad
function foo() {
const that = this;
return function () {
console.log(that);
};
}
// ✓ good
function foo() {
const self = this;
return function () {
console.log(self);
};
}
// ✓ best
function foo() {
return () => console.log(self);
}
该规则发现以下方法的回调函数,然后检查return语句的使用。
Array.from
Array.prototype.every
Array.prototype.filter
Array.prototype.find
Array.prototype.findIndex
Array.prototype.map
Array.prototype.reduce
Array.prototype.reduceRight
Array.prototype.some
Array.prototype.sort
// ✓ good
[1, 2, 3].map((x) => {
const y = x + 1;
return x * y;
});
// ✓ good
[1, 2, 3].map((x) => x + 1);
// ✗ bad
const flat = {};
[[0, 1], [2, 3], [4, 5]].reduce((memo, item, index) => {
const flatten = memo.concat(item);
flat[index] = flatten;
});
// ✓ good
const flat = {};
[[0, 1], [2, 3], [4, 5]].reduce((memo, item, index) => {
const flatten = memo.concat(item);
flat[index] = flatten;
return flatten;
});
// ✗ bad
inbox.filter((msg) => {
const { subject, author } = msg;
if (subject === 'Mockingbird')
return author === 'Harper Lee';
else
return false;
});
// ✓ good
inbox.filter((msg) => {
const { subject, author } = msg;
if (subject === 'Mockingbird')
return author === 'Harper Lee';
return false;
});
// ✗ bad
function Queue(contents = []) {
this.queue = [...contents];
}
Queue.prototype.pop = function () {
const value = this.queue[0];
this.queue.splice(0, 1);
return value;
};
// ✓ good
class Queue {
constructor(contents = []) {
this.queue = [...contents];
}
pop() {
const value = this.queue[0];
this.queue.splice(0, 1);
return value;
}
}
// ✗ bad
const inherits = require('inherits');
function PeekableQueue(contents) {
Queue.apply(this, contents);
}
inherits(PeekableQueue, Queue);
PeekableQueue.prototype.peek = function () {
return this.queue[0];
};
// ✓ good
class PeekableQueue extends Queue {
peek() {
return this.queue[0];
}
}
// ✗ bad
class Jedi {
constructor() {}
getName() {
return this.name;
}
}
// ✗ bad
class Rey extends Jedi {
constructor(...args) {
super(...args);
}
}
// ✓ good
class Rey extends Jedi {
constructor(...args) {
super(...args);
this.name = 'Rey';
}
}
constructor-super, no-this-before-super
// ✗ bad
class Dog extends Animal {
constructor () {
this.legs = 4;
}
}
// ✗ bad
class Dog extends Animal {
constructor () {
this.legs = 4;
super();
}
}
// ✓ good
class Dog extends Animal {
constructor () {
super();
this.legs = 4;
}
}
// ✗ bad
const reg = /abc def/;
// ✓ good
const reg = /abc {3}def/;
// ✗ bad
RegExp('[a-z');
// ✓ good
RegExp('[a-z]');
// ✗ bad
/^abc[]/.test('abcdefg');
// ✓ good
/^abc[a-z]/.test('abcdefg');
// ✗ bad
const pattern = /\x1f/;
// ✓ good
const pattern = /\x20/;
no-self-assign, no-self-compare
// ✗ bad
foo = foo;
[a, b] = [a, b];
// ✗ bad
let x = 10;
if (x === x)
x = 20;
no-global-assign, no-shadow-restricted-names
// ✗ bad
Object = null;
undefined = 1;
window = {};
function NaN() {}
try {} catch(eval) {}
// ✗ bad
class Dog {}
Dog = 'Fido';
// ✗ bad
let math = Math();
const json = JSON();
no-dupe-args, no-dupe-keys, no-dupe-class-members, no-duplicate-case, no-duplicate-imports
// ✗ bad
function foo(a, b, a) {
console.log(a, b);
}
// ✗ bad
const obj = {
name: 'alice',
name: 'bob',
'name': 'carol',
};
// ✗ bad
class Dog {
bark () {}
bark () {}
}
// ✗ bad
switch (id) {
case 1:
// ...
case 1:
}
// ✗ bad
import { myFunc1 } from 'module';
import { myFunc2 } from 'module';
// ✗ bad
0
if(0) 0
{0}
f(0), {}
(function anIncompleteIIFE () {});
a && b
a ? b : 0
// ✓ good
a = 0
new C()
delete a.b
void a
a && b()
a ? b() : c()
eval, no-implied-eval, no-script-url
// ✗ bad
let value = eval('obj.' + key);
setTimeout('alert("Hi!");', 100);
setInterval('alert("Hi!");', 100);
execScript('alert("Hi!")');
location.href = 'javascript:void(0)';
// ✓ good
let value = obj[key];
setTimeout(function() {
alert('Hi!');
}, 100);
setInterval(function() {
alert('Hi!');
}, 100);
alert('Hi!');
no-void, no-with, no-labels, no-unused-labels, no-extra-label, no-label-var, no-iterator, [no-proto]
// ✗ bad
throw 'error';
throw 0;
throw undefined;
throw null;
// ✓ good
throw new Error();
throw new Error('error');
const e = new Error('error');
throw e;
// ✗ bad
const {} = foo;
const [] = foo;
const {a: {}} = foo;
const {a: []} = foo;
function foo({}) {}
function foo([]) {}
function foo({a: {}}) {}
function foo({a: []}) {}
// ✓ good
const {a = {}} = foo;
const {a = []} = foo;
function foo({a = {}}) {}
function foo({a = []}) {}
// ✗ bad
// make() returns a new element
// based on the passed in tag name
//
// @param {String} tag
// @return {Element} element
function make(tag) {
// ...
return element;
}
// ✓ good
/**
* make() returns a new element
* based on the passed-in tag name
* @param {String} tag
* @return {Element} element
*/
function make(tag) {
// ...
return element;
}
// ✗ bad
const active = true; // is current tab
// ✓ good
// is current tab
const active = true;
// ✗ bad
function getType() {
console.log('fetching type...');
// set the default type to 'no type'
const type = this.type || 'no type';
return type;
}
// ✓ good
function getType() {
console.log('fetching type...');
// set the default type to 'no type'
const type = this.type || 'no type';
return type;
}
// ✓ good
function getType() {
// set the default type to 'no type'
const type = this.type || 'no type';
return type;
}
// ✗ bad
//is current tab
const active = true;
// ✓ good
// is current tab
const active = true;
// ✗ bad
/**
*make() returns a new element
*based on the passed-in tag name
*/
function make(tag) {
// ...
return element;
}
// ✓ good
/**
* make() returns a new element
* based on the passed-in tag name
*/
function make(tag) {
// ...
return element;
}
// ✓ good
class Calculator extends Abacus {
constructor() {
super();
// FIXME: shouldn't use a global here
total = 0;
}
}
class Calculator extends Abacus {
constructor() {
super();
// TODO: total should be configurable by an options param
this.total = 0;
}
}
// ✗ bad
alert('message');
confirm('Are you sure?');
// ✗ bad
console.log('code info.');
debugger;
// ✓ good
console.warn('This method is deprecated.');
console.error('Circular dependencies!')