Skip to content

Commit 5d9221d

Browse files
shulaodasxzz
andauthored
fix(tsc): replace TS private fields with string keys to avoid syntax errors (#78)
Co-authored-by: 三咲智子 Kevin Deng <sxzz@sxzz.moe>
1 parent daf2768 commit 5d9221d

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/tsc/index.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,25 @@ export function tscEmit(tscOptions: TscOptions): TscResult {
300300
debug(`got source file: ${file.fileName}`)
301301
let dtsCode: string | undefined
302302
let map: SourceMapInput | undefined
303+
304+
// fix #77
305+
const stripPrivateFields: ts.TransformerFactory<ts.SourceFile> = (ctx) => {
306+
const visitor = (node: ts.Node) => {
307+
if (ts.isPropertySignature(node) && ts.isPrivateIdentifier(node.name)) {
308+
return ctx.factory.updatePropertySignature(
309+
node,
310+
node.modifiers,
311+
ctx.factory.createStringLiteral(node.name.text),
312+
node.questionToken,
313+
node.type,
314+
)
315+
}
316+
return ts.visitEachChild(node, visitor, ctx)
317+
}
318+
return (sourceFile) =>
319+
ts.visitNode(sourceFile, visitor, ts.isSourceFile) ?? sourceFile
320+
}
321+
303322
const { emitSkipped, diagnostics } = program.emit(
304323
file,
305324
(fileName, code) => {
@@ -313,7 +332,7 @@ export function tscEmit(tscOptions: TscOptions): TscResult {
313332
},
314333
undefined,
315334
true,
316-
undefined,
335+
{ afterDeclarations: [stripPrivateFields] },
317336
// @ts-expect-error private API: forceDtsEmit
318337
true,
319338
)

0 commit comments

Comments
 (0)