tslint not excluding node_modules in angular project
Asked Answered
O

3

15

I tried to perform the following to exclude node_modules from tslint. But none of them resolved the issue.

I am confused if it is because of the way I call the node_modules folder.

First attempt - Added the following in tsconfig.json and tsconfig-aot.json

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

Second attempt - Added exclude to each project section in .angular-cli.json

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

Third attempt - Executed tslint-cli with --exclude option

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

Still no change. Whenever I do yarn webpack:build, I am still getting these warnings from node_modules.

Also in tsconfig.json and tsconfig-aot.json, it already has "skipLibCheck": true

UPDATE

I installed the npm module angular-select2-component which was causing the error. Added the tslint stacktrace.

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
Ortensia answered 13/3, 2018 at 8:48 Comment(1)
Could you give us the stack trace of your linting please ?Eklund
C
9

Solution one:

Try to specify explicitly from which directory should start checking. On my end it is:

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

This solution works only if your project is well structured ie:

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

Solution two:

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

More information can be found in this PR

Solution three:

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

-e is an abbreviation for --exclude, introduced with this PR.

Curkell answered 13/3, 2018 at 8:56 Comment(4)
Could you please add some explanations to solutions 2 and 3?Ortensia
Find out more info in the links provided in the edited answer.Curkell
And...? Still have problem with tslint?Curkell
Hi .. I had tried all the solutions, but surprisingly none worked. I ditched the node_module which caused the issue and took a different module.Ortensia
C
3

The problem is not your tsconfig. In fact, it should exclude node_modules by default. The problem is the angular-select2-component module. It is not built correctly.

Most typescript modules are built such that there is a main in the package.json which points to a JS file (the compiled output of the project). They also have a types entry in package.json which points to the root type definitions file .d.ts.

The module you are importing (angular-select2-component) has its main entry set to index.ts, a TypeScript file. It has no types entry. This is not entirely unsupported, it "can work". Typescript will basically treat this module as a second source folder. This means that when you are compiling your project it is also compiling angular-select2-component. It won't matter if you exclude it from tsconfig.json because it has to be compiled. This would be the same result you would get if you tried to exclude one of your source files but it was imported by another source file. This is also why skipLibCheck is ignored.

So there is no way to exclude angular-select2-component from compilation because it is needed. You might be able to use @nickolay's solution which excludes it from linting but leaves it in for compilation. Notice that his implementation of "solution 3" uses globs and it doesn't look like you were using globs in your attempt at "solution 3".

Clay answered 27/8, 2019 at 22:19 Comment(1)
Also, it appears this lack of packaging is an open issue (github.com/godbasin/angular-select2/issues/32) but it has been for over a year so :shrug:Clay
R
0

Also flag --type-check for tslint can produce errors from node_modules just remove flag --type-check when you ran command

For example

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

instead of

tslint -e \"node_modules/**/*.ts\" -p tsconfig.json -c tslint.json --type-check
Rolf answered 27/8, 2019 at 19:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.