Skip to content

Commit

Permalink
Merge pull request #1740 from thecrypticace/fix/normalization-append-…
Browse files Browse the repository at this point in the history
…error

Fix `append()` failing after mutating nodes through .parent
  • Loading branch information
ai committed Apr 30, 2022
2 parents 2713d8d + 2955628 commit 8a1f6c2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/container.js
Expand Up @@ -326,7 +326,7 @@ class Container extends Node {
i.raws.before = sample.raws.before.replace(/\S/g, '')
}
}
i.parent = this
i.parent = this.proxyOf
return i
})

Expand Down
47 changes: 47 additions & 0 deletions test/visitor.test.ts
Expand Up @@ -7,6 +7,7 @@ import postcss, {
Container,
Root,
Rule,
AtRule,
Declaration,
Plugin,
PluginCreator,
Expand Down Expand Up @@ -1552,4 +1553,50 @@ test('marks cleaned nodes as dirty on moving in a document', () => {
])
})

test('append works after reassigning nodes through .parent', async () => {
let plugin: Plugin = {
postcssPlugin: 'test',

Rule(rule) {
if (
!(
rule.selector === '.nested' &&
rule.nodes.length === 1 &&
rule.nodes[0].type === 'atrule'
)
) {
return
}

let atrule = rule.nodes[0]

atrule.append(rule.clone({ nodes: [] }).append(...atrule.nodes))

rule.after(atrule)
rule.remove()
},

OnceExit(root) {
let firstNode = root.nodes[0] as AtRule
let secondNode = root.nodes[1] as AtRule
let rule2 = secondNode.nodes[0]
rule2.parent!.nodes = rule2.parent!.nodes
firstNode.append(...secondNode.nodes)
secondNode.remove()
}
}

let { css } = await postcss([plugin]).process(
postcss.parse(
`@media (min-width: 640px) { .page { width: auto; } } .nested { @media (min-width: 640px) { width: auto; } }`
),
{ from: 'whatever' }
)

is(
css,
'@media (min-width: 640px) { .page { width: auto; } .nested { width: auto } }'
)
})

test.run()

0 comments on commit 8a1f6c2

Please sign in to comment.