Skip to content

Commit

Permalink
fix csstools/postcss-nesting#82: use the RuleExit listener (csstools/…
Browse files Browse the repository at this point in the history
  • Loading branch information
schelmo authored and romainmenke committed Nov 16, 2021
1 parent a7d9894 commit 2d66323
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
20 changes: 16 additions & 4 deletions plugins/postcss-nesting/.bin/test-tape.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,30 @@ export default async function tape() {
failures += await test('ignores invalid entries', { basename: 'ignore' })
failures += await test('supports complex entries', { basename: 'complex' })

let mixinPlugin = () => {
return {
postcssPlugin: 'mixin',
AtRule: {
mixin(node) {
node.replaceWith('& .in{ &.deep { color: blue; }}')
},
},
}
}
mixinPlugin.postcss = true
failures += await test('supports other visitors', { basename: 'mixin' }, mixinPlugin)

return failures === 0
}

async function test(name, init) {
async function test(name, init, ...plugins) {
const { basename } = Object(init)

let sourceUrl = new URL(`test/${basename}.css`, workingUrl)
let expectUrl = new URL(`test/${basename}.expect.css`, workingUrl)
let resultUrl = new URL(`test/${basename}.result.css`, workingUrl)

let plugins = [plugin]
plugins.unshift(plugin)

let sourceCss = await fs.readFile(sourceUrl, 'utf8')
let expectCss = await fs.readFile(expectUrl, 'utf8')
Expand All @@ -49,10 +62,9 @@ async function test(name, init) {
}
}


if (new URL(process.argv[1], 'file:').href === import.meta.url) {
tape().then(
() => process.exit(0),
() => process.exit(1)
() => process.exit(1),
)
}
26 changes: 12 additions & 14 deletions plugins/postcss-nesting/src/lib/walk.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ import transformAtruleWithinRule, { isAtruleWithinRule } from './atrule-within-r
import transformAtruleWithinAtrule, { isAtruleWithinAtrule } from './atrule-within-atrule.js'

export default function walk(node) {
node.nodes.slice(0).forEach((child) => {
if (child.parent === node) {
if (isRuleWithinRule(child)) {
transformRuleWithinRule(child)
} else if (isNestRuleWithinRule(child)) {
transformNestRuleWithinRule(child)
} else if (isAtruleWithinRule(child)) {
transformAtruleWithinRule(child)
} else if (isAtruleWithinAtrule(child)) {
transformAtruleWithinAtrule(child)
}
node.each((child) => {
if (isRuleWithinRule(child)) {
transformRuleWithinRule(child)
} else if (isNestRuleWithinRule(child)) {
transformNestRuleWithinRule(child)
} else if (isAtruleWithinRule(child)) {
transformAtruleWithinRule(child)
} else if (isAtruleWithinAtrule(child)) {
transformAtruleWithinAtrule(child)
}

if (Object(child.nodes).length) {
walk(child)
}
if (Object(child.nodes).length) {
walk(child)
}
})
}
4 changes: 2 additions & 2 deletions plugins/postcss-nesting/src/postcss-8-nesting.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import walk from './lib/walk.js'
export default function postcssNesting() {
return {
postcssPlugin: 'postcss-nesting',
Once(root) {
walk(root)
RuleExit(rule) {
walk(rule)
},
}
}
Expand Down
6 changes: 6 additions & 0 deletions plugins/postcss-nesting/test/mixin.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
a {
& b {
color: red;
}
@mixin;
}
5 changes: 5 additions & 0 deletions plugins/postcss-nesting/test/mixin.expect.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

a b {
color: red;
}
a .in.deep { color: blue; }

0 comments on commit 2d66323

Please sign in to comment.