diff --git a/mod.ts b/mod.ts index 6389c54..389808e 100644 --- a/mod.ts +++ b/mod.ts @@ -115,6 +115,13 @@ router // if (version) { // tsModule = await import(`https://esm.sh/typescript@${versionNumber}`).default; // } + + console.log({ + "post": "/twoslash", + code, + extension, + opts + }) const twoslash = await twoslasher(code, extension, opts); context.response.body = twoslash; @@ -160,7 +167,7 @@ router } decodedQuery = Object.assign( - { code: codeParam, extension: extensionParam }, + { code: codeParam, extension: extensionParam ?? "ts" }, optsObj, ); } catch (e) { @@ -179,6 +186,15 @@ router ) } + console.log({ + "get": "/twoslash" + url.search, + code, + extension, + opts + }) + + console.log(code) + const twoslash = await twoslasher(code, extension, opts); console.log("twoslash ", twoslash); context.response.body = twoslash; diff --git a/tests/mod_test.ts b/tests/mod_test.ts new file mode 100644 index 0000000..5bdef20 --- /dev/null +++ b/tests/mod_test.ts @@ -0,0 +1,27 @@ +import { twoslasher } from "../vendor/twoslash.ts"; +import { path } from "../deps.ts"; + +import { twoslasher as twoslasherv2 } from "npm:@typescript/twoslash/dist/twoslash.cjs.development.js"; + +const { dirname, fromFileUrl, join } = path; +const __dirname = dirname(fromFileUrl(import.meta.url)); + + +const twoslash1 = await twoslasher(`\ +// @noErrors +console.t +// ^| +`, `ts`, { +}) + +const twoslash2 = await twoslasherv2(`\ +// @noErrors +console.t +// ^| +`, `ts`, { +}) + +console.log({ + twoslash1, + twoslash2, +}) \ No newline at end of file diff --git a/vendor/twoslash.ts b/vendor/twoslash.ts index 8af1bad..7287d21 100644 --- a/vendor/twoslash.ts +++ b/vendor/twoslash.ts @@ -236,7 +236,7 @@ export function getIdentifierTextSpans(ts: typeof Typescript, sourceFile: Typesc return textSpans function checkChildren(node: Typescript.Node) { - ts.forEachChild(node, child => { + ts.forEachChild(node, (child: { getStart: (arg0: Typescript.SourceFile,arg1: boolean) => any; end: number; getText: (arg0: Typescript.SourceFile) => any; }) => { if (ts.isIdentifier(child)) { const start = child.getStart(sourceFile, false) textSpans.push({ span: ts.createTextSpan(start, child.end - start), text: child.getText(sourceFile) }) @@ -672,7 +672,7 @@ export async function twoslasher(code: string, extension: string, options: TwoSl const defaultCompilerOptions = { strict: true, - target: ts.ScriptTarget.Latest, + target: ts.ScriptTarget.ES2022, allowJs: true, ...(options.defaultCompilerOptions ?? {}), } @@ -787,8 +787,8 @@ export async function twoslasher(code: string, extension: string, options: TwoSl let docs: string | undefined if (quickInfo && quickInfo.displayParts) { - text = quickInfo.displayParts.map(dp => dp.text).join("") - docs = quickInfo.documentation ? quickInfo.documentation.map(d => d.text).join("
") : undefined + text = quickInfo.displayParts.map((dp: { text: any; }) => dp.text).join("") + docs = quickInfo.documentation ? quickInfo.documentation.map((d: { text: any; }) => d.text).join("
") : undefined } else { throw new TwoslashError( `Invalid QuickInfo query`, @@ -809,7 +809,7 @@ export async function twoslasher(code: string, extension: string, options: TwoSl } case "completion": { - const completions = ls.getCompletionsAtPosition(filename, position, {}) + const completions = ls.getCompletionsAtPosition(filename, position - 1, {}) if (!completions && !handbookOptions.noErrorValidation) { throw new TwoslashError( `Invalid completion query`, @@ -853,7 +853,7 @@ export async function twoslasher(code: string, extension: string, options: TwoSl if (!sourceFiles.includes(filetype)) return const output = ls.getEmitOutput(f) - output.outputFiles.forEach(output => { + output.outputFiles.forEach((output: { name: any; text: any; }) => { system.writeFile(output.name, output.text) }) }) @@ -1079,11 +1079,11 @@ export async function twoslasher(code: string, extension: string, options: TwoSl const output = ls.getEmitOutput(emitSource!) const file = output.outputFiles.find( - o => o.name === fsRoot + handbookOptions.showEmittedFile || o.name === handbookOptions.showEmittedFile +( o: { name: string; }) => o.name === fsRoot + handbookOptions.showEmittedFile || o.name === handbookOptions.showEmittedFile ) if (!file) { - const allFiles = output.outputFiles.map(o => o.name).join(", ") + const allFiles = output.outputFiles.map((o: { name: any; }) => o.name).join(", ") throw new TwoslashError( `Cannot find the output file in the Twoslash VFS`, `Looking for ${handbookOptions.showEmittedFile} in the Twoslash vfs after compiling`,