![](/img/trans.png)
[英]Angular 2 - Receiving TSLint errors in my node_modules folder
[英]tslint not excluding node_modules in angular project
我试图执行排除以下node_modules
从tslint
。 但他们都没有解决这个问题。
如果是因为我调用node_modules
文件夹的方式,我很困惑。
第一次尝试 - 在tsconfig.json
和tsconfig-aot.json
添加了以下tsconfig-aot.json
"exclude": [
"../node_modules/**/*",
"./node_modules/**/*",
"node_modules",
"node_modules/**/*.ts",
"**/node_modules/**/*",
"**/node_modules/**"
]
第二次尝试 - 在.angular-cli.json
为每个项目部分添加了exclude
"lint": [{
"project": "../../../tsconfig.json",
"exclude": "./node_modules/*" // also tried **/node_modules/**/*
},
{
"project": "../../../tsconfig-aot.json",
"exclude": "./node_modules/*"
}
],
第三次尝试 - 使用--exclude
选项执行tslint-cli
tslint --exclude node_modules --project tsconfig-aot.json
tslint --exclude node_modules --project tsconfig.json
仍然没有变化。 每当我做网络yarn webpack:build
,我仍然会从node_modules获取这些警告。
同样在tsconfig.json
和tsconfig-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
尝试明确指定应从哪个目录开始检查。 在我看来它是:
"scripts": {
"lint": "tslint \"src/**/*.ts\"",
},
只有当您的项目结构合理时,此解决方案才有效:
package.json
some-other.conf.js
src/here_is_app_code
{
"extends": [
"tslint:recommended"
],
"linterOptions": {
"exclude": [
"node_modules"
]
}
}
更多信息可以在这个PR中找到
tslint \"./**/*.ts\" -e \"node_modules\"
-e
是--exclude
的缩写,与此PR一起介绍。
另外flag --type-check
for tslint可以从node_modules
产生错误只需在运行命令时删除flag --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
问题不在于你的tsconfig。 实际上,它应该默认排除node_modules
。 问题是angular-select2-component
模块。 它没有正确构建。
大多数打字稿模块的构建使得package.json
中有一个main
指向JS文件(项目的编译输出)。 它们在package.json
也有一个types
条目,它指向根类型定义文件.d.ts
。
要导入的模块( angular-select2-component
)的main
条目设置为index.ts
,一个TypeScript文件。 它没有types
入口。 这并非完全不受支持,它“可以工作”。 Typescript基本上将此模块视为第二个源文件夹。 这意味着在编译项目时,它还会编译angular-select2-component
。 如果将它从tsconfig.json
排除它并不重要,因为它必须被编译。 如果您尝试排除其中一个源文件但是由另一个源文件导入,则会产生相同的结果。 这也是忽略skipLibCheck
原因。
所以没有办法从编译中排除angular-select2-component
,因为它是必需的。 您可以使用@ nickolay的解决方案,将其从linting中排除,但将其留待编译。 请注意,他对“解决方案3”的实现使用了globs,看起来你在尝试“解决方案3”时看起来并不像是在使用globs。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.