IDE 配置文件

56 阅读10分钟

VsCode

基础设置

  1. 保存格式化:设置 → 搜索 Format On Save → 勾选
  2. Tab Size:设为 2

EditorConfig

安装扩展 EditorConfig for VS Code;部分 IDE(如 WebStorm)内置支持,其余需独立安装。
文档:editorconfig.org

VS Code 支持的属性

配置描述
[*]匹配全部文件,匹配除 / 路径分隔符外的任意字符串
charsetlatin1、utf-8、utf-8-bom、utf-16be、utf-16le(不推荐 utf-8-bom)
end_of_linelf、cr、crlf
indent_size缩进列数;indent_style 为 tab 时用 tab_width
indent_stylespace | tab
insert_final_newlinetrue 时文件以换行结尾
trim_trailing_whitespace删除行首尾空格

换行符: lf(\n)、cr(\r)、crlf(\r\n)

根目录 .editorconfig

# https://editorconfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

.vscode/extensions.json

{
  "recommendations": ["editorconfig.editorconfig"]
}

Prettier

格式化代码,支持 json、js;ts 可用 cjs。本质将代码转为 AST 再格式化。
Playground:prettier.io/playground/

安装

yarn add --dev --exact prettier
npm i prettier -D

配置文件格式

优先级从高到低:.prettierrc.js / .prettierrc.cjs.prettierrc.json.prettierrc.yaml / .yml.prettierrc.tomlpackage.jsonprettier 字段。

项目示例 .prettierrc.cjs

module.exports = {
  printWidth: 120,
  useTabs: false,
  tabWidth: 2,
  semi: false,
  singleQuote: true,
  jsxSingleQuote: true,
  arrowParens: "avoid",
  bracketSpacing: true,
  trailingComma: "none",
  jsxBracketSameLine: false,
};

配置项详解

缩进与宽度

配置项默认值说明
printWidth80每行最大字符数,超过则换行
tabWidth2每个缩进级别的空格数
useTabsfalse是否用 Tab 代替空格

分号

配置项默认值说明
semitrue是否在语句末尾添加分号

引号

配置项默认值说明
singleQuotefalse是否使用单引号(JSX 默认仍可能用双引号,见 jsxSingleQuote)
jsxSingleQuotefalseJSX 中是否使用单引号
quoteProps"as-needed"对象属性引号:as-needed / consistent / preserve

尾随逗号 trailingComma

说明
none不加
es5对象、数组等 ES5 有效位置
all含函数参数、调用等

括号

配置项默认值说明
bracketSpacingtrue{ foo: bar } vs {foo: bar}
bracketSameLinefalse多行 JSX/HTML 的 > 是否放在最后一行末尾
arrowParens"always"always 总加括号;avoid 单参数可省略

jsxBracketSameLine 已废弃,Prettier 2.4+ 使用 bracketSameLine

配置项默认值说明
singleAttributePerLinefalseJSX/HTML/TSX 是否每属性一行

换行符与文件

配置项默认值说明
endOfLine"lf"lf / crlf / cr / auto
insertPragmafalse是否在文件顶插入 @format
requirePragmafalse是否仅格式化带 @format 的文件
embeddedLanguageFormatting"auto"auto / off

HTML / JSX

配置项默认值说明
htmlWhitespaceSensitivity"css"css / strict / ignore
vueIndentScriptAndStylefalseVue 中是否缩进 script/style 内容

其它格式

配置项默认值说明
proseWrap"preserve"Markdown:always / never / preserve
parser自动推断如 typescript、json、css

Overrides 示例

module.exports = {
  singleQuote: true,
  overrides: [
    { files: "*.json", options: { parser: "json", tabWidth: 2 } },
    { files: ["*.yml", "*.yaml"], options: { tabWidth: 2, singleQuote: false } },
  ],
};

文档示例(另一套取值,作对照)

module.exports = {
  printWidth: 120,
  tabWidth: 2,
  semi: true,
  singleQuote: true,
  jsxBracketSameLine: true,
  bracketSameLine: true,
  bracketSpacing: false,
  arrowParens: "avoid",
  trailingComma: "es5",
};

.prettierignore

