Airbnb JavaScript Style Guide는 Airbnb에서 사용하는 자바스크립트 코딩 스타일 가이드로, 자바스크립트 코드의 가독성, 일관성, 유지보수성을 향상시키기 위한 목적으로 작성되었습니다. 협업을 위해서는 필수라고 생각합니다.
이 가이드를 따르는 이유는 다음과 같습니다.
따라서 Airbnb JavaScript Style Guide를 따르면 더 나은 코드를 작성하고, 일관성을 유지하며, 보안 문제를 예방할 수 있습니다. 또한 이를 따르면 다른 개발자들과 적극적인 커뮤니케이션을 할 수 있어 협업과 유지보수가 더욱 수월해집니다.
(cf. https://github.com/airbnb/javascript 한글판 - https://github.com/ParkSB/javascript-style-guide )
{
"parserOptions": {
"ecmaVersion": 2020,
"sourceType": "module" // import/export
"sourceType": "default" // require
},
"env": {
"browser": true,
"es6": true,
"node": true
},
"rules": {
"no-console": "off",
"max-classes-per-file": ["error", 10]
},
"extends": ["airbnb", "plugin:prettier/recommended"]
// 꼭 배열의 맨 마지막이여야 모든 중복 규칙을 비활성화 시킬 수 있습니다.
}
plugins
우선 plugin 종류는 여러 가지 있는데, 예를 들어
extends
rules
env
ESLint 공식문서
/* 사용하실때는 주석을 삭제해주세요 ~! */
{
"bracketSpacing": true, // 객체 리터럴에서 괄호에 공백 삽입 여부
"singleQuote": true, // 쌍따옴표
"semi": true, // 세미콜론 자동
"useTabs": false, // 탭x
"tabWidth": 2, // 탭 사이즈 2
"trailingComma": "all",
"printWidth": 80, // 80자 이상 넘어가면 자동 넘어감
"editor.formatOnSave": false,
"[javascript]": {
"editor.formatOnSave": true // vscode에서 저장 시, 포맷팅
}
}
Object사용 시, 선언되어있는 값이라면 value입력없이 바로 사용.
value 없이 사용하는 값들은 상단에 value를 지정해주는 것들은 하단에 배치
const anakinSkywalker = 'Anakin Skywalker';
const lukeSkywalker = 'Luke Skywalker';
// bad
const obj = {
episodeOne: 1,
twoJediWalkIntoACantina: 2,
lukeSkywalker,
episodeThree: 3,
mayTheFourth: 4,
anakinSkywalker,
};
// good
const obj = {
lukeSkywalker,
anakinSkywalker,
episodeOne: 1,
twoJediWalkIntoACantina: 2,
episodeThree: 3,
mayTheFourth: 4,
};
// really bad
function handleThings(opts) {
// \* 함수 인수를 변경하면 안됩니다.
옵션이 거짓이면 원하는 개체로 설정되지만 미묘한 버그가 발생할 수 있습니다.
opts = opts || {};
// ...
}
// still bad
function handleThings(opts) {
if (opts === void 0) {
opts = {};
}
// ...
}
// good
function handleThings(opts = {}) {
// ...// 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 + 1}.`);
// good
[1, 2, 3].map((number) => {
const nextNumber = number + 1;
return `A string containing the ${nextNumber}.`;
});
// good
[1, 2, 3].map((number, index) => ({
[index]: number,
}));
// No implicit return with side effects
function foo(callback) {
const val = callback();
if (val === true) {
// Do something if callback returns true
}
}
let bool = false;
// bad
foo(() => bool = true);
// good
foo(() => {
bool = true;
});// bad
const foo = a ? a : b;
const bar = c ? true : false;
const baz = c ? false : true;
const quux = a != null ? a : b;
// good
const foo = a || b;
const bar = !!c;
const baz = !c;
const quux = a ?? b;
// bad
if (test)
return false;
// good
if (test) return false;
// good
if (test) {
return false;
}
// bad
function foo() { return false; }
// good
function bar() {
return false;
}
\\* ... for multiline comments.// 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
*/
function make(tag) {
// ...
return
}