Prettier ESLint 结合 ts

Prettier ESLint 结合 ts

1.第一步

  npx eslint --init

```javascript
// How would you like to use ESLint? · style
// ✔ What type of modules does your project use? · esm
// ✔ Which framework does your project use? · react
// ✔ Does your project use TypeScript? · No / Yes
// ✔ Where does your code run? · browser, node
// ✔ How would you like to define a style for your project? · guide
// ✔ Which style guide do you want to follow? · standard-with-typescript
// ✔ What format do you want your config file to be in? · JavaScript

选择生成的 .eslintrc 配置项

module.exports = {
  env: {
    browser: true,
    es2021: true,
    node: true
  },
  extends: [
    'plugin:react/recommended',
    'standard-with-typescript'
  ],
  overrides: [
  ],
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module'
  },
  plugins: [
    'react'
  ],
  rules: {
  }
}

第2步

修改配置

module.exports = {
  env: {
    browser: true,
    es2021: true,
    node: true
  },
  extends: [
    'plugin:react/recommended',
    'standard-with-typescript'
  ],
  'settings': {
    'react': {
      'version': '18.2.0'
    }
  },
  overrides: [
  ],
  globals: {
    '$': true,
    'process': true,
    'Promise': true,
    '__dirname': true
  },
  parserOptions: {
    ecmaVersion: 'latest',
    sourceType: 'module',
    'ecmaFeatures': true,
    project: './tsconfig.json',
    tsconfigRootDir: __dirname
  },

  plugins: [
    'react'
  ],
  rules: {
    'semi': [2, 'always'], // 语句强制分号结尾
    'semi-spacing': [0, { 'before': false, 'after': false }], // 分号前后空格
    '@typescript-eslint/semi': 0,
    // "quotes": [0, "single"], // 引号类型 `` "" ''
    '@typescript-eslint/strict-boolean-expressions': 0,
    '@typescript-eslint/consistent-type-definitions': 0,
    'no-mixed-spaces-and-tabs': [2, false], // 禁止混用tab和空格
    'no-spaced-func': 2, // 函数调用时 函数名与()之间不能有空格
    '@typescript-eslint/prefer-optional-chain': 0
  }
};

第三步

下载vscode 插件 ESLint和 Prettier ESLint
在这里插入图片描述
在这里插入图片描述

第四步

// yarn add -D prettier@^2.5.1 eslint@^8.7.0 @typescript-eslint/parser@^5.0.1 typescript@^4.4.4

第5步

配置你当前项目下的.vscode/settings.json

{
  "eslint.run": "onType", // 运行linter 的时间,onSave(保存后)或onType(输入时),默认为onType
  "eslint.options": {
    "extensions": [".js", ".vue", ".jsx", ".tsx"] //指定vscode的eslint所处理的文件的后缀
  },
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": false // 保存时按eslint自动修复
  },

  "javascript.validate.enable": false,
  "editor.defaultFormatter": "rvest.vs-code-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",
  "[javascriptreact]": {
    "editor.defaultFormatter": "rvest.vs-code-prettier-eslint"
  }
}

第六步

重新启动vscode

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值