From 79b1755b104238c48d6d24b73d3b2a605a1a8359 Mon Sep 17 00:00:00 2001 From: dingxiaohuan Date: Fri, 28 Feb 2025 16:15:40 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20CodeBuilder=20?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E5=BE=AA=E7=8E=AF=E4=BE=9D=E8=B5=96=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E5=92=8C=E4=BB=A3=E7=A0=81=E5=9D=97=E5=A4=84=E7=90=86?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/generator/CodeBuilder.ts | 56 +++++++++---------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/modules/code-generator/src/generator/CodeBuilder.ts b/modules/code-generator/src/generator/CodeBuilder.ts index e633babbf5..2264676685 100644 --- a/modules/code-generator/src/generator/CodeBuilder.ts +++ b/modules/code-generator/src/generator/CodeBuilder.ts @@ -28,7 +28,6 @@ export class CodeBuilder implements ICodeBuilder { if (chunks.length <= 0) { return ''; } - const unprocessedChunks = chunks.map((chunk) => { return { name: chunk.name, @@ -39,38 +38,35 @@ export class CodeBuilder implements ICodeBuilder { }); const resultingString: string[] = []; + const processed = new Set(); while (unprocessedChunks.length > 0) { - let indexToRemove = 0; - for (let index = 0; index < unprocessedChunks.length; index++) { - if (unprocessedChunks[index].linkAfter.length <= 0) { - indexToRemove = index; - break; + let indexToRemove = -1; + for (let index = 0; index < unprocessedChunks.length; index++) { + if (unprocessedChunks[index].linkAfter.length === 0) { + indexToRemove = index; + break; + } + } + + if (indexToRemove === -1) { + throw new CodeGeneratorError( + 'Operation aborted. Reason: cyclic dependency between chunks.', + ); } - } - - if (unprocessedChunks[indexToRemove].linkAfter.length > 0) { - throw new CodeGeneratorError( - 'Operation aborted. Reason: cyclic dependency between chunks.', - ); - } - - const { type, content, name } = unprocessedChunks[indexToRemove]; - const compiledContent = this.generateByType(type, content); - if (compiledContent) { - resultingString.push(`${compiledContent}\n`); - } - - unprocessedChunks.splice(indexToRemove, 1); - if (!unprocessedChunks.some((ch) => ch.name === name)) { - unprocessedChunks.forEach( - // remove the processed chunk from all the linkAfter arrays from the remaining chunks - (ch) => { - // eslint-disable-next-line no-param-reassign - ch.linkAfter = ch.linkAfter.filter((after) => after !== name); - }, - ); - } + + const { type, content, name } = unprocessedChunks[indexToRemove]; + const compiledContent = this.generateByType(type, content); + if (compiledContent) { + resultingString.push(`${compiledContent}\n`); + } + + processed.add(name); + unprocessedChunks.splice(indexToRemove, 1); + + unprocessedChunks.forEach((ch) => { + ch.linkAfter = ch.linkAfter.filter((after) => !processed.has(after)); + }); } return resultingString.join('\n');