cartant / rxjs-tslint-rules

TSLint rules for RxJS
https://cartant.github.io/rxjs-tslint-rules/
MIT License
372 stars 22 forks source link

Angular CLI gotcha when an RxJS function is used only in a unit test #14

Open balassy opened 7 years ago

balassy commented 7 years ago

I have the following rxjs.imports.ts file in an Angular 4 project:

import 'rxjs/add/observable/of';

I have the following rule in the tslint.json file:

"rxjs-add": {
  "options": [{
    "file": "./src/rxjs.imports.ts"
  }],
  "severity": "error"
}

The following error is thrown when I run tslint:

ERROR: (rxjs-add) C:/Git/MyProject/src/rxjs.imports.ts[3, 1]: 
Unused patched observable in ./src/rxjs.imports.ts: of

I am using Observable.of(...) in my code, however only in one single .spec.ts file. It seems that the error is thrown during linting the application files, because if I remove the following block from the lint section of the .angular-cli.json file, the error disappears:

{
  "project": "src/tsconfig.app.json",
  "exclude": "**/node_modules/**"
},

Do you have any recommendation on how can I fix this, or this is just another gotcha that would be useful to add to the README?

Thank you!

cartant commented 7 years ago

I've (only quickly) read your issue and would like to confirm whether or not you've read and followed the Angular CLI recommendations in the README.md.

balassy commented 7 years ago

Yes, I did, thank you. If I understand correctly, it is about the case when only an application file is using an operator, but not any test file, and your recommendation helps fixing the problem when linting the test files. This issue is about the opposite direction: only a test file is using an operator, but any application file, and the linting of the application files is failing.

cartant commented 7 years ago

Off the top of my head, I cannot think of an ideal solution to this. The linting of the application and test files uses the same tslint.json configuration and, therefore, the same central file of imports.

A hacky solution would be to create an unused.ts file that will be matched in the linting of the application, but not the tests and include in that file a function that 'uses' whichever operators are not used in the application. Of course, that means that said operators will be linked into the application even though they are not really needed. Not ideal.

Alternatively, you could look into what's involved in getting Angular's CLI to use a different tslint.json file for the test linting, but that's probably something that cannot be changed without ejecting. If tests could be linted using their own tslint.json - like the e2e tests - this would be easy to solve, but they cannot be, as they are in the same directory as the application files (so creating a local, relative tslint.json won't work).

On the plus side, the good news is that this sort of annoyance will be a thing of the past with the lettable/pipeable operators in RxJS 5.5.

balassy commented 6 years ago

Thanks for your reply, Nicholas.

kamok commented 6 years ago

@balassy Any stuff in tsconfig? I had a few unused variable rules in there which threw that error.