簡體   English   中英

tslint不排除angular項目中的node_modules

[英]tslint not excluding node_modules in angular project

我試圖執行排除以下node_modulestslint 但他們都沒有解決這個問題。

如果是因為我調用node_modules文件夾的方式,我很困惑。

第一次嘗試 - 在tsconfig.jsontsconfig-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.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

解決方案一:

嘗試明確指定應從哪個目錄開始檢查。 在我看來它是:

"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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM