Skip to content

Commit

Permalink
loop over AST directly instead of wrapping in function call (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartveneman authored Oct 11, 2024
1 parent ac7f910 commit 50f42e4
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,6 @@ export function format(css, { minify = false } = {}) {
.replace(/calc\(([^*]*)\*([^*])/g, 'calc($1 * $2') // force correct whitespace around * in calc()
.replace(/\s+/g, SPACE) // collapse multiple whitespaces into one
.replace(/selector|url|supports|layer\(/ig, (match) => lowercase(match)) // lowercase function names

}

/** @param {import('css-tree').Declaration} node */
Expand Down Expand Up @@ -439,32 +438,26 @@ export function format(css, { minify = false } = {}) {
return indent(indent_level) + substr(node).trim()
}

/** @param {import('css-tree').CssNode} node */
function print(node) {
let buffer = EMPTY_STRING

/** @type {import('css-tree').List<import('css-tree').CssNode>} */
// @ts-expect-error Property 'children' does not exist on type 'AnPlusB', but we're never using that
let children = node.children

children.forEach((child, item) => {
if (child.type === TYPE_RULE) {
buffer += print_rule(child)
} else if (child.type === TYPE_ATRULE) {
buffer += print_atrule(child)
} else {
buffer += print_unknown(child, indent_level)
}
/** @type {import('css-tree').List<import('css-tree').CssNode>} */
// @ts-expect-error Property 'children' does not exist on type 'AnPlusB', but we're never using that
let children = ast.children
let buffer = EMPTY_STRING

if (item.next !== null) {
buffer += NEWLINE + NEWLINE
}
})
children.forEach((child, item) => {
if (child.type === TYPE_RULE) {
buffer += print_rule(child)
} else if (child.type === TYPE_ATRULE) {
buffer += print_atrule(child)
} else {
buffer += print_unknown(child, indent_level)
}

return buffer
}
if (item.next !== null) {
buffer += NEWLINE + NEWLINE
}
})

return print(ast)
return buffer
}

/**
Expand Down

0 comments on commit 50f42e4

Please sign in to comment.