codelyzer icon indicating copy to clipboard operation
codelyzer copied to clipboard

Cannot disable template rules

Open crushjz opened this issue 5 years ago • 8 comments

Describe the bug

Can't disable the template-cyclomatic-complexity rule.

Context and configuration

I'm using tslint in an Angular project.

tsconfig.json setting: "template-cyclomatic-complexity": [true, 10],

Following this readme (https://github.com/mgechev/codelyzer/blob/master/README.md#disable-a-rule-that-validates-template-or-styles) in order to disable a rule, I have to write the tslint:disable:rule in the ts file that references the template.

When I execute the ng lint command without the tslint:disable:rule comment, I can see this output:

ERROR: /project/src/app/module/components/temp/temp.component.html:7:2 - The cyclomatic complexity exceeded the defined limit (cost '10'). Your template should be refactored.
ERROR: /project/src/app/module/components/temp/temp.component.html:48:6 - The cyclomatic complexity exceeded the defined limit (cost '10'). Your template should be refactored.
ERROR: /project/src/app/module/components/temp/temp.component.html:59:16 - The cyclomatic complexity exceeded the defined limit (cost '10'). Your template should be refactored.

If I add the tslint:disable:rule comment, this is the output:

ERROR: /project/src/app/module/components/temp/temp.component.html:7:2 - The cyclomatic complexity exceeded the defined limit (cost '10'). Your template should be refactored.

Seems that the rule is not entirely disabled.

To Reproduce

ng lint on the root of the project.

Expected behavior

The rule should be disabled entirely.

Code

/* tslint:disable:template-cyclomatic-complexity */
@Component({
  selector: 'temp',
  templateUrl: './temp.component.html',
  styleUrls: ['./temp.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})

Environment

  • OS: MacOS
  • Node.js version: 8.11.3
  • npm version: 6.9.0
  • Angular version: "@angular/core": "^7.2.0"
  • tslint version: "tslint": "^5.15.0" "tslint-angular": "^1.1.2"

Additional context

N/A

crushjz avatar Apr 30 '19 08:04 crushjz

Additional fact: the disable like comment works if you use inline template.

I took a quick look and could not find where this error might be (btw, we have no test for this). Any thoughts, @mgechev?

rafaelss95 avatar May 02 '19 02:05 rafaelss95

I will take a look at this right after I/O.

mgechev avatar May 02 '19 03:05 mgechev

Any update on this?

Plondrein avatar Jul 10 '19 21:07 Plondrein

Hi guys, any updates? This happens when you get the value of a reactive form in the template. image

@mgechev @rafaelss95 @crushjz

SebasG22 avatar Sep 23 '19 15:09 SebasG22

The following workaround would work (I know it's not ideal):

/* tslint:disable:template-cyclomatic-complexity */
@Component({
  selector: 'temp',
  templateUrl: './temp.component.html',
  styleUrls: ['./temp.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class Foo {}

And in your template:

<!-- tslint:disable:template-cyclomatic-complexity -->
<div>
  ...
</div>

mgechev avatar Sep 25 '19 22:09 mgechev

@mgechev Unfortunately it does not work if you have multiple pieces of code violating that rule.

The rule I'm using is template-no-call-expression.

I cannot test the rule template-cyclomatic-complexity right now because we have disabled it completely from our project.

(I have also updated codelyzer to 5.1.2 and tslint to 5.20.0)

crushjz avatar Oct 08 '19 09:10 crushjz

Any update on this?

I am facing the same problem for tslint:disable:template-i18n, We need to disable template-i18n rule for some templates in our project but it's not working.

@rafaelss95 @mgechev

usmanmustafa33 avatar Mar 19 '20 04:03 usmanmustafa33

The suggested workaround does not work for me as well. It gets rid of 90% of rule violation, but not all.

tsvetkovv avatar Feb 13 '23 14:02 tsvetkovv