Skip to content

Commit 191b934

Browse files
committed
Fix tsx as input language flag
1 parent 11693ee commit 191b934

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

lib/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const defaultGrammars = [sourceJson, sourceJs, sourceTsx, sourceTs]
4444
const extensionToScopeMap = new Map([
4545
['json', 'source.json'],
4646
['js', 'source.js'],
47+
['tsx', 'source.tsx'],
4748
['ts', 'source.ts']
4849
])
4950

@@ -186,7 +187,10 @@ export default function rehypeTwoslash(options) {
186187
let annotations = []
187188

188189
try {
189-
const result = twoslash(value, scope === 'source.js' ? 'js' : 'ts')
190+
const result = twoslash(
191+
value,
192+
scope === 'source.js' ? 'js' : scope === 'source.tsx' ? 'tsx' : 'ts'
193+
)
190194
value = result.code
191195
annotations = result.nodes
192196
scope = extensionToScopeMap.get(result.meta.extension)

test/fixtures/basic/input.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
</code></pre>
3333
<pre><code class="language-jsx">let fragment = &lt;jsx />
3434
</code></pre>
35-
<pre><code class="language-ts">// @errors: 2304 1005 1161
35+
<pre><code class="language-ts">// @errors: 1005 1161 2304
3636
let fragment = &lt;jsx />
3737
</code></pre>
38-
<pre><code class="language-tsx">// @errors: 2304 1005 1161
38+
<pre><code class="language-tsx">// @errors: 1005 1161 2304 7026
3939
let fragment = &lt;jsx />
4040
</code></pre>

test/fixtures/basic/output.html

+14-12
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,20 @@
7878
<div class="rehype-twoslash-hover rehype-twoslash-popover" id="rehype-twoslash-lfjlfj-1-1" popover=""><pre class="rehype-twoslash-popover-code"><code class="language-ts"><span class="pl-smi">any</span></code></pre></div>
7979
</div>
8080
<div class="highlight highlight-ts">
81-
<pre><code class="language-ts"><span class="pl-k">let</span> <span class="rehype-twoslash-popover-target" data-popover-target="rehype-twoslash-e2111lfj-0"><span class="pl-smi">fragment</span></span> <span class="pl-k">=</span> &#x3C;<span class="rehype-twoslash-popover-target" data-popover-target="rehype-twoslash-e2111lfj-2"><span class="rehype-twoslash-error-target rehype-twoslash-popover-target" data-popover-target="rehype-twoslash-e2111lfj-1"><span class="pl-en">jsx</span></span></span> <span class="rehype-twoslash-error-target rehype-twoslash-popover-target" data-popover-target="rehype-twoslash-e2111lfj-3">/</span>>
81+
<pre><code class="language-ts"><span class="pl-k">let</span> <span class="rehype-twoslash-popover-target" data-popover-target="rehype-twoslash-e1122lfj-0"><span class="pl-smi">fragment</span></span> <span class="pl-k">=</span> &#x3C;<span class="rehype-twoslash-popover-target" data-popover-target="rehype-twoslash-e1122lfj-2"><span class="rehype-twoslash-error-target rehype-twoslash-popover-target" data-popover-target="rehype-twoslash-e1122lfj-1"><span class="pl-en">jsx</span></span></span> <span class="rehype-twoslash-error-target rehype-twoslash-popover-target" data-popover-target="rehype-twoslash-e1122lfj-3">/</span>>
8282
</code></pre>
83-
<div class="rehype-twoslash-hover rehype-twoslash-popover" id="rehype-twoslash-e2111lfj-0" popover=""><pre class="rehype-twoslash-popover-code"><code class="language-ts"><span class="pl-k">let</span> <span class="pl-smi">fragment</span><span class="pl-k">:</span> <span class="pl-en">jsx</span></code></pre></div>
84-
<div class="rehype-twoslash-error rehype-twoslash-popover" id="rehype-twoslash-e2111lfj-1" popover=""><pre class="rehype-twoslash-popover-code"><code>Cannot find name 'jsx'. (2304)</code></pre></div>
85-
<div class="rehype-twoslash-hover rehype-twoslash-popover" id="rehype-twoslash-e2111lfj-2" popover=""><pre class="rehype-twoslash-popover-code"><code class="language-ts"><span class="pl-k">type</span> <span class="pl-en">jsx</span> <span class="pl-k">=</span> <span class="pl-c">/*unresolved*/</span> <span class="pl-c1">any</span></code></pre></div>
86-
<div class="rehype-twoslash-error rehype-twoslash-popover" id="rehype-twoslash-e2111lfj-3" popover=""><pre class="rehype-twoslash-popover-code"><code>'>' expected. (1005)</code></pre></div>
83+
<div class="rehype-twoslash-hover rehype-twoslash-popover" id="rehype-twoslash-e1122lfj-0" popover=""><pre class="rehype-twoslash-popover-code"><code class="language-ts"><span class="pl-k">let</span> <span class="pl-smi">fragment</span><span class="pl-k">:</span> <span class="pl-en">jsx</span></code></pre></div>
84+
<div class="rehype-twoslash-error rehype-twoslash-popover" id="rehype-twoslash-e1122lfj-1" popover=""><pre class="rehype-twoslash-popover-code"><code>Cannot find name 'jsx'. (2304)</code></pre></div>
85+
<div class="rehype-twoslash-hover rehype-twoslash-popover" id="rehype-twoslash-e1122lfj-2" popover=""><pre class="rehype-twoslash-popover-code"><code class="language-ts"><span class="pl-k">type</span> <span class="pl-en">jsx</span> <span class="pl-k">=</span> <span class="pl-c">/*unresolved*/</span> <span class="pl-c1">any</span></code></pre></div>
86+
<div class="rehype-twoslash-error rehype-twoslash-popover" id="rehype-twoslash-e1122lfj-3" popover=""><pre class="rehype-twoslash-popover-code"><code>'>' expected. (1005)</code></pre></div>
8787
</div>
88-
<div class="highlight highlight-ts">
89-
<pre><code class="language-ts"><span class="pl-k">let</span> <span class="rehype-twoslash-popover-target" data-popover-target="rehype-twoslash-e2111lfj-1-0"><span class="pl-smi">fragment</span></span> <span class="pl-k">=</span> &#x3C;<span class="rehype-twoslash-popover-target" data-popover-target="rehype-twoslash-e2111lfj-1-2"><span class="rehype-twoslash-error-target rehype-twoslash-popover-target" data-popover-target="rehype-twoslash-e2111lfj-1-1"><span class="pl-en">jsx</span></span></span> <span class="rehype-twoslash-error-target rehype-twoslash-popover-target" data-popover-target="rehype-twoslash-e2111lfj-1-3">/</span>>
90-
</code></pre>
91-
<div class="rehype-twoslash-hover rehype-twoslash-popover" id="rehype-twoslash-e2111lfj-1-0" popover=""><pre class="rehype-twoslash-popover-code"><code class="language-ts"><span class="pl-k">let</span> <span class="pl-smi">fragment</span><span class="pl-k">:</span> <span class="pl-en">jsx</span></code></pre></div>
92-
<div class="rehype-twoslash-error rehype-twoslash-popover" id="rehype-twoslash-e2111lfj-1-1" popover=""><pre class="rehype-twoslash-popover-code"><code>Cannot find name 'jsx'. (2304)</code></pre></div>
93-
<div class="rehype-twoslash-hover rehype-twoslash-popover" id="rehype-twoslash-e2111lfj-1-2" popover=""><pre class="rehype-twoslash-popover-code"><code class="language-ts"><span class="pl-k">type</span> <span class="pl-en">jsx</span> <span class="pl-k">=</span> <span class="pl-c">/*unresolved*/</span> <span class="pl-c1">any</span></code></pre></div>
94-
<div class="rehype-twoslash-error rehype-twoslash-popover" id="rehype-twoslash-e2111lfj-1-3" popover=""><pre class="rehype-twoslash-popover-code"><code>'>' expected. (1005)</code></pre></div>
88+
<div class="highlight highlight-tsx">
89+
<pre><code class="language-tsx"><span class="pl-k">let</span> <span class="rehype-twoslash-popover-target" data-popover-target="rehype-twoslash-e1127lfj-0"><span class="pl-smi">fragment</span></span> <span class="pl-k">=</span> <span class="rehype-twoslash-error-target rehype-twoslash-popover-target" data-popover-target="rehype-twoslash-e1127lfj-3">&#x3C;</span><span class="rehype-twoslash-popover-target" data-popover-target="rehype-twoslash-e1127lfj-5"><span class="rehype-twoslash-error-target rehype-twoslash-popover-target" data-popover-target="rehype-twoslash-e1127lfj-4"><span class="rehype-twoslash-error-target rehype-twoslash-popover-target" data-popover-target="rehype-twoslash-e1127lfj-2"><span class="pl-ent">jsx</span></span></span></span><span class="rehype-twoslash-error-target rehype-twoslash-popover-target" data-popover-target="rehype-twoslash-e1127lfj-1"> /></span>
90+
</code></pre>
91+
<div class="rehype-twoslash-hover rehype-twoslash-popover" id="rehype-twoslash-e1127lfj-0" popover=""><pre class="rehype-twoslash-popover-code"><code class="language-ts"><span class="pl-k">let</span> <span class="pl-smi">fragment</span><span class="pl-k">:</span> <span class="pl-c1">any</span></code></pre></div>
92+
<div class="rehype-twoslash-error rehype-twoslash-popover" id="rehype-twoslash-e1127lfj-1" popover=""><pre class="rehype-twoslash-popover-code"><code>JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists. (7026)</code></pre></div>
93+
<div class="rehype-twoslash-error rehype-twoslash-popover" id="rehype-twoslash-e1127lfj-2" popover=""><pre class="rehype-twoslash-popover-code"><code>JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists. (7026)</code></pre></div>
94+
<div class="rehype-twoslash-error rehype-twoslash-popover" id="rehype-twoslash-e1127lfj-3" popover=""><pre class="rehype-twoslash-popover-code"><code>JSX element implicitly has type 'any' because no interface 'JSX.IntrinsicElements' exists. (7026)</code></pre></div>
95+
<div class="rehype-twoslash-error rehype-twoslash-popover" id="rehype-twoslash-e1127lfj-4" popover=""><pre class="rehype-twoslash-popover-code"><code>Cannot find name 'React'. (2304)</code></pre></div>
96+
<div class="rehype-twoslash-hover rehype-twoslash-popover" id="rehype-twoslash-e1127lfj-5" popover=""><pre class="rehype-twoslash-popover-code"><code class="language-ts"><span class="pl-smi">any</span></code></pre></div>
9597
</div>

0 commit comments

Comments
 (0)