MCPcopy Create free account
hub / github.com/W4G1/multithreading / visitor

Function visitor

scripts/build.ts:23–87  ·  view source on GitHub ↗
(node)

Source from the content-addressed store, hash-verified

21 context,
22) => {
23 const visitor: ts.Visitor = (node) => {
24 // 1. Handle Static Imports/Exports (import ... from "./x.ts")
25 if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) {
26 if (node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier)) {
27 const text = node.moduleSpecifier.text;
28 if (text.endsWith(".ts")) {
29 const newSpecifier = ts.factory.createStringLiteral(
30 text.replace(/\.ts$/, ".js"),
31 );
32
33 if (ts.isImportDeclaration(node)) {
34 return ts.factory.updateImportDeclaration(
35 node,
36 node.modifiers,
37 node.importClause,
38 newSpecifier,
39 node.attributes,
40 );
41 } else {
42 return ts.factory.updateExportDeclaration(
43 node,
44 node.modifiers,
45 node.isTypeOnly,
46 node.exportClause,
47 newSpecifier,
48 node.attributes,
49 );
50 }
51 }
52 }
53 }
54
55 // 2. Handle Dynamic Worker URL (new URL("./worker.ts", ...))
56 if (ts.isNewExpression(node)) {
57 // Check if the expression being new'ed is "URL"
58 if (ts.isIdentifier(node.expression) && node.expression.text === "URL") {
59 if (node.arguments && node.arguments.length > 0) {
60 const firstArg = node.arguments[0]!;
61
62 // Check if first arg is a string literal ending in .ts
63 if (ts.isStringLiteral(firstArg) && firstArg.text.endsWith(".ts")) {
64 console.log(
65 `[Transform] Rewriting new URL("${firstArg.text}") -> .js`,
66 );
67
68 const newArg = ts.factory.createStringLiteral(
69 firstArg.text.replace(/\.ts$/, ".js"),
70 );
71
72 // Create a new arguments array, preserving the second argument (import.meta.url)
73 const newArguments = [newArg, ...node.arguments.slice(1)];
74
75 return ts.factory.updateNewExpression(
76 node,
77 node.expression,
78 node.typeArguments,
79 newArguments,
80 );

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected