From 5fa5cd9a060bce37cf2b667b793eef22f35a4d4b Mon Sep 17 00:00:00 2001 From: gavin1012_hw Date: Wed, 12 Oct 2022 15:41:28 +0800 Subject: [PATCH] Fix no-name for autoGenerated Identifier in ts2panda Related Issue: I5VCP6 Signed-off-by: gavin1012_hw Change-Id: Ie7a7ac26f48696e6f3722142ede9a3204b4ebaae --- ts2panda/src/base/lreference.ts | 7 ++++++- ts2panda/src/index.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/ts2panda/src/base/lreference.ts b/ts2panda/src/base/lreference.ts index e27363aee3..b079b70bb8 100644 --- a/ts2panda/src/base/lreference.ts +++ b/ts2panda/src/base/lreference.ts @@ -139,7 +139,12 @@ export class LReference { let name = jshelpers.getTextOfIdentifierOrLiteral(realNode); let variable = compiler.getCurrentScope().find(name); if (!variable.v) { - variable.v = compiler.getCurrentScope().add(name, VarDeclarationKind.NONE); + // @ts-ignore + if (ts.isGeneratedIdentifier(realNode)) { + variable.v = compiler.getCurrentScope().add(name, VarDeclarationKind.VAR); + } else { + variable.v = compiler.getCurrentScope().add(name, VarDeclarationKind.NONE); + } } return new LReference(realNode, compiler, isDeclaration, ReferenceKind.LocalOrGlobal, variable); diff --git a/ts2panda/src/index.ts b/ts2panda/src/index.ts index 866827b25d..a727424d27 100644 --- a/ts2panda/src/index.ts +++ b/ts2panda/src/index.ts @@ -86,6 +86,8 @@ function main(fileNames: string[], options: ts.CompilerOptions) { // @ts-ignore (ctx: ts.TransformationContext) => { return (node: ts.SourceFile) => { + makeAutoGeneratedNodeName(node); + if (ts.getEmitHelpers(node)) { let newStatements = []; ts.getEmitHelpers(node)?.forEach( @@ -124,6 +126,30 @@ function main(fileNames: string[], options: ts.CompilerOptions) { }); } +let generatedVarId = 0; + +function makeAutoGeneratedNodeName(node: ts.Node) { + node.forEachChild(childNode => { + switch (childNode.kind) { + case ts.SyntaxKind.Identifier: { + // @ts-ignore + if (ts.isGeneratedIdentifier(childNode) && (childNode).escapedText == "") { + if (generatedVarId < 26) { // #a ~ #z + // @ts-ignore + (childNode).escapedText = "#" + String.fromCharCode(97 /* a */ + generatedVarId); + } else { + // @ts-ignore + (childNode).escapedText = "#" + (generatedVarId - 26); + } + generatedVarId++; + } + break; + } + } + makeAutoGeneratedNodeName(childNode); + }); +} + function getOutputBinName(node: ts.SourceFile) { let outputBinName = CmdOptions.getOutputBinName(); let fileName = node.fileName.substring(0, node.fileName.lastIndexOf('.')); -- Gitee