Skip to content

Commit

Permalink
Call StyleSheet / StyleSheetExit in visitors passed to composeVisitors
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Dec 25, 2024
1 parent 7f29035 commit fa63311
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 28 deletions.
58 changes: 30 additions & 28 deletions node/composeVisitors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/**
* Composes multiple visitor objects into a single one.
* @param {Visitor[]} visitors
* @param {Visitor[]} visitors
* @return {Visitor}
*/
function composeVisitors(visitors) {
Expand All @@ -13,6 +13,8 @@ function composeVisitors(visitors) {

/** @type Visitor */
let res = {};
composeSimpleVisitors(res, visitors, 'StyleSheet');
composeSimpleVisitors(res, visitors, 'StyleSheetExit');
composeObjectVisitors(res, visitors, 'Rule', ruleVisitor, wrapUnknownAtRule);
composeObjectVisitors(res, visitors, 'RuleExit', ruleVisitor, wrapUnknownAtRule);
composeObjectVisitors(res, visitors, 'Declaration', declarationVisitor, wrapCustomProperty);
Expand Down Expand Up @@ -54,8 +56,8 @@ function wrapCustomProperty(k, f) {
}

/**
* @param {import('./index').Visitor['Rule']} f
* @param {import('./ast').Rule} item
* @param {import('./index').Visitor['Rule']} f
* @param {import('./ast').Rule} item
*/
function ruleVisitor(f, item) {
if (typeof f === 'object') {
Expand All @@ -72,8 +74,8 @@ function ruleVisitor(f, item) {
}

/**
* @param {import('./index').Visitor['Declaration']} f
* @param {import('./ast').Declaration} item
* @param {import('./index').Visitor['Declaration']} f
* @param {import('./ast').Declaration} item
*/
function declarationVisitor(f, item) {
if (typeof f === 'object') {
Expand All @@ -94,9 +96,9 @@ function declarationVisitor(f, item) {
}

/**
*
* @param {Visitor[]} visitors
* @param {string} key
*
* @param {Visitor[]} visitors
* @param {string} key
* @returns {[any[], boolean, Set<string>]}
*/
function extractObjectsOrFunctions(visitors, key) {
Expand Down Expand Up @@ -124,8 +126,8 @@ function extractObjectsOrFunctions(visitors, key) {
* @param {Visitor} res
* @param {Visitor[]} visitors
* @param {K} key
* @param {(visitor: Visitor[K], item: any) => any | any[] | void} apply
* @param {(k: string, f: any) => any} wrapKey
* @param {(visitor: Visitor[K], item: any) => any | any[] | void} apply
* @param {(k: string, f: any) => any} wrapKey
*/
function composeObjectVisitors(res, visitors, key, apply, wrapKey) {
let [values, hasFunction, allKeys] = extractObjectsOrFunctions(visitors, key);
Expand All @@ -152,11 +154,11 @@ function composeObjectVisitors(res, visitors, key, apply, wrapKey) {
}

/**
* @param {Visitor} res
* @param {Visitor[]} visitors
* @param {string} key
* @param {import('./ast').TokenOrValue['type']} type
* @param {boolean} isExit
* @param {Visitor} res
* @param {Visitor[]} visitors
* @param {string} key
* @param {import('./ast').TokenOrValue['type']} type
* @param {boolean} isExit
*/
function composeTokenVisitors(res, visitors, key, type, isExit) {
let [values, hasFunction, allKeys] = extractObjectsOrFunctions(visitors, key);
Expand All @@ -182,8 +184,8 @@ function composeTokenVisitors(res, visitors, key, type, isExit) {
}

/**
* @param {Visitor[]} visitors
* @param {import('./ast').TokenOrValue['type']} type
* @param {Visitor[]} visitors
* @param {import('./ast').TokenOrValue['type']} type
*/
function createTokenVisitor(visitors, type, isExit) {
let v = createArrayVisitor(visitors, (visitor, /** @type {import('./ast').TokenOrValue} */ item) => {
Expand Down Expand Up @@ -271,8 +273,8 @@ function createTokenVisitor(visitors, type, isExit) {
}

/**
* @param {Visitor[]} visitors
* @param {string} key
* @param {Visitor[]} visitors
* @param {string} key
*/
function extractFunctions(visitors, key) {
let functions = [];
Expand All @@ -286,9 +288,9 @@ function extractFunctions(visitors, key) {
}

/**
* @param {Visitor} res
* @param {Visitor[]} visitors
* @param {string} key
* @param {Visitor} res
* @param {Visitor[]} visitors
* @param {string} key
*/
function composeSimpleVisitors(res, visitors, key) {
let functions = extractFunctions(visitors, key);
Expand Down Expand Up @@ -316,9 +318,9 @@ function composeSimpleVisitors(res, visitors, key) {
}

/**
* @param {Visitor} res
* @param {Visitor[]} visitors
* @param {string} key
* @param {Visitor} res
* @param {Visitor[]} visitors
* @param {string} key
*/
function composeArrayFunctions(res, visitors, key) {
let functions = extractFunctions(visitors, key);
Expand All @@ -337,8 +339,8 @@ function composeArrayFunctions(res, visitors, key) {
/**
* @template T
* @template V
* @param {T[]} visitors
* @param {(visitor: T, item: V) => V | V[] | void} apply
* @param {T[]} visitors
* @param {(visitor: T, item: V) => V | V[] | void} apply
* @returns {(item: V) => V | V[] | void}
*/
function createArrayVisitor(visitors, apply) {
Expand All @@ -350,7 +352,7 @@ function createArrayVisitor(visitors, apply) {
for (let i = 0; i < arr.length; i++) {
// For each value, call all visitors. If a visitor returns a new value,
// we start over, but skip the visitor that generated the value or saw
// it before (to avoid cycles). This way, visitors can be composed in any order.
// it before (to avoid cycles). This way, visitors can be composed in any order.
for (let v = 0; v < visitors.length;) {
if (seen.get(v)) {
v++;
Expand Down
33 changes: 33 additions & 0 deletions node/test/composeVisitors.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -686,4 +686,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 fa63311

Please sign in to comment.