Skip to content
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

twoslash renderer not highlighting errors that span multiple tokens #144

Open
with-heart opened this issue Mar 4, 2022 · 1 comment · May be fixed by #145
Open

twoslash renderer not highlighting errors that span multiple tokens #144

with-heart opened this issue Mar 4, 2022 · 1 comment · May be fixed by #145

Comments

@with-heart
Copy link
Contributor

I'm trying to render an error for this block of code:

type MyPick<T, K> = {
  [Key in K]: T[Key]
}

I'm using this twoslash block:

```ts twoslash
// @errors: 2322 2536
type MyPick<T, K> = {
  [Key in K]: T[Key]
}
```

That block results in this output:
image

The error output is correct. There are two errors: one on K and another on T[Key]. However, as you can see there's no error squiggly for T[Key].

Here's what it should look like (screenshot from ts playground):

image

@with-heart
Copy link
Contributor Author

with-heart commented Mar 4, 2022

I believe this has two causes:

  1. @typescript/twoslash parses T[Key] as 4 tokens:

    [
      {
        content: 'T',
        color: '#267F99',
        fontStyle: 0,
        explanation: [Array],
      },
      {
        content: '[',
        color: '#000000',
        fontStyle: 0,
        explanation: [Array],
      },
      {
        content: 'Key',
        color: '#267F99',
        fontStyle: 0,
        explanation: [Array],
      },
      {
        content: ']',
        color: '#000000',
        fontStyle: 0,
        explanation: [Array],
      },
    ]
  2. The logic for findTokenFunc in renderers/twoslash.ts expects a single token:

    const findTokenFunc = (start: number) => (e: any) =>
    start <= e.character && start + token.content.length >= e.character + e.length

Here's the error itself as reported by @typescript/twoslash:

{
  category: 1,
  code: 2536,
  length: 6,
  start: 36,
  line: 1,
  character: 14,
  renderedMessage: "Type 'Key' cannot be used to index type 'T'.",
  id: 'err-2536-36-6'
}

Using findTokenDebug in place of findTokenFunc gives the following results (truncated to relevant output):

false 14 <= 14 && 15 >= 20
false 15 <= 14 && 16 >= 20
false 16 <= 14 && 19 >= 20
false 19 <= 14 && 20 >= 20

So this fails in two ways:

  1. start is only <= to e.character for the first character of the tokens.
  2. start + token.content.length is only >= e.character + e.length for the last character of the tokens.

It seems like what we'd need to do is apply the error to any token that falls within the range of the error. I'm not super familiar with the codebase though, so I'm not sure what the implications of making that change would be (if any).

Happy to tackle this myself if anyone could give me a little direction here.

@with-heart with-heart linked a pull request Mar 4, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant