以前我们的项目编码风格一直比较混乱。前不久,我们在项目中使用了Babel,吃上了ES2015,顺便配上了ESLint。按照ESLint的rules,我们整理出一份自己的JavaScript代码规范。
npm install --save-dev eslint eslint-config-rgui
然后创建.eslintrc
文件,内容如下:
{
"extends": "rgui",
"env": {
"browser": true,
"node": true
}
}
具体可以参考ESLint配置。
indent, no-tabs, no-mixed-spaces-and-tabs
// ✗ bad
function () {
console.log('Hello');
}
// ✗ bad
function () {
——console.log('Hello');
}
// ✓ good
function () {
console.log('Hello');
}
\n
,禁止使用Windows换行符\r\n
✎
// ✗ 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 */ };
1tbs
(one true brace style)格式,允许单行模式。✎
// ✗ 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(); }
multi-or-nest
方式,多行时使用大括号,单行时省略大括号。✎
// ✗ 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();
nonblock-statement-body-position
// ✗ bad
if (foo) return;
while (cond) doSomething();
for (let i = 0; i < count; i++) doSomething();
// ✓ good
if (foo)
return;
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,
'>5,
};
// ✓ good
const good = {
foo: 3,
bar: 4,
'>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) {
// ...
}
相关阅读:
本文来自网易实践者社区,经作者赵雨森授权发布。