Skip to content

TypeScript相关 #17

@sophister

Description

@sophister

WebStorm 中使用 ESlint 校验 typescript ts/tsx

备注:我使用的 webstorm版本是:

WebStorm 2018.3.5
Build #WS-183.5912.23, built on February 27, 2019

webstorm 中,即使打开了IDE自带的 eslint校验,即使eslint能找到我们对应的 .eslintrc文件,依然很多配置的规则没有被应用到 ts/tsx文件的校验,比如我们的 .eslintrc 文件内容如下:

{
  "extends": [
    "airbnb",
    "plugin:@typescript-eslint/recommended"
  ],
  "parser": "@typescript-eslint/parser",
  "ecmaFeatures": {
    "classes": true
  },
  "plugins": [
    "react-hooks",
    "@typescript-eslint"
  ],
  "rules": {
    "react/jsx-filename-extension": ["error", { "extensions": [".js", ".jsx", ".ts", ".tsx"] }],
    "react/jsx-indent":["error",4],
    "global-require": "off",
    "indent": ["error", 4,{ "SwitchCase": 1 }],
    "no-unused-expressions":0,           
    "class-methods-use-this":0,           
    "no-underscore-dangle":0,            
    "arrow-body-style":["error", "always"], 
    "max-len":["error",200],
    "no-plusplus":["error",{"allowForLoopAfterthoughts": true}],
    "prefer-destructuring": 0,
    "guard-for-in":"off",
    "react/jsx-indent-props":["error",4],
    "react/require-default-props": 0,     
    "react/no-array-index-key":0,
    "react/jsx-boolean-value":["error","always"],
    "react/forbid-prop-types":0,
    "react/prop-types":0,
    "react/sort-comp":0,
    "react/destructuring-assignment":0,
    "object-curly-newline": ["error", {
        "ObjectExpression": { "consistent": true,"minProperties": 5 },
        "ObjectPattern": { "consistent": true, "minProperties": 5},
        "ImportDeclaration": { "consistent": true,  "minProperties": 5 },
        "ExportDeclaration": { "consistent": true, "minProperties": 5 }
    }],
    "react-hooks/rules-of-hooks": "error",
    "react-hooks/exhaustive-deps": "warn",
    "import/no-extraneous-dependencies": [
        "error", {"packageDir": "./"}
    ],
    "react/jsx-one-expression-per-line":  0,
    "import/prefer-default-export": 0,
    "no-shadow": 0,
    "@typescript-eslint/explicit-function-return-type": ["error", {
        "allowExpressions": true,
        "allowTypedFunctionExpressions": true
    }],
    "@typescript-eslint/camelcase": 0,
    "@typescript-eslint/no-non-null-assertion": 0
  },
  "globals": {
    "requestAnimationFrame": true,
    "cancelAnimationFrame": true,
    "isNaN": true
  },
  "settings": {
    "import/core-modules": ["common","components"],
    "import/resolver": {
      "node": {
        "moduleDirectory": [
          "./node_modules/",
          "./src/"
        ],
        "extensions": [
            ".js",
            ".jsx",
            ".ts",
            ".tsx"
        ]
      }
    },
    "import/parsers": {
      "@typescript-eslint/parser": [".ts", ".tsx", ".js", ".jsx"]
    }
  }
}

还需要单独在 webstorm 里配置一下,默认 eslint 不会去处理 .ts/.tsx的文件,具体设置见这个帖子:https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000225170/comments/360000332879

为了防止打不开原文,截图保留如下:

image

内容如下:

To anyone else searching for this, you must go to Help > Find Action and type "registry". Click the first result (Registry...) and scroll down to eslint.additional.file.extensions. Add js,ts.

The issue for me is that IntelliJ/Webstorm only does this automatically if you're using typescript-eslint-parser and eslint-plugin-typescript. However, those packages are now @typescript-eslint/parser and @typescript-eslint/eslint-plugin.

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions