tslint不排除angular项目中的node_modules

问题描述 投票:3回答:1

我尝试执行以下操作以从node_modules中排除tslint。但他们都没有解决这个问题。

如果是因为我调用node_modules文件夹的方式,我很困惑。

第一次尝试 - 在tsconfig.jsontsconfig-aot.json中添加了以下内容

"exclude": [
    "../node_modules/**/*",
    "./node_modules/**/*",
    "node_modules",
    "node_modules/**/*.ts",
    "**/node_modules/**/*",
    "**/node_modules/**"
]

第二次尝试 - 在exclude的每个项目部分添加了.angular-cli.json

"lint": [{
        "project": "../../../tsconfig.json",
        "exclude": "./node_modules/*" // also tried **/node_modules/**/*
    },
    {
        "project": "../../../tsconfig-aot.json",
        "exclude": "./node_modules/*"
    }
],

第三次尝试 - 用tslint-cli选项执行--exclude

tslint --exclude node_modules --project tsconfig-aot.json
tslint --exclude node_modules --project tsconfig.json

仍然没有变化。每当我做yarn webpack:build时,我仍然会从node_modules获取这些警告。

同样在tsconfig.jsontsconfig-aot.json,它已经有"skipLibCheck": true

UPDATE

我安装了导致错误的npm模块angular-select2-component。添加了tslint堆栈跟踪。

WARNING in ./node_modules/angular-select2-component/index.ts
[1, 41]: file should end with a newline

 @ ./src/main/webapp/app/home/home.module.ts 13:34-70
 @ ./src/main/webapp/app/app.module.ts
 @ ./src/main/webapp/app/app.main.ts

WARNING in ./node_modules/angular-select2-component/src/select2.component.ts
[22, 24]: Type boolean trivially inferred from a boolean literal, remove type annotation
[43, 22]: missing whitespace
[44, 40]: missing whitespace
[46, 14]: missing whitespace
[54, 45]: missing whitespace
[67, 14]: missing whitespace
[61, 32]: missing whitespace
[79, 18]: missing whitespace
[59, 51]: " should be '
[54, 33]: == should be ===
[35, 45]: missing whitespace
[44, 11]: missing whitespace
[54, 11]: missing whitespace
[61, 15]: missing whitespace
[30, 1]: Consecutive blank lines are forbidden
[13, 15]: The selector of the component "Select2Component" should be named kebab-case and include dash (https://angular.io/styleguide#style-05-02)

 @ ./node_modules/angular-select2-component/index.ts 6:9-43
 @ ./src/main/webapp/app/home/home.module.ts
 @ ./src/main/webapp/app/app.module.ts
 @ ./src/main/webapp/app/app.main.ts

WARNING in ./node_modules/angular-select2-component/src/custom-input.ts
[59, 2]: file should end with a newline
[20, 47]: missing whitespace

 @ ./node_modules/angular-select2-component/src/select2.component.ts 25:21-46
 @ ./node_modules/angular-select2-component/index.ts
 @ ./src/main/webapp/app/home/home.module.ts
 @ ./src/main/webapp/app/app.module.ts
 @ ./src/main/webapp/app/app.main.ts
angular typescript tslint
1个回答
2
投票

Solution one:

尝试明确指定应从哪个目录开始检查。在我看来它是:

"scripts": {
    "lint": "tslint \"src/**/*.ts\"",
},

只有当您的项目结构合理时,此解决方案才有效:

package.json
some-other.conf.js
src/here_is_app_code

Solution two:

{
    "extends": [
      "tslint:recommended"
    ],
    "linterOptions": {
        "exclude": [
            "node_modules"
        ]
    }
}

更多信息可以在this PR找到

Solution three:

tslint \"./**/*.ts\" -e \"node_modules\"

-e--exclude的缩写,与this PR一起介绍。


0
投票

同时为tslint标记--type-check可以从node_modules产生错误只是在运行命令时删除标志--type-check

例如

tslint -e \"node_modules/**/*.ts\" -p tsconfig.json -c tslint.json

代替

tslint -e \"node_modules/**/*.ts\" -p tsconfig.json -c tslint.json --type-check

0
投票

问题不在于你的tsconfig。实际上,它应该默认排除node_modules。问题是angular-select2-component模块。它没有正确构建。

大多数打字稿模块的构建使得main中有一个package.json,它指向一个JS文件(项目的编译输出)。他们在types中也有一个package.json条目,它指向根类型定义文件.d.ts

您导入的模块(angular-select2-component)将其main条目设置为类型脚本文件index.ts。它没有types条目。这并非完全不受支持,它“可以工作”。 Typescript基本上将此模块视为第二个源文件夹。这意味着当您编译项目时,它也在编译angular-select2-component。如果你从tsconfig.json中排除它是无关紧要的,因为它必须被编译。如果您尝试排除其中一个源文件但是由另一个源文件导入,则会产生相同的结果。这也是skipLibCheck被忽略的原因。

所以没有办法从编译中排除angular-select2-component,因为它是必需的。您可以使用@ nickolay的解决方案,将其从linting中排除,但将其留待编译。请注意,他对“解决方案3”的实现使用了globs,看起来你在尝试“解决方案3”时看起来并不像是在使用globs。

© www.soinside.com 2019 - 2024. All rights reserved.