Skip to content

fix: 修复 CodeBuilder 中的循环依赖检测和代码块处理逻辑 #3130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 26 additions & 30 deletions modules/code-generator/src/generator/CodeBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export class CodeBuilder implements ICodeBuilder {
if (chunks.length <= 0) {
return '';
}

const unprocessedChunks = chunks.map((chunk) => {
return {
name: chunk.name,
Expand All @@ -39,38 +38,35 @@ export class CodeBuilder implements ICodeBuilder {
});

const resultingString: string[] = [];
const processed = new Set<string>();

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');
Expand Down