|
| 1 | +/** |
| 2 | + * This file is the core of the shikiji-twoslash package, |
| 3 | + * Decoupled from twoslash's implementation and allowing to introduce custom implementation or cache system. |
| 4 | + */ |
| 5 | +import type { twoslasher } from '@typescript/twoslash' |
| 6 | +import type { ShikijiTransformer } from 'shikiji-core' |
| 7 | +import type { Element, ElementContent, Text } from 'hast' |
| 8 | +import type { ModuleKind, ScriptTarget } from 'typescript' |
| 9 | + |
| 10 | +import { addClassToHast } from 'shikiji-core' |
| 11 | +import { rendererClassic } from './renderer-classic' |
| 12 | +import type { TransformerTwoSlashOptions } from './types' |
| 13 | + |
| 14 | +export * from './types' |
| 15 | +export * from './renderer-classic' |
| 16 | +export * from './renderer-rich' |
| 17 | +export * from './icons' |
| 18 | + |
| 19 | +export function defaultTwoSlashOptions() { |
| 20 | + return { |
| 21 | + customTags: ['annotate', 'log', 'warn', 'error'], |
| 22 | + defaultCompilerOptions: { |
| 23 | + module: 99 satisfies ModuleKind.ESNext, |
| 24 | + target: 99 satisfies ScriptTarget.ESNext, |
| 25 | + }, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +export function createTransformer(runTwoslasher: typeof twoslasher) { |
| 30 | + return function transformerTwoSlash(options: TransformerTwoSlashOptions = {}): ShikijiTransformer { |
| 31 | + const { |
| 32 | + langs = ['ts', 'tsx'], |
| 33 | + twoslashOptions = defaultTwoSlashOptions(), |
| 34 | + langAlias = { |
| 35 | + typescript: 'ts', |
| 36 | + json5: 'json', |
| 37 | + yml: 'yaml', |
| 38 | + }, |
| 39 | + explicitTrigger = false, |
| 40 | + renderer = rendererClassic(), |
| 41 | + throws = true, |
| 42 | + } = options |
| 43 | + const filter = options.filter || ((lang, _, options) => langs.includes(lang) && (!explicitTrigger || /\btwoslash\b/.test(options.meta?.__raw || ''))) |
| 44 | + return { |
| 45 | + preprocess(code, shikijiOptions) { |
| 46 | + let lang = shikijiOptions.lang |
| 47 | + if (lang in langAlias) |
| 48 | + lang = langAlias[shikijiOptions.lang] |
| 49 | + |
| 50 | + if (filter(lang, code, shikijiOptions)) { |
| 51 | + shikijiOptions.mergeWhitespaces = false |
| 52 | + const twoslash = runTwoslasher(code, lang, twoslashOptions) |
| 53 | + this.meta.twoslash = twoslash |
| 54 | + return twoslash.code |
| 55 | + } |
| 56 | + }, |
| 57 | + pre(pre) { |
| 58 | + if (this.meta.twoslash) |
| 59 | + addClassToHast(pre, 'twoslash lsp') |
| 60 | + }, |
| 61 | + code(codeEl) { |
| 62 | + const twoslash = this.meta.twoslash |
| 63 | + if (!twoslash) |
| 64 | + return |
| 65 | + |
| 66 | + const insertAfterLine = (line: number, nodes: ElementContent[]) => { |
| 67 | + if (!nodes.length) |
| 68 | + return |
| 69 | + let index: number |
| 70 | + if (line >= this.lines.length) { |
| 71 | + index = codeEl.children.length |
| 72 | + } |
| 73 | + else { |
| 74 | + const lineEl = this.lines[line] |
| 75 | + index = codeEl.children.indexOf(lineEl) |
| 76 | + if (index === -1) { |
| 77 | + if (throws) |
| 78 | + throw new Error(`[shikiji-twoslash] Cannot find line ${line} in code element`) |
| 79 | + return |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // If there is a newline after this line, remove it because we have the error element take place. |
| 84 | + const nodeAfter = codeEl.children[index + 1] |
| 85 | + if (nodeAfter && nodeAfter.type === 'text' && nodeAfter.value === '\n') |
| 86 | + codeEl.children.splice(index + 1, 1) |
| 87 | + codeEl.children.splice(index + 1, 0, ...nodes) |
| 88 | + } |
| 89 | + |
| 90 | + const locateTextToken = ( |
| 91 | + line: number, |
| 92 | + character: number, |
| 93 | + ) => { |
| 94 | + const lineEl = this.lines[line] |
| 95 | + if (!lineEl) { |
| 96 | + if (throws) |
| 97 | + throw new Error(`[shikiji-twoslash] Cannot find line ${line} in code element`) |
| 98 | + } |
| 99 | + const textNodes = lineEl.children.flatMap(i => i.type === 'element' ? i.children || [] : []) as (Text | Element)[] |
| 100 | + let index = 0 |
| 101 | + for (const token of textNodes) { |
| 102 | + if ('value' in token && typeof token.value === 'string') |
| 103 | + index += token.value.length |
| 104 | + |
| 105 | + if (index > character) |
| 106 | + return token |
| 107 | + } |
| 108 | + if (throws) |
| 109 | + throw new Error(`[shikiji-twoslash] Cannot find token at L${line}:${character}`) |
| 110 | + } |
| 111 | + |
| 112 | + const skipTokens = new Set<Element | Text>() |
| 113 | + |
| 114 | + for (const error of twoslash.errors) { |
| 115 | + if (error.line == null || error.character == null) |
| 116 | + return |
| 117 | + const token = locateTextToken(error.line, error.character) |
| 118 | + if (!token) |
| 119 | + continue |
| 120 | + |
| 121 | + skipTokens.add(token) |
| 122 | + |
| 123 | + if (renderer.nodeError) { |
| 124 | + const clone = { ...token } |
| 125 | + Object.assign(token, renderer.nodeError.call(this, error, clone)) |
| 126 | + } |
| 127 | + |
| 128 | + if (renderer.lineError) |
| 129 | + insertAfterLine(error.line, renderer.lineError.call(this, error)) |
| 130 | + } |
| 131 | + |
| 132 | + for (const query of twoslash.queries) { |
| 133 | + if (query.kind === 'completions') { |
| 134 | + const token = locateTextToken(query.line - 1, query.offset) |
| 135 | + if (!token) |
| 136 | + throw new Error(`[shikiji-twoslash] Cannot find token at L${query.line}:${query.offset}`) |
| 137 | + skipTokens.add(token) |
| 138 | + |
| 139 | + if (renderer.nodeCompletions) { |
| 140 | + const clone = { ...token } |
| 141 | + Object.assign(token, renderer.nodeCompletions.call(this, query, clone)) |
| 142 | + } |
| 143 | + |
| 144 | + if (renderer.lineCompletions) |
| 145 | + insertAfterLine(query.line, renderer.lineCompletions.call(this, query)) |
| 146 | + } |
| 147 | + else if (query.kind === 'query') { |
| 148 | + const token = locateTextToken(query.line - 1, query.offset) |
| 149 | + if (!token) |
| 150 | + throw new Error(`[shikiji-twoslash] Cannot find token at L${query.line}:${query.offset}`) |
| 151 | + |
| 152 | + skipTokens.add(token) |
| 153 | + |
| 154 | + if (renderer.nodeQuery) { |
| 155 | + const clone = { ...token } |
| 156 | + Object.assign(token, renderer.nodeQuery.call(this, query, clone)) |
| 157 | + } |
| 158 | + |
| 159 | + if (renderer.lineQuery) |
| 160 | + insertAfterLine(query.line, renderer.lineQuery.call(this, query, token)) |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + for (const info of twoslash.staticQuickInfos) { |
| 165 | + const token = locateTextToken(info.line, info.character) |
| 166 | + if (!token || token.type !== 'text') |
| 167 | + continue |
| 168 | + |
| 169 | + // If it's already rendered as popup or error, skip it |
| 170 | + if (skipTokens.has(token)) |
| 171 | + continue |
| 172 | + |
| 173 | + const clone = { ...token } |
| 174 | + Object.assign(token, renderer.nodeStaticInfo.call(this, info, clone)) |
| 175 | + } |
| 176 | + |
| 177 | + if (renderer.lineCustomTag) { |
| 178 | + for (const tag of twoslash.tags) |
| 179 | + insertAfterLine(tag.line, renderer.lineCustomTag.call(this, tag)) |
| 180 | + } |
| 181 | + }, |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments