如何在 eslint 中使用 atom-typescript 或替代

Ste*_*lin 5 typescript eslint prettier

Atom 软件包 - 已安装

  • 原子打字稿
  • 仅语言打字稿语法
  • 短绒
  • linter-eslint
  • linter-stylelint
  • linter-ui-default
  • 更漂亮的原子

更漂亮 - 设置:

  • ESLint 集成 = 选中
  • Stylelint 集成 = 选中
  • EditotConfig 集成 = 未选中

.eslintrc

{
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaVersion": 2020,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true,
      "modules": true,
      "impliedStrict": true,
      "experimentalObjectRestSpread": true
    }
  },
  "settings": {
    "react": {
      "pragma": "React",
      "version": "detect"
    },
    "import/resolver": {
      "node": {
        "extensions": [".js", ".jsx", ".ts", ".tsx"],
        "paths": ["./"]
      },
      "babel-module": {}
    }
  },
  "extends": [
    "plugin:react/recommended",
    "airbnb",
    "plugin:prettier/recommended",
    "plugin:@typescript-eslint/recommended",
    "prettier/react",
    "prettier/@typescript-eslint",
    "plugin:import/typescript"
  ],
  "plugins": ["react", "@typescript-eslint", "prettier", "mocha"],
  "env": {
    "es6": true,
    "browser": true,
    "jest": true,
    "mocha": true,
    "node": true
  }
}
Run Code Online (Sandbox Code Playgroud)

配置文件

{
  "compileOnSave": false,
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": "./",
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": true,
    "jsx": "react",
    "module": "esnext",
    "moduleResolution": "node",
    "noEmit": true,
    "noImplicitAny": true,
    "outDir": "./dist/out-tsc",
    "resolveJsonModule": true,
    "sourceMap": true,
    "strict": true,
    "target": "esnext",
    "downlevelIteration": true,
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "skipLibCheck": true,
    "esModuleInterop": true
  },
  "include": [
    "src",
    "src/__tests__"
  ],
  "exclude": [
    "node_modules",
    "public"
  ]
}
Run Code Online (Sandbox Code Playgroud)

SomeLabel.jsx

  • 正确显示错误 - 表单标签必须与控件相关联
import React, { FC } from 'react';
const SomeLabel: FC = () => {
  return (
    <label>hello world</label>
  );
};
export default SomeLabel;
Run Code Online (Sandbox Code Playgroud)

SomeLabel.tsx

  • 不显示错误!
import React, { FC } from 'react';
const SomeLabel: FC = () => {
  return (
    <label>hello world</label>
  );
};
export default SomeLabel;
Run Code Online (Sandbox Code Playgroud)

jsx 文件正确地承担了 eslint 错误,而打字稿文件扩展名则没有。这对 Visual Studio 代码自动起作用,但对 atom 不起作用。我怎样才能让原子正常工作?

谢谢你

Pet*_*lka 3

我遇到了同样的问题,GitHub 存储库上的问题linter-eslint对我有所帮助。

您需要告诉 linter-eslint 它实际上知道如何处理 TypeScript 文件。

您可以通过在此处添加 TypeScript 文件 (source.ts) 的范围来做到这一点:

linter-eslint 设置的屏幕截图

上面的屏幕截图来自包的设置页面linter-eslint(首选项 > 包 > linter-eslint)。

我添加source.tssource.tsx重新加载了窗口(查看 > 开发人员 > 重新加载窗口),Atom 开始显示我的 lint 错误。