eslint-config-21st
这个包提供了 21st digital 的 ESLint 可共享配置 支持 Prettier。 它基于 JavaScript 标准样式。
Install
如果您还没有 package.json
文件,请使用 npm init
创建一个。
然后我们需要安装配置所需的一切:
npx install-peerdeps --dev @21st-digital/eslint-config
您可以在 package.json 中看到现在有一大堆 devDependencies。
在项目目录的根目录中创建一个 .eslintrc
文件(它应该位于 package.json 所在的位置)。 您的 .eslintrc
文件应如下所示:
{
"extends": ["@21st-digital"]
}
提示:您也可以将此对象放入 package.json
属性 "eslintConfig"
下: . 这会在您的项目中减少一个文件。
- You can add two scripts to your package.json to lint and/or fix:
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint . --fix"
},
- Now you can manually lint your code by running
npm run lint
and fix all fixable issues with npm run lint:fix
. You probably want your editor to do this though.
VS Code
您可能希望您的编辑器为您检查和修复。 以下是 VS Code 的说明:
- Install the ESLint package
- Now we need to setup some VS Code settings via
Code/File
→ Preferences
→ Settings
. It's easier to enter these settings while editing the settings.json
file, so click the Open (Open Settings) icon in the top right corner:
// These are all my auto-save configs
"editor.formatOnSave": true,
// turn it off for JS and JSX, we will do this via eslint
"[javascript]": {
"editor.formatOnSave": false
},
"[javascriptreact]": {
"editor.formatOnSave": false
},
// show eslint icon at bottom toolbar
"eslint.alwaysShowStatus": true,
// tell the ESLint plugin to run on save
"editor.codeActionsOnSave": {
"source.fixAll": true
},
// Optional BUT IMPORTANT: If you have the prettier extension enabled for other languages like CSS and HTML, turn it off for JS since we are doing it through Eslint already
"prettier.disableLanguages": ["javascript", "javascriptreact"],
第一次尝试对文件进行 lint 后,您可能需要单击右下角的“ESLint”并在警报窗口中选择“Allow Everywhere”。
最后,您通常需要重新启动 VS 代码。 他们说你不需要,但在我重新启动之前它对我没有用。
eslint-config-21st
This package provides 21st digital's ESLint Shareable Config with support for Prettier. It is based on JavaScript Standard Style.
Install
If you don't already have a package.json
file, create one with npm init
.
Then we need to install everything needed by the config:
npx install-peerdeps --dev @21st-digital/eslint-config
You can see in your package.json there are now a big list of devDependencies.
Create a .eslintrc
file in the root of your project's directory (it should live where package.json does). Your .eslintrc
file should look like this:
{
"extends": ["@21st-digital"]
}
Tip: You can alternatively put this object in your package.json
under the property "eslintConfig":
. This makes one less file in your project.
- You can add two scripts to your package.json to lint and/or fix:
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint . --fix"
},
- Now you can manually lint your code by running
npm run lint
and fix all fixable issues with npm run lint:fix
. You probably want your editor to do this though.
VS Code
You probably want your editor to lint and fix for you. Here are the instructions for VS Code:
- Install the ESLint package
- Now we need to setup some VS Code settings via
Code/File
→ Preferences
→ Settings
. It's easier to enter these settings while editing the settings.json
file, so click the Open (Open Settings) icon in the top right corner:
// These are all my auto-save configs
"editor.formatOnSave": true,
// turn it off for JS and JSX, we will do this via eslint
"[javascript]": {
"editor.formatOnSave": false
},
"[javascriptreact]": {
"editor.formatOnSave": false
},
// show eslint icon at bottom toolbar
"eslint.alwaysShowStatus": true,
// tell the ESLint plugin to run on save
"editor.codeActionsOnSave": {
"source.fixAll": true
},
// Optional BUT IMPORTANT: If you have the prettier extension enabled for other languages like CSS and HTML, turn it off for JS since we are doing it through Eslint already
"prettier.disableLanguages": ["javascript", "javascriptreact"],
After attempting to lint your file for the first time, you may need to click on 'ESLint' in the bottom right and select 'Allow Everywhere' in the alert window.
Finally you'll usually need to restart VS code. They say you don't need to, but it's never worked for me until I restart.