SonarSource / SonarTS

Static code analyzer for TypeScript
GNU Lesser General Public License v3.0
761 stars 53 forks source link

"Functions should not have too many parameters" rule : false positive warnings with Angular DI #893

Closed jdussouillez closed 4 years ago

jdussouillez commented 4 years ago

I'm using Sonar to analyze my TS code in my Angular app.

I recently enabled the typescript:S107 (RSPEC-107) rule "Functions should not have too many parameters" but I have some false positive warnings because of Angular Dependency Injection.

Is there a solution to keep this rule for function parameters but ignore it for constructors ? Or even better, ignore it for constructors if the class has some annotations ? (for Angular, Injectable, Component, Pipe, Directive).

SonarQube version: 8.0.0.29455
Rule key: typescript:S107

Reproducer

import {Component} from "@angular/core";
/* ... */

@Component({
    selector: "app-test",
    templateUrl: "./test.component.html",
    styleUrls: ["./test.component.scss"]
})
export class TestComponent {

    constructor(
        protected readonly testService0: Test0Service,
        protected readonly testService1: Test1Service,
        protected readonly testService2: Test2Service,
        protected readonly testService3: Test3Service,
        protected readonly testService4: Test4Service,
        protected readonly testService5: Test5Service,
        protected readonly testService6: Test6Service,
        protected readonly testService7: Test7Service,
        protected readonly testService8: Test8Service
        /* .... */) {
    }
}

Current behavior

I get a warning because the constructor has more parameters than allowed by the rule.

Expected behavior

No warning because there're no other solutions to inject dependencies in this Angular component.

vilchik-elena commented 4 years ago

Hi, This rule is now part of SonarJS analyzer, and this ticket https://github.com/SonarSource/SonarJS/issues/1347 should fix your problem.