语法同 .gitignore。项目示例:

node_modules/**
dist/**
public/**
doc/**
*.md
coverage/**

常见忽略:node_modulesdistbuildcoveragepackage-lock.json*.min.js

CLI 与 scripts

npx prettier --check .
npx prettier --write .
npx prettier --write "src/**/*.{ts,tsx,js,jsx,json,css,less,md}"

package.json

"lint:prettier": "prettier --write --loglevel warn \"src/**/*.{js,ts,json,tsx,css,less,scss,stylus,html,md}\""
  • --write:写回文件
  • --loglevel warn:仅警告级日志
{
  "scripts": {
    "format": "prettier --write \"src/**/*.{ts,tsx,js,jsx,json,css,less,md}\"",
    "format:check": "prettier --check \"src/**/*.{ts,tsx,js,jsx,json,css,less,md}\""
  }
}

VS Code

根目录 .vscode/settings.jsonprettier.requireConfig: true 时须有 Prettier 配置文件,否则保存可能不格式化;false 时无配置文件也可用扩展默认(有配置文件则以文件为准)。

{
  "search.exclude": {
    "/node_modules": true,
    "dist": true,
    "pnpm-lock.yaml": true
  },
  "files.autoSave": "onFocusChange",
  "editor.formatOnSave": true,
  "editor.formatOnType": true,
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "[javascript]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },
  "[javascriptreact]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },
  "[typescript]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },
  "[typescriptreact]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },
  "[json]": { "editor.defaultFormatter": "vscode.json-language-features" },
  "[html]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },
  "[markdown]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },
  "javascript.validate.enable": false
}

插件:editor.defaultFormatter": "esbenp.prettier-vscode"editor.formatOnSave": true(与上可合并使用)。


ESLint

识别并报告 ECMAScript/JavaScript 模式,使代码一致并减少错误。
文档:eslint.org/docs/latest…
Node:^12.22.0、^14.17.0 或 >=16.0.0(v9 见下)

依赖描述
eslint-config-standardStandard 预设
eslint-config-airbnbAirbnb 规范
@typescript-eslint/parserTS 解析器
@typescript-eslint/eslint-pluginTS 规则
eslint-plugin-reactReact 规则
eslint-plugin-importimport 规则

standard / airbnb 二选一安装:

pnpm add eslint eslint-config-standard eslint-plugin-import -D

React:

pnpm add eslint-plugin-react-hooks eslint-plugin-react -D

TypeScript:

pnpm add @typescript-eslint/eslint-plugin @typescript-eslint/parser -D

.eslintrc.js 示例:

module.exports = {
  env: { browser: true, es2021: true, node: true },
  extends: [
    "standard",
    "eslint:recommended",
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:import/recommended",
    "plugin:react-hooks/recommended",
  ],
  parser: "@typescript-eslint/parser",
  parserOptions: {
    ecmaFeatures: { jsx: true },
    ecmaVersion: "latest",
    sourceType: "module",
  },
  plugins: ["react", "@typescript-eslint", "react-hooks", "import"],
  rules: {},
};

其它插件

  1. eslint-plugin-jsx-a11y — JSX 可访问性
  2. eslint-plugin-react-refresh — refresh() 相关,加快 HMR
  3. eslint-plugin-promise — Promise 用法
  4. eslint-plugin-node — Node 环境陷阱与安全 API
plugins: [
  "jsx-a11y",
  "react-refresh",
  "promise",
  "node",
],

eslint-friendly-formatter — 自定义报告样式:

module.exports = {
  formatter: require("eslint-friendly-formatter"),
};

eslint-import-resolver-alias — 路径别名:

module.exports = {
  settings: { "import/resolver": { alias: true } },
};

ESLint 配置字段说明

roottrue 为本文件根配置,不再向父目录查找(monorepo 子包常用)。

parserOptions — 传给解析器:sourceType: "module" 按 ES 模块解析;旧项目可能用 babel-eslint,新项目常见 @babel/eslint-parser(与 Babel 版本对齐)。

env — 运行环境全局,如 browseres6 / es2021

extends — 继承规则集,如 eslint:recommendedplugin:vue/recommended(需 eslint-plugin-vue)。

