Skip to content

Commit

Permalink
Generate node's own label before descendant labels
Browse files Browse the repository at this point in the history
  • Loading branch information
aajanki committed Nov 1, 2024
1 parent 6368d2b commit 1b37243
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
9 changes: 6 additions & 3 deletions src/ast/steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ function namedStepsParallel(
generateName: (prefix: string) => string,
) {
let steps: Record<StepName, StepsStepASTNamed> | ForStepASTNamed
const mainLabel = step.label ?? generateName('parallel')
if (!isRecord(step.steps)) {
const forStep = namedSteps(step.steps, generateName).step

Expand All @@ -536,7 +537,7 @@ function namedStepsParallel(
}

return {
name: step.label ?? generateName('parallel'),
name: mainLabel,
step: new ParallelStepASTNamed(
steps,
step.shared,
Expand All @@ -562,14 +563,15 @@ function namedStepsSwitch(
step: SwitchStepAST,
generateName: (prefix: string) => string,
): NamedWorkflowStep {
const mainLabel = step.label ?? generateName('switch')
const namedBranches = step.branches.map((branch) => ({
condition: branch.condition,
steps: branch.steps.map((nested) => namedSteps(nested, generateName)),
next: branch.next,
}))

return {
name: step.label ?? generateName('switch'),
name: mainLabel,
step: new SwitchStepASTNamed(namedBranches),
}
}
Expand All @@ -578,6 +580,7 @@ function namedStepsTry(
step: TryStepAST,
generateName: (prefix: string) => string,
) {
const mainLabel = step.label ?? generateName('try')
const namedTrySteps = step.trySteps.map((nested) =>
namedSteps(nested, generateName),
)
Expand All @@ -586,7 +589,7 @@ function namedStepsTry(
)

return {
name: step.label ?? generateName('try'),
name: mainLabel,
step: new TryStepASTNamed(
namedTrySteps,
namedExceptSteps,
Expand Down
28 changes: 14 additions & 14 deletions test/transpiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2299,7 +2299,7 @@ describe('Loops', () => {
assertTranspiled(code, expected)
})

it('transpiles a while loop with a single-stetment body', () => {
it('transpiles a while loop with a single-statement body', () => {
const code = `
function main() {
const i = 5;
Expand Down Expand Up @@ -2350,15 +2350,15 @@ describe('Loops', () => {
- assign1:
assign:
- x: 0.11
- switch3:
- switch1:
switch:
- condition: \${x >= 0}
steps:
- switch1:
- switch2:
switch:
- condition: \${x > 0.9}
next: return1
- switch2:
- switch3:
switch:
- condition: \${x < 0.5}
steps:
Expand All @@ -2371,15 +2371,15 @@ describe('Loops', () => {
assign:
- x: \${2 * (1 - x)}
- next1:
next: switch3
next: switch1
- return1:
return: \${x}
`

assertTranspiled(code, expected)
})

it('transpiles a break and a while loop as the statement in a block', () => {
it('transpiles a break and a while loop as the last statement in a block', () => {
const code = `
function main() {
const x = 0.11;
Expand All @@ -2402,15 +2402,15 @@ describe('Loops', () => {
- assign1:
assign:
- x: 0.11
- switch3:
- switch1:
switch:
- condition: \${x >= 0}
steps:
- switch1:
- switch2:
switch:
- condition: \${x > 0.9}
next: end
- switch2:
- switch3:
switch:
- condition: \${x < 0.5}
steps:
Expand All @@ -2423,7 +2423,7 @@ describe('Loops', () => {
assign:
- x: \${2 * (1 - x)}
- next1:
next: switch3
next: switch1
`

assertTranspiled(code, expected)
Expand Down Expand Up @@ -2451,22 +2451,22 @@ describe('Loops', () => {
- assign1:
assign:
- x: 0.11
- switch2:
- switch1:
switch:
- condition: \${x < 0.9}
steps:
- switch1:
- switch2:
switch:
- condition: \${x < 0.5}
steps:
- assign2:
assign:
- x: \${x * 2}
next: switch2
next: switch1
- assign3:
assign:
- x: \${2 * (1 - x)}
next: switch2
next: switch1
- return1:
return: \${x}
`
Expand Down

0 comments on commit 1b37243

Please sign in to comment.