Skip to content

Template strings in types are buggy #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DGCK81LNN opened this issue Feb 16, 2022 · 4 comments
Closed

Template strings in types are buggy #206

DGCK81LNN opened this issue Feb 16, 2022 · 4 comments
Labels

Comments

@DGCK81LNN
Copy link

DGCK81LNN commented Feb 16, 2022

The following piece of code is valid but it is parsed incorrectly:

var this_ = ``;
var isCorrect = `foo`;
type ButThis = ``; //<- first backtick is misparsed
type IsWrong = `foo`;

// Um... So I'm in a template string or what?

var value = `type A = string`; //<- second backtick is misparsed

The snippet above is valid TypeScript:
https://www.typescriptlang.org/play?#code/G4QwTgBALgFglgZwPoQLwQAYYNwChSSIDCA9mGAKYDGUamAZiSTrlAJ4AOFEAQgK5QAKvAR0s2CAHpJAHgC0EenDAJaAIxBUA1lDjaIiCAHd4UCq07cAkggDqYEgDsA5mMbM8uaRACqAWwA6IIgAZRIIKwByPwNHCBBoCj8OABsQMwhVMDgXCDJjGHSAflx8cAhQFL5udAx2LggAQTosnOccKVkFBGonABMIDW1dfUMTODMgA

I was just browsing someone's code on GitHub when I noticed the highlighting was wrong. I tried to reproduce the problem in the snippet above. The first backtick on line 3 seems to have been ignored or what.

The original code I saw looked more like this, actually:

type A<B extends string, C>
  = B extends `${any}:${infer T}` ? T extends D ? E[T] : F : F
type G<B extends string, C>
  = B extends `${infer L}${X}${infer R}` ? `${L}${Y}${Replace<R, X, Y>}` : B
type H<B extends string, C>
  = B extends `${infer L}]${infer R}` ? [ParamType<L, K>, ...ExtractAll<R, K>] : []

The output of tree-sitter parse is the following: I didn't know anything about tree-sitter before, and I have been unable to run tree-sitter right now, sorry. I'll come back to this later.

@DGCK81LNN DGCK81LNN added the bug label Feb 16, 2022
@sam-goodwin
Copy link

sam-goodwin commented Feb 20, 2022

Experiencing this problem also, see: github-linguist/linguist#5794

GitHub syntax highlighting is incorrectly handling template literal types:
image

Expected behaviour:
It should render the same as VS Code does:
image

@villelahdenvuo
Copy link

Also ran into this issue:
image

@reverofevil
Copy link

Even TypeScript's team ran into this issue.

@hendrikvanantwerpen
Copy link
Contributor

Latest master supports these examples now, as far as I can tell, so I'm closing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants