From fc4065a632b497880ee1e34739e63b4404e7cfcf Mon Sep 17 00:00:00 2001 From: Luis Soares <57358121+luis-soares-sky@users.noreply.github.com> Date: Sun, 7 Jan 2024 17:59:44 +0000 Subject: [PATCH] Fix Rooibos_init injection causing duplicate calls (#247) * Remove unused imports * Introduce init call search and check --- bsc-plugin/src/lib/rooibos/RooibosSession.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/bsc-plugin/src/lib/rooibos/RooibosSession.ts b/bsc-plugin/src/lib/rooibos/RooibosSession.ts index f54291d9..53e7fa88 100644 --- a/bsc-plugin/src/lib/rooibos/RooibosSession.ts +++ b/bsc-plugin/src/lib/rooibos/RooibosSession.ts @@ -1,6 +1,6 @@ import * as path from 'path'; import type { BrsFile, BscFile, ClassStatement, FunctionStatement, NamespaceStatement, Program, ProgramBuilder, Scope, Statement } from 'brighterscript'; -import { isBrsFile, ParseMode, util } from 'brighterscript'; +import { isBrsFile, isCallExpression, isVariableExpression, ParseMode, WalkMode } from 'brighterscript'; import type { AstEditor } from 'brighterscript/dist/astUtils/AstEditor'; import type { RooibosConfig } from './RooibosConfig'; import { SessionInfo } from './RooibosSessionInfo'; @@ -10,7 +10,6 @@ import type { FileFactory } from './FileFactory'; import type { TestSuite } from './TestSuite'; import { diagnosticErrorNoMainFound as diagnosticWarnNoMainFound, diagnosticNoStagingDir } from '../utils/Diagnostics'; import undent from 'undent'; -import { BrsTranspileState } from 'brighterscript/dist/parser/BrsTranspileState'; import * as fsExtra from 'fs-extra'; import type { MockUtil } from './MockUtil'; @@ -89,7 +88,12 @@ export class RooibosSession { } } if (mainFunction) { - editor.addToArray(mainFunction.func.body.statements, 0, new RawCodeStatement(`Rooibos_init("${this.config?.testSceneName ?? 'RooibosScene'}")`)); + const initCall = mainFunction.func.body.findChild(f => isCallExpression(f) && isVariableExpression(f.callee) && f.callee.name.text.toLowerCase() === 'rooibos_init', { + walkMode: WalkMode.visitAllRecursive + }); + if (!initCall) { + editor.addToArray(mainFunction.func.body.statements, 0, new RawCodeStatement(`Rooibos_init("${this.config?.testSceneName ?? 'RooibosScene'}")`)); + } } } addLaunchHookFileIfNotPresent() {