网站搜索

typescript - 即使禁用,Tslint仍然强制执行 no-submodule-imports 规则


我正在尝试配置我的 tslint,以允许来自 Parse 库 (https://www.npmjs.com/package/parse-js) 的子模块,这样我就可以使用 parse/react-native,但它不起作用。

我已经尝试在我的 tslint 中同时添加 "no-submodule-imports": [true, "parse", "parse/react-native"] 和 "no-submodule-imports": false,但我仍然收到以下错误:

ERROR: src/App.tsx:2:19 - Submodule import paths from this package are disallowed; import from the root instead

node_modules 中此子模块在 @types/parse 的声明是:

declare module "parse/node" {
    export = Parse;
}

declare module "parse" {
    import * as parse from "parse/node";
    export = parse
}

declare module "parse/react-native" {
    import * as parse from "parse/node";
    export = parse
}

代码确实能正常工作,但如果我运行 tslint 就会出现那个错误。我尝试了不同的组合,问题依然存在。

这是我的 tslint.json 文件的样子:

{
  "extends": [
    "tslint:latest",
    "tslint-react",
    "tslint-react-a11y",
    "tslint-config-prettier"
  ],
  "no-submodule-imports": [true, "parse", "parse/react-native"],
  "import-blacklist": false,
  "defaultSeverity": "error",
  "rules": {
    "ban": {
      "options": [
        ["_", "extend", "use object spread: { ...a, ...b }"],
        ["_", "isNull", "use plain JS: == null"],
        ["_", "isDefined", "use plain JS: != null"],
        ["Object", "assign", "use object spread: { ...a, ...b }"],
        ["Object", "getOwnPropertyNames", "use Object.keys()"],
        ["describe", "only", "should not be committed to repo"],
        ["it", "only", "should not be committed to repo"],
        ["test", "only", "should not be committed to repo"]
      ]
    },
    "no-object-literal-type-assertion": false,
    "ordered-imports": [
      true,
      {
        "import-sources-order": "any",
        "named-imports-order": "any"
      }
    ],
    "no-any": true,
    "interface-over-type-literal": false,
    "linebreak-style": {
      "options": ["LF"]
    },
    "quotemark": [true, "single"],
    "jsx-boolean-value": ["never"],
    "member-access": [true],
    "no-console": false,
    "no-implicit-dependencies": false,
    "no-invalid-this": {
      "options": ["check-function-in-method"]
    },
    "interface-name": false,
    "max-classes-per-file": false,
    "jsx-no-lambda": false,
    "no-trailing-whitespace": true,
    "no-this-assignment": [
      true,
      {
        "allow-destructuring": true
      }
    ],
    "no-unnecessary-callback-wrapper": false,
    "no-unnecessary-initializer": true,
    "object-literal-sort-keys": false,
    "prefer-conditional-expression": false,
    "variable-name": {
      "options": [
        "allow-leading-underscore",
        "allow-pascal-case",
        "ban-keywords",
        "check-format"
      ]
    },
    "member-ordering": [
      true,
      "private-before-public",
      "static-before-instance",
      "variables-before-functions"
    ],
    "trailing-comma": [
      true,
      {
        "multiline": "always",
        "singleline": "never",
        "esSpecCompliant": true
      }
    ]
  },
  "jsRules": {
    "object-literal-sort-keys": false
  },
  "linterOptions": {
    "exclude": [
      "/**/packages/newrelic-webpack-plugin/embedded.js",
      "/**/coverage/**/*",
      "/**/*.json",
      "/**/build/**/*"
    ]
  }
}

提前感谢您的帮助。

答案#1

我也遇到过这个情况。看起来对于一个 Angular 项目,这条规则需要放在 src 文件夹里的 tslint 文件中。