-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
twoslasher.ts
46 lines (44 loc) · 1.11 KB
/
twoslasher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { createTwoslasher } from 'twoslash'
import * as cache from '../../utils/cache.js'
import { hash } from '../../utils/hash.js'
const twoslasher_ = createTwoslasher()
export function twoslasher(
...parameters: Parameters<typeof twoslasher_>
): ReturnType<typeof twoslasher_> {
const codeHash = hash(parameters[0])
if (cache.twoslash.get(codeHash)) return cache.twoslash.get(codeHash)
try {
const twoslash = twoslasher_(...parameters)
cache.twoslash.set(codeHash, twoslash)
return twoslash
} catch (e) {
const error = e as Error
const lines = parameters[0].split('\n')
const line = lines.length - 1
return {
code: parameters[0],
nodes: [
{
filename: '',
level: 'error',
type: 'error',
code: 0,
length: 100,
start: 0,
line,
character: 0,
text: error.message.replace('\n', ''),
id: '',
},
],
// @ts-expect-error
meta: {},
queries: [],
completions: [],
errors: [],
highlights: [],
hovers: [],
tags: [],
}
}
}