Skip to content

Reverse SyntaxKind query shows misleading result #18062

Closed

Description

TypeScript Version: 2.4.2

Code

console.log(ts.SyntaxKind[8]);

Expected behavior: NumericLiteral

Actual behavior: FirstLiteralToken

Description:

Maybe this is a duplicate of a more broad issue (which I did not find), and I am not sure if there is a workaround for this specific case, but currently I would presume that I can use SyntaxKind enum for reverse lookups of values I got from TS ast node's kind property.

As this enum describes not just unique values but also ranges (with FirstSomething and LastSomething elements), reversing the enum values is not unambiguous, and currently it gives back the less expected result (FirstLiteralToken) and not the expected one (NumericLiteral).

I think changing the order of the enum items or putting these range items to outside of the enum could solve this issue, but this would break compatibility of course.

Footnote: other tools (for example https://astexplorer.net/) also ran into this mistake and it makes harder to beginners to understand without reading TS's source code what's actually happening here. (I thought for a while that FirstLiteralToken is some kind of special token, but then I empirically found out it only shows this token type for numeric values and just then I looked into TS's source code...)

((I'll report this issue to AstExplorer too.))

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions