Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call StyleSheet / StyleSheetExit / Rule.custom.* in visitors passed to composeVisitors #875

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
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();