From c24fd61ba120293a9ea4e6953b12f8aa8842ce97 Mon Sep 17 00:00:00 2001 From: hufeng Date: Wed, 30 Mar 2022 17:25:36 +0800 Subject: [PATCH] Fix duplicate-named function's declaration in Scope Signed-off-by: hufeng Change-Id: I7374b57c8ab56c5ae378db43a0fa5afd6617a5dd --- ts2panda/src/recorder.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/ts2panda/src/recorder.ts b/ts2panda/src/recorder.ts index da2bb579c1..cc1f90981f 100644 --- a/ts2panda/src/recorder.ts +++ b/ts2panda/src/recorder.ts @@ -628,19 +628,22 @@ export class Recorder { let funcName = funcId ? jshelpers.getTextOfIdentifierOrLiteral(funcId) : '*default*'; let moduleKind = isExport ? ModuleVarKind.EXPORTED : ModuleVarKind.NOT; let funcDecl = new FuncDecl(funcName, node, moduleKind); - scope.setDecls(funcDecl); let hoistScope = scope; + let need2AddDecls: boolean = true; if (scope instanceof GlobalScope || scope instanceof ModuleScope) { this.collectHoistDecls(node, hoistScope, funcDecl); } else if (scope instanceof LocalScope) { hoistScope = scope.getNearestVariableScope(); let expectHoistScope = this.getScopeOfNode(node.parent.parent); if ((hoistScope == expectHoistScope) && (hoistScope instanceof FunctionScope)) { - this.collectHoistDecls(node, hoistScope, funcDecl); + need2AddDecls = this.collectHoistDecls(node, hoistScope, funcDecl); } } else { LOGD("Function declaration", " in function is collected in its body block"); } + if (need2AddDecls) { + scope.setDecls(funcDecl); + } } private recordOtherFunc(node: ts.FunctionLikeDeclaration, scope: Scope) { // functionlikedecalration except function declaration @@ -763,20 +766,17 @@ export class Recorder { return parameter.dotDotDotToken ? true : false; } - private collectHoistDecls(node: ts.Node, scope: VariableScope, decl: Decl) { + private collectHoistDecls(node: ts.Node, scope: VariableScope, decl: Decl): boolean { let declName = decl.name; // if variable share a same name with the parameter of its contained function, it should not be hoisted if (scope instanceof FunctionScope) { let nearestFunc = jshelpers.getContainingFunctionDeclaration(node); - if (!nearestFunc) { - return; - } let functionParameters = this.getParametersOfFunction(nearestFunc); if (functionParameters) { for (let i = 0; i < functionParameters.length; i++) { if (functionParameters[i].name == declName) { - return; + return false; } } } @@ -784,10 +784,11 @@ export class Recorder { // Variable named of global identifier should not be hoisted. if (isGlobalIdentifier(declName) && (scope instanceof GlobalScope)) { - return; + return true; } this.setHoistMap(scope, decl); + return false; } setScopeMap(node: ts.Node, scope: Scope) { -- Gitee