From f312de6b04dd2e4757c321fffff267af385e7767 Mon Sep 17 00:00:00 2001 From: Bart Veneman <1536852+bartveneman@users.noreply.github.com> Date: Fri, 11 Oct 2024 16:28:50 +0200 Subject: [PATCH] loop over AST directly instead of wrapping in function call --- index.js | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/index.js b/index.js index 975fe67..88b44d3 100644 --- a/index.js +++ b/index.js @@ -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 */ @@ -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} */ - // @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} */ + // @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 } /**