简体   繁体   English

使用ESlint整理React + Typescript项目

[英]Linting React+Typescript project with ESlint

I read in an article that the TypeScript core team explaining ESLint has a more performant architecture than TSLint. 我在一篇文章中读到,解释ESLint的TypeScript核心团队比TSLint具有更高的性能。 And on the other hand @typescript-eslint/parser makes a more convenient AST which works the best alongside @typescript-eslint/eslint-plugin . 另一方面, @typescript-eslint/parser使AST更方便,与@typescript-eslint/eslint-plugin一起使用效果最佳。

Now I have a feeling that my tslintrc file has not a good setup of plugins and extensions. 现在,我感觉到我的tslintrc文件没有很好的插件和扩展程序设置。

  • Is it good to follow Airbnb rules in tsx or just go with the standard rules? 遵循tsx Airbnb规则还是只遵循标准规则是件好事吗?
  • What should be the sequence of including extends and plugins that they do not override each other and get the best linting and auto-fixing out of it? 包括extendsplugins它们不会相互覆盖并从中获得最好的棉绒和自动修复的顺序应该是什么?
  • The app created with CRA yarn create react-app myapp --typescript and have not changed anything in the tsconfig.json 使用CRA yarn create react-app myapp --typescript ,并且未更改tsconfig.json任何tsconfig.json

This is my .eslintrc.js 这是我的.eslintrc.js

module.exports = {
    env: {
        browser: true,
        es6: true,
        node: true
    },
    parser: "@typescript-eslint/parser",
    extends: [
        "plugin:@typescript-eslint/recommended",
        "react-app",
        "plugin:prettier/recommended",
        "prettier",
        "prettier/react"
    ],
    globals: {
        Atomics: "readonly",
        SharedArrayBuffer: "readonly"
    },
    parserOptions: {
        ecmaFeatures: {
            tsx: true
        },
        ecmaVersion: 2018,
        sourceType: "module"
    },
    plugins: ["@typescript-eslint", "react", "prettier"],
    rules: {
        indent: ["error", "tab"],
        "linebreak-style": ["error", "unix"],
        quotes: ["error", "single"],
        semi: ["error", "always"]
    }
};

// and these parts in the VSCode setting
    "eslint.validate": [
        "javascript",
        "javascriptreact",
        { "language": "typescript", "autoFix": true },
        { "language": "typescriptreact", "autoFix": true }
    ],
   "prettier.eslintIntegration": true,

Also if someone knows a good setting in a project in GitHub/Gist that would be appritiated. 另外,如果有人知道GitHub / Gist中某个项目的良好设置,那将是值得推荐的。

Is it good to follow Airbnb rules in tsx or just go with the standard rules? 遵循tsx中的Airbnb规则还是只遵循标准规则是件好事吗?

Depends heavily on your preferences. 在很大程度上取决于您的偏好。 There is no absolute. 没有绝对的。 Look at the rules that are activated in each of them and decide for yourself. 查看每个规则中激活的规则,然后自己决定。 AirBnB's style guide is more on the restrictive side, some may like it and some may not. AirBnB的样式指南更多是限制性的,有些人可能喜欢,有些人可能不喜欢。

What should be the sequence of including extends and plugins that they do not override each other and get the best linting and auto-fixing out of it? 包括extends和plugins且它们不会相互覆盖并从中获得最好的棉绒和自动修复的顺序应该是什么?

The order doesn't matter, as long as the rules are not doing the same thing - each plugin/extension will only turn on/off it's own provided rules, and again it's up to you to decide which one you choose. 只要规则不做相同的事情,顺序就没有关系-每个插件/扩展名只会打开/关闭它自己提供的规则,而且由您自己决定选择哪个规则。 For example, I try to keep all style related rules to prettier, and let ESLint deal with lint related rules. 例如,我尝试使所有与样式相关的规则保持漂亮,并让ESLint处理与皮棉相关的规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM