eslint + prettierを使ってvscodeでコードの構文を確認する方法を紹介します。現在、私たちのプロジェクトは主に tsx に基づいており、以下の設定手順も tsx に基づいています。
非常に簡単です。2 つのプラグインは次のようになります。
コード形式と構文チェックだけの場合: vscode 設定ファイルに関連する設定を記述するだけです ~
{
"editor.formatOnSave": true, // 值设置为true时,每次保存的时候自动格式化;值设置为false时,按Command+Shift+F 格式化代码
"editor.defaultFormatter": "esbenp.prettier-vscode", //默认格式化规则(可以按语言配置)
"editor.detectIndentation": false, //vscode默认 启用时根据文件类型自动设置tabsize的选项,设置为false
"editor.tabSize": 2, //tab的缩进为2
// 添加ts语法验证支持
"eslint.validate": [
"javascript",
"javascriptreact",
"typescript",
"typescriptreact"
],
// vscode 中附带了ts的最新稳定版本,也可以通过该配置项 配置要使用的ts
"typescript.tsdk": "node_modules/typescript/lib",
// 每次保存的时候将代码按eslint进行修复
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
// 此处还可配置tslint stylelint,如下:
//"source.fixAll.tslint": true,
// "source.fixAll.stylelint": true
},
}