typescript 节点eslint添加前缀'src',而不考虑tsconfig设置

g6ll5ycj  于 2022-11-18  发布在  TypeScript
关注(0)|答案(1)|浏览(119)

我正在运行client文件夹中的npx eslint --fix src。如果在我的代码库中指定了相对导入,我希望git钩子运行npx eslint命令,将其转换为绝对路径。然而,要转换的路径的前缀是src/(尽管我在compilerOptions中指定了baseUrl)。
示例:

import { UserContext } from '../../services/userContext'; 

goes to 

import { UserContext } from 'src/services/userContext';

I want:

import { UserContext } from 'services/userContext';

我的文件夹结构看起来像这样(假设我使用节点,并希望为服务器和客户端的mono repo):

repo/
  .eslintrc
  client/
    tsconfig.json
    src/
    <all my client code and components>
  server/

我的**.eslintrc**:

// .eslintrc
{
  "env": {
    "node": true,
    "commonjs": true
  },
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint", "import", "no-relative-import-paths"],
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:import/recommended",
    "plugin:import/errors",
    "plugin:import/warnings",
    "plugin:import/typescript",
  ],
  "settings": {
    "import/resolver": {
      "typescript": {
        "project": [
      "server/tsconfig.json",
      "client/tsconfig.json"
    ]
      },
      "node": {
        "moduleDirectory": [
          "client/src/",
          "client/node_modules",
          "server/src",
          "server/node_modules"
      ],
        "extensions": [".ts", ".tsx"]
      }
    }
  },
  "rules": {
    "import/order": [
      "warn",
      {
        "alphabetize": {
          "caseInsensitive": true,
          "order": "asc"
        },
        "groups": [["builtin", "external"]],
        "newlines-between": "always"
      }
    ],
    "import/no-unresolved": [1, {"commonjs": false, "amd": false}],
    "@typescript-eslint/no-explicit-any": "off",
    "no-relative-import-paths/no-relative-import-paths": [
      "warn",
      { "allowSameFolder": true }
    ]
  },
  "

我的tsconfig.json

{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": "src",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "isolatedModules": true,
    "jsx": "react-jsx",
    "lib": ["dom", "dom.iterable", "esnext"],
    "noEmit": true,
    "noFallthroughCasesInSwitch": true,
    "noImplicitAny": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strict": true,
    "target": "es5",
  },
  "include": ["src"]
}

是否存在任何看似错误的配置?

3yhwsihp

3yhwsihp1#

有意思。我在跑步:

~/repo/client/
$ npx eslint --fix src

然后将自动更正的绝对导入路径作为前缀/src/。

~/repo/client/src
$ npx eslint --fix .

这并没有发生,路径看起来很好。我想知道一种方法,使幂等,而不必担心我们从哪里运行eslint。

更新

这实际上不起作用。为什么我不能从根目录运行eslint --fix .

相关问题