rules — 覆盖 extends;off / warn / error(或 0 / 1 / 2)。

文档若仍写 babel-eslint,新工程可迁 @babel/eslint-parser

常用 rules 备忘

严重级别:0/off 关闭,1/warn 警告,2/error 错误。

环境与调试(随构建环境切换)

  • no-console:生产 错误,开发关闭
  • no-debugger:同上

Vue(eslint-plugin-vue)

规则级别与要点说明
vue/attribute-hyphenation关闭不强制模板属性 kebab-case
vue/max-attributes-per-line错误单行最多 10 个属性;多行每行 1 个
vue/singleline-html-element-content-newline关闭不强制单行标签内换行
vue/multiline-html-element-content-newline关闭不强制多行标签内换行
vue/html-closing-bracket-newline关闭不强制闭合 > 单独换行
vue/no-v-html关闭允许 v-html(注意 XSS)
vue/html-self-closing关闭不强制自闭合
vue/require-default-prop关闭不强制 prop 默认值
vue/require-prop-types关闭不强制 prop 类型

类、函数、this 与构造

规则级别与要点说明
accessor-pairs错误getter/setter 成对
constructor-super错误派生类须先 super
consistent-this错误,别名 that统一 this 别名
curly错误,multi-line多行或易歧义时须 {}
new-cap错误new 配大写构造;允许直接调大写函数
new-parens错误new Foo() 须括号
no-class-assign错误禁止对类名再赋值
no-const-assign错误禁止对 const 再赋值
no-dupe-class-members错误禁止类成员重名
no-dupe-args错误禁止参数重名
no-func-assign错误禁止对函数声明再赋值
no-inner-declarations错误,仅 functions禁止嵌套块内 function 声明
no-new-symbol错误禁止 new Symbol()
no-obj-calls错误禁止把 Math/JSON/Reflect 当函数调
no-useless-constructor错误禁止空壳构造
no-this-before-super错误super() 前禁止 this/super
wrap-iife错误,anyIIFE 须括号包裹

比较、控制流与异常

规则级别与要点说明
eqeqeq错误,always,null 忽略用 === / !==
no-dupe-keys错误对象禁止重复键
no-duplicate-case错误switch 禁止重复 case
no-fallthrough错误case 贯穿须注释或改写
no-self-compare错误禁止 x === x
no-sequences错误禁止逗号表达式副作用
no-unmodified-loop-condition错误循环条件变量须在循环内更新
no-unneeded-ternary错误,defaultAssignment: false禁止可简化三元
no-unreachable错误return/throw 后禁止死代码
no-unsafe-finally错误finally 里禁止 return/throw 破坏完成
no-throw-literal错误禁止 throw 非 Error
valid-typeof错误typeof 与合法字符串比较
use-isnan错误用 Number.isNaN / isNaN
yoda错误,never禁止尤达条件

危险写法与全局污染

规则级别与要点说明
no-eval错误禁止 eval
no-implied-eval错误禁止 setTimeout(string) 等
no-with错误禁止 with
no-extend-native错误禁止改内置原型
no-native-reassign错误禁止对只读全局再赋值
no-new-wrappers错误禁止 new String/Number/Boolean
no-iterator错误禁止 iterator
no-labels错误,allowLoop/Switch: false限制 label
no-label-var错误label 勿与变量冲突
no-lone-blocks错误禁止无意义单独块

正则与字符串

规则级别与要点说明
no-control-regex关闭关闭时不检查控制字符
no-empty-character-class错误禁止 [] 空类
no-invalid-regexp错误RegExp 参数须合法
no-regex-spaces错误正则字面量禁止多空格
no-useless-escape关闭不报告多余转义

变量、require 与模块

规则级别与要点说明
no-delete-var错误禁止 delete 变量
no-redeclare错误禁止同作用域重复声明
no-undef错误禁止未声明标识符
no-undef-init错误禁止 var x = undefined
no-unused-vars错误,vars: all,args: after-used未使用变量;参数从后往前算
no-var错误禁止 var
prefer-const错误未再赋值 let 改 const
one-var错误,initialized: never已初始化变量分开声明
global-require警告倾向顶层 require
no-new-require错误禁止 new require(...)
no-path-concat错误路径用 path API
no-new-object错误倾向 {}
no-array-constructor警告倾向 []

