fix(linker): stop appended/prepended blocks rendering twice when nested - #3475
Open
afonsojanu wants to merge 1 commit into
Open
fix(linker): stop appended/prepended blocks rendering twice when nested#3475afonsojanu wants to merge 1 commit into
afonsojanu wants to merge 1 commit into
Conversation
When a block declared with append, prepend or replace shows up somewhere other than the top level of a template (for example one pulled in through an include), its content was left sitting in its original spot in the tree in addition to being merged into the matching parent block. That meant it rendered twice. Fixes pugjs#3158.
|
There is no change log for this pull request yet. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This fixes #3158, which has been open since 2019.
The repro from that issue still happens on current master: including a template that appends to a layout block causes the appended content to show up twice, once in the layout block where it belongs and once again right where the
includewas.It's not actually specific to includes. The same thing happens with any
block append/prepend/replacethat ends up nested somewhere other than the top level of a template, for example:pug-linker'sextend()walks the whole tree looking for named blocks, and when it finds one matching a block declared further up the extends chain, it correctly merges that block's content into the target. What it didn't do was clear the block out from its own original spot in the tree, so anything code-gen visits afterward (which just treats a leftoverNamedBlockthe same as a plainBlock) renders it a second time.The fix clears a block's own
nodesonce its content has been merged elsewhere, but only after the walk has finished descending into it, otherwise a block nested even deeper inside it (again, this can happen through an include) never gets visited at all.I added an end-to-end case reproducing the exact scenario from the issue (
packages/pug/test/cases/append-inside-include.*), plus updated a handful ofpug-linkersnapshot tests. Those snapshot diffs are limited todeclaredBlocks, an internal bookkeeping structure the linker builds for chainedextends— I checked that nothing outsidepug-linkeritself reads that property, and traced through a 3-level extends chain by hand to confirm the actual chaining behavior (matching a block declared two levels up) is unaffected.Ran the full suite (
npx jest) before and after; the only failures either way are two pre-existingerror.reportingtests that expect an older Node.js error message format, unrelated to this change.