ReactiveX / rxjs-tslint

TSLint rules targeting RxJS
MIT License
309 stars 41 forks source link

imports not added/changed #41

Closed Ristaaf closed 6 years ago

Ristaaf commented 6 years ago

Since importing operators (and observables) in rjxs before 6 was prototype based, it was not needed to import them in each file, thus in our project we did all the

import "rxjs/add/operator/filter";

in our app.module.ts file and no imports in the rest of the application. So now when we run the upgrade tooling, we don't get any of the new imports where they need to be since the upgrade only search and replaces old imports with new not taking into consideration that the imports may need to be in other files.

I would love it if there "change old import to new ones" added a new import for each operator and observable used in a specific file, even if it was not imported before in that file. That would save me (and I assume several more people have done this since there was no point in adding the same operator to the Observable prototype again and again) a lot of work. (In my case I have around 500 files to manually fix where I have used this pattern)

mgechev commented 6 years ago

You should get these operators auto imported. Can you share a minimal demo and details with your setup?

Ristaaf commented 6 years ago

Sure, a stupid example which does not really do anything, but shows the problem and has the same kind of architecture as our real application:

  1. on a new @angular/cli project
  2. add these imports to app.module.ts:
    import 'rxjs/add/operator/take';
    import 'rxjs/add/observable/from';
  3. write this as the app.component.ts file:
    
    import { Component } from '@angular/core';
    import { Observable } from 'rxjs/Observable';

@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'app'; a$: Observable = Observable.from([true]);

constructor(){ this.a$.take(1).subscribe(); } }

Ristaaf commented 6 years ago

Actually this works, after reading other closed issues I realized that you have to do the migration before upgrading rxjs to 6 in package.json, if I do it while I still have rxjs 5 it works fine. So I am closing this