Fix for code enabling/disabling output context on single node (#8776)

This commit is contained in:
Michael Mauderer 2024-01-17 10:18:40 +00:00 committed by GitHub
parent 184128949e
commit 080690b3ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 18 deletions

View File

@ -179,24 +179,23 @@ const isOutputContextOverridden = computed({
set(shouldOverride) {
const module = projectStore.module
if (!module) return
const replacements = shouldOverride
? [Ast.TextLiteral.new(projectStore.executionMode)]
: undefined
const edit = props.node.rootSpan.module.edit()
const newAst = prefixes.modify(
edit,
props.node.rootSpan,
projectStore.isOutputContextEnabled
? {
enableOutputContext: undefined,
disableOutputContext: replacements,
}
: {
enableOutputContext: replacements,
disableOutputContext: undefined,
},
)
graph.setNodeContent(props.node.rootSpan.exprId, newAst.code())
const replacementText = shouldOverride
? [Ast.TextLiteral.new(projectStore.executionMode, edit)]
: undefined
const replacements = projectStore.isOutputContextEnabled
? {
enableOutputContext: undefined,
disableOutputContext: replacementText,
}
: {
enableOutputContext: replacementText,
disableOutputContext: undefined,
}
const expression = props.node.rootSpan
const newAst = prefixes.modify(edit, expression, replacements)
const code = newAst.code()
graph.setNodeContent(props.node.rootSpan.exprId, code)
},
})

View File

@ -106,4 +106,11 @@ test.each([
const intron = Ast.parse(source, edit)
const instantiated = pattern.instantiate(edit, [intron.exprId])
expect(instantiated.code(edit)).toBe(result)
// Check that `instantiate` has not affected the base module.
const intron2 = Ast.parse(source, edit)
const originalParent = intron2.parent
const edit2 = edit.edit()
pattern.instantiate(edit2, [intron2.exprId])
expect(edit.get(intron2.exprId)!.parent).toBe(originalParent)
})

View File

@ -1,3 +1,4 @@
import { assert } from '@/util/assert'
import { Ast } from '@/util/ast'
import { MutableModule } from '@/util/ast/abstract'
@ -44,7 +45,14 @@ export class Pattern {
for (const matched of placeholders(ast, this.placeholder)) {
const replacement = subtrees.shift()
if (replacement === undefined) break
edit.get(replacement)!.parent = matched.parent
const replacementAst = edit.splice(edit.get(replacement))
if (replacementAst === null) {
console.error(
'Subtree ID provided to `instantiate` is not accessible in the `edit` module.',
)
continue
}
replacementAst.parent = matched.parent
matched.ref.node = replacement
}
return ast