-
-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(twoslash): support targeting multiple tokens a node, more accurat…
…e result
- Loading branch information
Showing
3 changed files
with
101 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<link rel="stylesheet" href="../../style-rich.css" /> | ||
<pre class="shiki vitesse-dark twoslash lsp" style="background-color:#121212;color:#dbd7caee" tabindex="0"><code><span class="line"><span style="color:#CB7676">const</span><span> </span><span style="color:#BD976A"><span class="twoslash-hover"><span class="twoslash-popup-info"><span class="line"><span style="color:#CB7676">const</span><span> </span><span style="color:#BD976A">x</span><span style="color:#666666">: [</span><span style="color:#5DA994">number</span><span style="color:#666666">]</span></span></span>x</span></span><span style="color:#666666">: [</span><span style="color:#5DA994">number</span><span style="color:#666666">] =</span><span style="color:#CB7676"> </span><span style="color:#666666">[</span><span style="color:#C98A7D99"><span class="twoslash-error">"</span></span><span style="color:#C98A7D"><span class="twoslash-error">hello</span></span><span style="color:#C98A7D99"><span class="twoslash-error">"</span></span><span style="color:#666666">]</span></span><div class="twoslash-meta-line twoslash-error-line">Type 'string' is not assignable to type 'number'.</div></code></pre> |
44 changes: 44 additions & 0 deletions
44
packages/shikiji-twoslash/test/target-multi-tokens.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { codeToHtml, codeToThemedTokens } from 'shikiji' | ||
import { transformerTwoSlash } from 'shikiji-twoslash' | ||
import { expect, it } from 'vitest' | ||
|
||
const code = `const x: [number] = ["hello"]` | ||
|
||
it('verify theme behavior', async () => { | ||
const tokens = await codeToThemedTokens(code, { | ||
lang: 'ts', | ||
theme: 'vitesse-dark', | ||
}) | ||
|
||
// `vitesse-dark` separates the the quotes ints tokens, where the error is targeting all strings | ||
expect.soft(tokens.find(i => i.find(j => j.content === '"'))) | ||
.toBeDefined() | ||
expect.soft(tokens.find(i => i.find(j => j.content === '"ref"'))) | ||
.not.toBeDefined() | ||
}) | ||
|
||
it('should split tokens correctly', async () => { | ||
const html = await codeToHtml(code, { | ||
lang: 'ts', | ||
theme: 'vitesse-dark', | ||
transformers: [ | ||
transformerTwoSlash({ | ||
twoslashOptions: { | ||
handbookOptions: { | ||
errors: [2322], | ||
}, | ||
}, | ||
}), | ||
], | ||
}) | ||
|
||
expect.soft( | ||
[...html.matchAll(/<span class="twoslash-error">(.*?)<\/span>/g)].map(i => i[1]), | ||
) | ||
.toEqual(['"', 'hello', '"']) | ||
|
||
expect( | ||
`<link rel="stylesheet" href="../../style-rich.css" />\n${html}`, | ||
) | ||
.toMatchFileSnapshot('./out/error-multi-tokens.html') | ||
}) |