Git Product home page Git Product logo

lint-rules's Introduction

@angular-extensions/lint-rules

https://github.com/angular-extensions/lint-rules

npm version npm downloads total npm downloads monthly CircleCI

Description

This repository offers some tslint rules useful for angular projects, see Rules.

Installation / Usage

  • Install the @angular-extensions/lint-rules npm package:
    npm install @angular-extensions/lint-rules --save-dev
    
  • Add @angular-extensions/lint-rules to the extensions list in your tslint.json:
    {
      "extends": [
        "tslint:recommended",
        "@angular-extensions/lint-rules"
      ]
    }
  • Lint your project with
    ng lint
    

Rules

The package includes the following rules:

Rule Description Details Enabled by default?
angular-call-super-lifecycle-method-in-extended-class Enforces the application to call parent lifecycle function e.g. super.ngOnDestroy() when using inheritance within an Angular component or directive. Details yes
angular-rxjs-takeuntil-before-subscribe Enforces the application of the takeUntil operator when calling of subscribe within an Angular component or directive. Details yes

angular-call-super-lifecycle-method-in-extended-class

This rule tries to avoid memory leaks and other problems in angular components and directives by ensuring that a life-cycle method, e.g. ngOnDestroy(){}, overriding its parent implementation must call the parent implementation with super.ngOnDestroy().

Example

This should trigger an error:

class MyClass {
    ngOnDestroy() {
        const a = 5;
    }
}
@Component({
  selector: 'app-my'
})
class MyComponent2 extends MyClass {

    ngOnDestroy() {
    ~~~~~~~~~~~            call to super.ngOnDestroy() is missing
        const b = 6;
    }
}

while this should be fine:

class MyClass {
    ngOnDestroy() {
        const a = 5;
    }
}
@Component({
  selector: 'app-my'
})
class MyComponent extends MyClass {

    ngOnDestroy() {
        super.ngOnDestroy();
        const b = 6;
    }
}

@Component({
  selector: 'app-my2'
})
class MyComponent2 {
    ngOnDestroy() {
        const b = 6;
    }
}

angular-rxjs-takeuntil-before-subscribe

This rule tries to avoid memory leaks in angular components and directives when calling .subscribe() without properly unsubscribing by enforcing the application of the takeUntil(this.destroy$) operator before the .subscribe() as well as before certain operators (shareReplay without refCount: true) and ensuring the component implements the ngOnDestroy method invoking this.destroy$.next(). All classes with a @Component or @Directive decorator and all their parent classes will be checked.

Example

This should trigger an error:

@Component({
  selector: 'app-my',
  template: '<div>{{k$ | async}}</div>'
})
class MyComponent {
      ~~~~~~~~~~~    component containing subscribe must implement the ngOnDestroy() method

    
    k$ = a.pipe(shareReplay(1));
                ~~~~~~~~~~~~~~   the shareReplay operator used within a component must be preceded by takeUntil

    someMethod() {
        const e = a.pipe(switchMap(_ => b)).subscribe();
                                            ~~~~~~~~~      subscribe within a component must be preceded by takeUntil
    }
}

while this should be fine:

@Component({
  selector: 'app-my',
  template: '<div>{{k$ | async}}</div>'
})
class MyComponent implements SomeInterface, OnDestroy {
    private destroy$: Subject<void> = new Subject<void>();

    k$ = a.pipe(takeUntil(this.destroy$), shareReplay(1));

    someMethod() {
        const e = a.pipe(switchMap(_ => b), takeUntil(this.destroy$)).subscribe();
    }

    ngOnDestroy() {
      this.destroy$.next();
    }
}

Further reading

Contributors

Note: this project is based on work in cartant/rxjs-tslint-rules#107

Development

Clone the repository and install the dependencies with npm install.

Note: using the build artifacts with npm link does not work correctly, since there will be a mismatch between the typescript version used by the consumer code and the typescript version used by the lint rules code. To test the package in a project, run

npm run build
cd dist
npm install --production

Then copy the content of the /dist folder (including the node_modules folder) into node_modules/@angular-extensions/lint-rules in the consumer project.

Publish

To publish the package, run

npm run publish-package

lint-rules's People

Contributors

dependabot[bot] avatar kreuzerk avatar macjohnny avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

lint-rules's Issues

ng lint with take(1) should be legit

I'm using a large project where we are use different type of operator in order to unsubscribe from observable.

for example take(1) operator or the Subscription
in this case this lint rules show me error.

I think this package is good but I can't use it as there more then one option for unsubscribe.
I be-live I'm not the only one how want to use it but can't as mention above.

Thanks

pipe(takeUntil()) is not recognized

running ng lint, shows the following error or lack of takeUntil

ERROR: /var/www/html/pwa-vimbo-new/src/@vimbo/directives/vimbo-perfect-scrollbar/vimbo-perfect-scrollbar.directive.ts:176:18 - subscribe within a component must be preceded by takeUntil

Captura de Tela_selecionar área_20200312083958

but you can see that in the line where the error is being accused, there is a pipe with takeUntil
Captura de Tela_selecionar área_20200312083942

Enviroment

Angular CLI: 8.3.23
Node: 10.15.3
OS: linux x64
Angular: 8.2.14
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... platform-server, router, service-worker

Package                                    Version
--------------------------------------------------------------------
@angular-devkit/architect                  0.803.23
@angular-devkit/build-angular              0.803.23
@angular-devkit/build-optimizer            0.803.20
@angular-devkit/build-webpack              0.803.23
@angular-devkit/core                       8.3.23
@angular-devkit/schematics                 8.3.23
@angular/cdk                               8.2.3
@angular/cli                               8.3.23
@angular/flex-layout                       8.0.0-beta.27
@angular/material                          8.2.3
@angular/material-moment-adapter           8.2.3
@ngtools/webpack                           8.3.23
@nguniversal/express-engine                8.2.6
@nguniversal/module-map-ngfactory-loader   8.2.6
@schematics/angular                        8.3.23
@schematics/update                         0.803.23
rxjs                                       6.5.4
typescript                                 3.5.3
webpack                                    4.39.2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.