twoslashes / twoslash

Markup for generating rich type information in your documentations ahead of time.
https://twoslash.netlify.app/
MIT License
430 stars 16 forks source link

Cannot render code with errors #24

Closed nostrorom closed 4 months ago

nostrorom commented 4 months ago

Hello,

When trying twoslah with shiki, compiler errors are treated as real errors instead of being rendered to html

Code without errors

index.js

import { codeToHtml } from 'shiki';
import { transformerTwoslash } from '@shikijs/twoslash';

const html = await codeToHtml(`console.log()`, {
  lang: 'ts',
  theme: 'vitesse-dark',
  transformers: [
    transformerTwoslash()
  ],
});

console.log(html);

then node ./index.js

correctly renders

<pre class="shiki vitesse-dark twoslash lsp" style="background-color:#121212;color:#dbd7caee" tabindex="0"><code><span class="line"><span style="color:#DBD7CAEE"><span class="twoslash-hover"><span class="twoslash-popup-container"><code class="twoslash-popup-code"><span class="line"><span style="color:#CB7676">var </span><span style="color:#BD976A">console</span><span style="color:#666666">: </span><span style="color:#5DA994">Console</span></span></code></span>console</span></span><span style="color:#DBD7CAEE">.</span><span style="color:#DBD7CAEE"><span class="twoslash-hover"><span class="twoslash-popup-container"><code class="twoslash-popup-code"><span class="line"><span style="color:#BD976A">Console</span><span style="color:#666666">.</span><span style="color:#80A665">log</span><span style="color:#666666">(...</span><span style="color:#BD976A">data</span><span style="color:#DBD7CAEE">: </span><span style="color:#BD976A">any</span><span style="color:#666666">[])</span><span style="color:#DBD7CAEE">: </span><span style="color:#CB7676">void</span></span></code><div class="twoslash-popup-docs">[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log)</div></span>log</span></span><span style="color:#DBD7CAEE">()</span></span></code></pre>

Code without errors

index.js

import { codeToHtml } from 'shiki';
import { transformerTwoslash } from '@shikijs/twoslash';

const html = await codeToHtml(`console.loog()`, {
  lang: 'ts',
  theme: 'vitesse-dark',
  transformers: [
    transformerTwoslash()
  ],
});

console.log(html);

then node ./index.js

throws an error instead of rendering:

  title: 'Errors were thrown in the sample, but not included in an error tag',
  description: 'These errors were not marked as being expected: 2551. \n' +
    'Expected: // @errors: 2551',
  recommendation: 'Compiler Errors:\n' +
    '\n' +
    'index.ts\n' +
    "  [2551] 8 - Property 'loog' does not exist on type 'Console'. Did you mean 'log'?",
  code: undefined

Using error tags to suppress errors

Following the expected tag leads to other errors, also suppressing them

These errors were not marked as being expected: 2551 1146 1005. 
Expected: // @errors: 2551 1146 1005

index.js

import { codeToHtml } from 'shiki';
import { transformerTwoslash } from '@shikijs/twoslash';

const html = await codeToHtml(` // @errors: 2551 1146 1005 console.loog()`, {
  lang: 'ts',
  theme: 'vitesse-dark',
  transformers: [
    transformerTwoslash()
  ],
});

console.log(html);

then node ./index.js

this renders the HTML, but the errors are also suppressed here, defeating the purpose

<pre class="shiki vitesse-dark twoslash lsp" style="background-color:#121212;color:#dbd7caee" tabindex="0"><code><span class="line"><span style="color:#758575DD">// @errors:2551 1146 1005 console.loog()</span></span></code></pre>

What am I missing here ?

antfu commented 4 months ago

What are you trying to do? Are you trying to render all errors, or ignores all errors?

You can add // @noErrors or // @noErrorVaildation deps on your need: https://github.com/twoslashes/twoslash/blob/0a18156a9f71b05d84b55b53ef63dca125aab071/packages/twoslash/src/types/handbook-options.ts#L4-L44

nostrorom commented 4 months ago

Hi @antfu

Thanks for your quick answer; reading it has made me understand I had the wrong expectations.

What I was trying to do was have this kind of error line:

image

<div class="twoslash-meta-line twoslash-error-line">Type 'string' is not assignable to type 'number'.</div><div class="twoslash-meta-line twoslash-error-line">Type 'number' is not assignable to type 'string'.</div>

Which I can reproduce from the example in the docs; my mistake was expecting similar error line divs also for JS errors. I know realise this is not the scope of twoslash, only TS errors.

nostrorom commented 4 months ago

Though if there is a way to also generate something like this for the error in my OP, I'd be happy to kown 😄

<div class="twoslash-meta-line twoslash-error-line">
  Property 'loog' does not exist on type 'Console'. Did you mean 'log'?"
</div>

Otherwise the issue is closed for me

antfu commented 4 months ago

Idk, maybe try adding // @ts-check to your js file?

nostrorom commented 4 months ago

Did no seem to work. I'm still fiddling with things to see if I can probably describe the cases where I find there is expected/unexpected behaviour, and then provide reproductible examples. Is there a discussion page where I can take this conversation to instead of issues? I'll close this for now.

Anyway thank you guys for yout work on this project, it's an awesome tool 👍