Tslint: no-angle-bracket-type-assertion --fix puts multiple casts in wrong order

Created on 26 Jun 2017  路  4Comments  路  Source: palantir/tslint

Bug Report

  • __TSLint version__: 5.4.0
  • __TypeScript version__: 2.4 RC
  • __Running TSLint via__: (pick one) CLI

TypeScript code being linted

<number><any>value

with tslint.json configuration:

{
    "rules": {
        "no-angle-bracket-type-assertion": true
    }
}

Actual behavior

value as number as any

Expected behavior

value as any as number
P2 Accepting PRs Bug

Most helpful comment

Got it. It is in fact sorting of the fixes.

It's failing locally when I change the rule's test.ts.lint file to:

var a = <any>5;
        ~~~~~~  [Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.]

var b = <number><any>5;
        ~~~~~~~~~~~~~~  [Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.]
                ~~~~~~  [Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.]

var c = <number>(5 as any);
        ~~~~~~~~~~~~~~~~~~  [Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.]

var d = <any> 5 as number;
        ~~~~~~~            [Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.]

var e = 5 as any as number;

var f = <number><any>d & e;
        ~~~~~~~~~~~~~~  [Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.]
                ~~~~~~  [Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.]

var f = <number><any>d & e;
        ~~~~~~~~~~~~~~  [Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.]
                ~~~~~~  [Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.]

```
Expected (from .fix file):

var b = 5 as number as any;

Actual (from TSLint):

var b = 5 as any as number;

All 4 comments

Some random thoughts:
The AST is a bit weird when dealing with type assertions. That may be the problem.

Or it's related to the sorting of the fixes, because both start at the same position and tslint usually sorts them by end position in descending order. Which will result in unexpected results when the length of the replacements differs.

Ehh, this particular sample is part of the test cases and I can't reproduce it. I'll close as related to #2955.

Got it. It is in fact sorting of the fixes.

It's failing locally when I change the rule's test.ts.lint file to:

var a = <any>5;
        ~~~~~~  [Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.]

var b = <number><any>5;
        ~~~~~~~~~~~~~~  [Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.]
                ~~~~~~  [Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.]

var c = <number>(5 as any);
        ~~~~~~~~~~~~~~~~~~  [Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.]

var d = <any> 5 as number;
        ~~~~~~~            [Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.]

var e = 5 as any as number;

var f = <number><any>d & e;
        ~~~~~~~~~~~~~~  [Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.]
                ~~~~~~  [Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.]

var f = <number><any>d & e;
        ~~~~~~~~~~~~~~  [Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.]
                ~~~~~~  [Type assertion using the '<>' syntax is forbidden. Use the 'as' syntax instead.]

```
Expected (from .fix file):

var b = 5 as number as any;

Actual (from TSLint):

var b = 5 as any as number;

Possibly related, --fix sometimes (in more complex code) "corrects" generic parameters: Foo<Bar> => Foo as Bar. It doesn't do this all the time though, and haven't figured out a good (simple) test file for a bug report yet....

Was this page helpful?
0 / 5 - 0 ratings

Related issues

avanderhoorn picture avanderhoorn  路  3Comments

denkomanceski picture denkomanceski  路  3Comments

DanielKucal picture DanielKucal  路  3Comments

Ne-Ne picture Ne-Ne  路  3Comments

cateyes99 picture cateyes99  路  3Comments