tree-sitter / tree-sitter-typescript

TypeScript grammar for tree-sitter
MIT License
335 stars 104 forks source link

Template strings in types are buggy #206

Closed DGCK81LNN closed 2 years ago

DGCK81LNN commented 2 years ago

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.

sam-goodwin commented 2 years ago

Experiencing this problem also, see: https://github.com/github/linguist/issues/5794

GitHub syntax highlighting is incorrectly handling template literal types:

image

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

image
villelahdenvuo commented 2 years ago

Also ran into this issue:

image
reverofevil commented 2 years ago

Even TypeScript's team ran into this issue.

hendrikvanantwerpen commented 2 years ago

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