Skip to content
This repository has been archived by the owner on Dec 28, 2021. It is now read-only.

fix #82: use the RuleExit listener #86

Merged
merged 1 commit into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions .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 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 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 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 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; }