Skip to content

Commit

Permalink
feat-fix(reconstruct): if-then-else now slices the array
Browse files Browse the repository at this point in the history
it doesn't `splice`
  • Loading branch information
EagleoutIce committed May 11, 2024
1 parent a203042 commit a7fc3f7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/dataflow/environments/built-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ registerBuiltInFunctions(['<<-'], processAssignment,
registerBuiltInFunctions(['->'], processAssignment, { swapSourceAndTarget: true, canBeReplacement: true }, )
registerBuiltInFunctions(['->>'], processAssignment, { superAssignment: true, swapSourceAndTarget: true, canBeReplacement: true } )
registerBuiltInFunctions(['&&', '||', '&', '|'], processSpecialBinOp, { lazy: true } )
registerBuiltInFunctions(['|>'], processPipe, {}, )
registerBuiltInFunctions(['|>', '%>%'], processPipe, {}, )
registerBuiltInFunctions(['function', '\\'], processFunctionDefinition, {}, )
registerBuiltInFunctions(['quote', 'substitute', 'bquote'], processQuote, { quoteArgumentsWithIndex: 0 }, )
registerBuiltInFunctions(['for'], processForLoop, {}, )
Expand Down
2 changes: 1 addition & 1 deletion src/reconstruct/reconstruct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ function reconstructIfThenElse(ifThenElse: RIfThenElse<ParentInformation>, condi
return otherwise
}
} else {
const thenRemainder = indentBy(then.splice(1), 1)
const thenRemainder = indentBy(then.slice(1), 1)
if(thenRemainder.length > 0) {
if(!thenRemainder[thenRemainder.length - 1].line.trim().endsWith('else')) {
thenRemainder[thenRemainder.length - 1].line += ' else '
Expand Down
2 changes: 1 addition & 1 deletion src/slicing/criterion/filters/all-variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const defaultAllVariablesCollectorFolds: FoldFunctions<ParentInformation, NodeId
if(c.flavor === 'named') {
return c.functionName.content === 'library' ? args.slice(1) : args
} else {
return [...a, ...args]
return [...a.filter(x => x !== EmptyArgument), ...args]

Check warning on line 49 in src/slicing/criterion/filters/all-variables.ts

View check run for this annotation

Codecov / codecov/patch

src/slicing/criterion/filters/all-variables.ts#L49

Added line #L49 was not covered by tests
}
},
foldArgument: (_: unknown, _a: unknown, b: NodeId[] | undefined) => b ?? [],
Expand Down
24 changes: 24 additions & 0 deletions test/functionality/slicing/static-program-slices/calls-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,5 +484,29 @@ cat(4 %a% 5)`)
assertSliced(label('Slice for variable in last filter', caps),
shell, code, ['12@Y'], 'Y')
})
describe('if-then-else formattings', () => {
const caps: SupportedFlowrCapabilityId[] = ['name-normal', ...OperatorDatabase['<-'].capabilities, 'numbers', 'if', 'logical', 'binary-operator', 'infix-calls', 'call-normal', 'newlines', 'unnamed-arguments', 'precedence']
const code = `x <- 3
{
if (x == 3)
{ x <- 4
y <- 2 }
else { x <- y <- 3 }
}
print(x)
`
assertSliced(label('Slice for initial x should return noting else', caps),
shell, code, ['1@x'], 'x <- 3')
assertSliced(label('Slice for first condition', caps),
shell, code, ['3@x'], 'x <- 3\nx')
assertSliced(label('Slice for last x', caps),
shell, code, ['8@x'], `x <- 3
if(x == 3) {
x <- 4
y <- 2
} else
{ x <- y <- 3 }
x`)
})
})
}))

0 comments on commit a7fc3f7

Please sign in to comment.