赋值、返回与解构

规则级别与要点说明
no-return-assign错误,except-parensreturn 右侧禁止易混赋值
no-self-assign错误禁止 a = a
no-ex-assign错误禁止对 catch(e) 的 e 再赋值
prefer-destructuring错误,object/array: false不强制解构
no-empty-pattern错误禁止空解构模式

其它逻辑与风格

规则级别与要点说明
camelcase错误,properties: always属性名驼峰
dot-location错误,property. 在下一行属性侧
no-extra-boolean-cast错误禁止冗余 !!
no-extra-parens错误,functions函数相关多余括号
no-floating-decimal错误禁止 .5、5.
no-octal错误禁止八进制字面量
no-octal-escape错误禁止八进制转义
no-sparse-arrays错误禁止稀疏数组
no-multi-str错误禁止反斜杠续行多行字符串
no-multiple-empty-lines错误,max: 1连续空行最多 1
no-trailing-spaces错误行尾无空白
eol-last错误文件末尾换行
no-mixed-spaces-and-tabs错误勿混 tab 与空格
no-multi-spaces错误禁止无意义多空格
no-irregular-whitespace错误禁止非常规空白
no-unexpected-multiline错误避免 ASI 意外多行
no-whitespace-before-property错误.prop 前无空格
no-underscore-dangle错误默认禁止首尾 _
no-useless-call错误禁止可普通调用的 call/apply
no-useless-computed-key错误禁止可点访问的计算键
generator-star-spacing错误function* 的 * 两侧空格
yield-star-spacing错误,bothyield* 的 * 两侧空格

格式与空格(Stylistic)

规则级别与要点说明
arrow-spacing错误=> 两侧空格
block-spacing错误,always块内侧空格
brace-style错误,1tbsOne True Brace Style
comma-dangle错误,never禁止尾随逗号
comma-spacing错误逗号前无、后有空格
comma-style错误,last逗号在行尾
indent错误,2 空格,SwitchCase: 2缩进
key-spacing错误对象键值冒号空格
keyword-spacing错误关键字两侧空格
object-curly-spacing错误,always{} 内侧空格
array-bracket-spacing错误,never[] 内侧不空格
quotes错误,单引号,avoidEscape默认单引号
semi错误,never不使用分号
semi-spacing错误分号前后空格
space-before-blocks错误,always{ 前空格
space-before-function-paren错误,neverfunction( 前无空格
space-in-parens错误,never() 内侧不空格
space-infix-ops错误中缀运算符两侧空格
space-unary-ops错误new/typeof 要空格;++ 不加
spaced-comment错误,always// /* 后须空格
template-curly-spacing错误,never${} 内不空格
operator-linebreak错误默认行尾断行
padded-blocks错误,never块内首尾不空行

其它

规则级别与要点说明
radix错误parseInt 须写进制

迁回 .eslintrc.js 时可将「级别与要点」还原为数组;no-console / no-debugger 的环境判断须写在配置里而非静态表。

ESLint v9

Node >=18.18.0。

pnpm create @eslint/config@latest

初始化选项:语法检查 → ES modules → 选框架 → TypeScript Yes → Browser+Node → 配置格式 JavaScript → npm 安装 Yes。

与 Prettier 协作

作用
eslint-config-prettier关闭与 Prettier 冲突的 ESLint 规则
eslint-plugin-prettier将 Prettier 作为 ESLint 规则运行
pnpm install eslint-plugin-prettier eslint-config-prettier -D
npm i eslint-config-prettier -D

.eslintrc / .eslintrc.js

module.exports = {
  extends: [
    "some-other-config",
    "plugin:prettier/recommended", // 或仅 "prettier",须放在 extends 最后
  ],
};

.vscode/settings.json

{
  "eslint.enable": true,
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  }
}

Git hooks(lint-staged + husky)

{
  "lint-staged": {
    "*.{ts,tsx,js,jsx,json,css,less,md}": ["prettier --write"]
  }
}