自动格式化&校验代码(eslint+prettier)

一、VScode编辑器安装2个插件
插件名是: ESLint + Prettier ESLint
二、在项目根目录下新建.eslintrc.prettierrc .eslintignore文件
注意:项目中也需要npm安装eslint插件等。

.eslintrc文件:

注意:不同项目的eslint配置不同,需跟项目关联!如下是:taro3-react项目配置规则

{
  "extends": ["taro/react"],
  "rules": {
    "react/jsx-uses-react": "off",
    "react/react-in-jsx-scope": "off",
    "import/no-commonjs": "off"
  }
}

.prettierrc文件:

{
  "tabWidth": 2,
  "singleQuote": true,
  "semi": false,
  "trailingComma": "es5",
  "arrowParens": "always",
  "endOfLine": "auto",
  "printWidth": 100
}

.eslintignore文件:设置eslint忽略的文件目录等。

lib/
三、在项目根目录中新建.vscode/settings.json配置文件,配置如下:
{
    "workbench.iconTheme": "vscode-icons",
    "eslint.alwaysShowStatus": true,
    "eslint.format.enable": true,
    "eslint.run": "onSave",
    "eslint.lintTask.enable": true,
    "vetur.useWorkspaceDependencies": true,
    "vsicons.dontShowNewVersionMessage": true,
    //   "workbench.colorTheme": "Monokai",
    "eslint.codeAction.showDocumentation": {
      "enable": false
    },
    "eslint.execArgv": null,
    "editor.defaultFormatter": "rvest.vs-code-prettier-eslint",
    // prettier-eslint 添加
    "editor.formatOnPaste": false, // required
    "editor.formatOnType": false, // required
    "editor.formatOnSave": true, // optional
    "editor.formatOnSaveMode": "file", // required to format on save
    "files.autoSave": "onFocusChange",
    "typescript.updateImportsOnFileMove.enabled": "always",
    "security.workspace.trust.untrustedFiles": "open",
    "eslint.codeActionsOnSave.rules": null,
    "explorer.confirmDelete": false,
    "prettier.printWidth": 125, // optional but recommended
    "editor.codeActionsOnSave": { // 【重要】设置编辑器保存时,自动格式化所有代码
      "source.fixAll": true
    }
  }
  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值