Skip to content

Commit

Permalink
Merge branch 'master' of github.com:parcel-bundler/lightningcss
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Jan 9, 2025
2 parents e5c575d + fbae126 commit 222fa9f
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 4 deletions.
23 changes: 19 additions & 4 deletions node/composeVisitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ function composeVisitors(visitors) {

/** @type Visitor */
let res = {};
composeObjectVisitors(res, visitors, 'Rule', ruleVisitor, wrapUnknownAtRule);
composeObjectVisitors(res, visitors, 'RuleExit', ruleVisitor, wrapUnknownAtRule);
composeSimpleVisitors(res, visitors, 'StyleSheet');
composeSimpleVisitors(res, visitors, 'StyleSheetExit');
composeObjectVisitors(res, visitors, 'Rule', ruleVisitor, wrapCustomAndUnknownAtRule);
composeObjectVisitors(res, visitors, 'RuleExit', ruleVisitor, wrapCustomAndUnknownAtRule);
composeObjectVisitors(res, visitors, 'Declaration', declarationVisitor, wrapCustomProperty);
composeObjectVisitors(res, visitors, 'DeclarationExit', declarationVisitor, wrapCustomProperty);
composeSimpleVisitors(res, visitors, 'Url');
Expand Down Expand Up @@ -45,8 +47,14 @@ function composeVisitors(visitors) {

module.exports = composeVisitors;

function wrapUnknownAtRule(k, f) {
return k === 'unknown' ? (value => f({ type: 'unknown', value })) : f;
function wrapCustomAndUnknownAtRule(k, f) {
if (k === 'unknown') {
return (value => f({ type: 'unknown', value }));
}
if (k === 'custom') {
return (value => f({ type: 'custom', value }));
}
return f;
}

function wrapCustomProperty(k, f) {
Expand All @@ -66,6 +74,13 @@ function ruleVisitor(f, item) {
}
return v?.(item.value);
}
if (item.type === 'custom') {
let v = f.custom;
if (typeof v === 'object') {
v = v[item.value.name];
}
return v?.(item.value);
}
return f[item.type]?.(item);
}
return f?.(item);
Expand Down
114 changes: 114 additions & 0 deletions node/test/composeVisitors.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,87 @@ test('unknown rules', () => {
assert.equal(res.code.toString(), '.menu_link{background:#056ef0}');
});

test('custom at rules', () => {
let res = transform({
filename: 'test.css',
minify: true,
code: Buffer.from(`
@testA;
@testB;
`),
customAtRules: {
testA: {},
testB: {}
},
visitor: composeVisitors([
{
Rule: {
custom: {
testA(rule) {
return {
type: 'style',
value: {
loc: rule.loc,
selectors: [
[{ type: 'class', name: 'testA' }]
],
declarations: {
declarations: [
{
property: 'color',
value: {
type: 'rgb',
r: 0xff,
g: 0x00,
b: 0x00,
alpha: 1,
}
}
]
}
}
};
}
}
}
},
{
Rule: {
custom: {
testB(rule) {
return {
type: 'style',
value: {
loc: rule.loc,
selectors: [
[{ type: 'class', name: 'testB' }]
],
declarations: {
declarations: [
{
property: 'color',
value: {
type: 'rgb',
r: 0x00,
g: 0xff,
b: 0x00,
alpha: 1,
}
}
]
}
}
};
}
}
}
}
])
});

assert.equal(res.code.toString(), '.testA{color:red}.testB{color:#0f0}');
});

test('known rules', () => {
let declared = new Map();
let res = transform({
Expand Down Expand Up @@ -686,4 +767,37 @@ test('variables', () => {
assert.equal(res.code.toString(), 'body{padding:20px;width:600px}');
});

test('StyleSheet', () => {
let styleSheetCalledCount = 0;
let styleSheetExitCalledCount = 0;
transform({
filename: 'test.css',
code: Buffer.from(`
body {
color: blue;
}
`),
visitor: composeVisitors([
{
StyleSheet() {
styleSheetCalledCount++
},
StyleSheetExit() {
styleSheetExitCalledCount++
}
},
{
StyleSheet() {
styleSheetCalledCount++
},
StyleSheetExit() {
styleSheetExitCalledCount++
}
}
])
});
assert.equal(styleSheetCalledCount, 2);
assert.equal(styleSheetExitCalledCount, 2);
});

test.run();

0 comments on commit 222fa9f

Please sign in to comment.