![](/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.