在 React 17.0 使用 ESLint 分析工具並套用 Airbnb 程式碼風格

做法

安裝 eslint 依賴套件。

1
2
3
4
npm i eslint \
eslint-plugin-react \
eslint-config-airbnb \
--save-dev

新增 .eslintrc.js 檔。

1
2
3
4
5
6
7
8
9
10
11
12
module.exports = {
extends: 'airbnb',
plugins: [
'react',
],
parserOptions: {
ecmaVersion: '2020', // or above
},
rules: {
//
},
};

修改 package.json 檔。

1
2
3
4
5
6
7
8
9
{
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"lint": "eslint src"
}
}

執行檢查。

1
npm run lint