Skip to content

Commit

Permalink
move indent-level to upper scope (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartveneman authored Jul 26, 2024
1 parent 00fac14 commit 85c0ac2
Showing 1 changed file with 19 additions and 24 deletions.
43 changes: 19 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export function format(css, { minify = false } = {}) {
const OPTIONAL_SPACE = minify ? EMPTY_STRING : SPACE;
const LAST_SEMICOLON = minify ? EMPTY_STRING : SEMICOLON;

let indent_level = 0

/**
* Indent a string
* @param {number} size
Expand Down Expand Up @@ -86,40 +88,38 @@ export function format(css, { minify = false } = {}) {

/**
* @param {import('css-tree').Rule} node
* @param {number} indent_level
* @returns {string} A formatted Rule
*/
function print_rule(node, indent_level) {
function print_rule(node) {
let buffer;
let prelude = node.prelude;
let block = node.block;

if (prelude.type === TYPE_SELECTORLIST) {
buffer = print_selectorlist(prelude, indent_level);
buffer = print_selectorlist(prelude);
} else {
// In case parsing the selector list fails we'll print it as-is
buffer = print_unknown(prelude, indent_level);
}

if (block.type === TYPE_BLOCK) {
buffer += print_block(block, indent_level);
buffer += print_block(block);
}

return buffer;
}

/**
* @param {import('css-tree').SelectorList} node
* @param {number} indent_level
* @returns {string} A formatted SelectorList
*/
function print_selectorlist(node, indent_level) {
function print_selectorlist(node) {
let buffer = EMPTY_STRING;
let children = node.children;

children.forEach((selector, item) => {
if (selector.type === TYPE_SELECTOR) {
buffer += print_selector(selector, indent_level);
buffer += print_selector(selector);
}

if (item.next !== null) {
Expand Down Expand Up @@ -231,19 +231,17 @@ export function format(css, { minify = false } = {}) {

/**
* @param {import('css-tree').Selector} node
* @param {number} indent_level
* @returns {string} A formatted Selector
*/
function print_selector(node, indent_level) {
function print_selector(node) {
return indent(indent_level) + print_simple_selector(node);
}

/**
* @param {import('css-tree').Block} node
* @param {number} indent_level
* @returns {string} A formatted Block
*/
function print_block(node, indent_level) {
function print_block(node) {
let children = node.children;
let buffer = OPTIONAL_SPACE;

Expand All @@ -257,7 +255,7 @@ export function format(css, { minify = false } = {}) {

children.forEach((child, item) => {
if (child.type === TYPE_DECLARATION) {
buffer += print_declaration(child, indent_level);
buffer += print_declaration(child);

if (item.next === null) {
buffer += LAST_SEMICOLON;
Expand All @@ -270,9 +268,9 @@ export function format(css, { minify = false } = {}) {
}

if (child.type === TYPE_RULE) {
buffer += print_rule(child, indent_level);
buffer += print_rule(child);
} else if (child.type === TYPE_ATRULE) {
buffer += print_atrule(child, indent_level);
buffer += print_atrule(child);
} else {
buffer += print_unknown(child, indent_level);
}
Expand All @@ -297,10 +295,9 @@ export function format(css, { minify = false } = {}) {

/**
* @param {import('css-tree').Atrule} node
* @param {number} indent_level
* @returns {string} A formatted Atrule
*/
function print_atrule(node, indent_level) {
function print_atrule(node) {
let buffer = indent(indent_level) + "@";
let prelude = node.prelude;
let block = node.block;
Expand All @@ -315,7 +312,7 @@ export function format(css, { minify = false } = {}) {
// `@import url(style.css);` has no block, neither does `@layer layer1;`
buffer += SEMICOLON;
} else if (block.type === TYPE_BLOCK) {
buffer += print_block(block, indent_level);
buffer += print_block(block);
}

return buffer;
Expand Down Expand Up @@ -345,10 +342,9 @@ export function format(css, { minify = false } = {}) {

/**
* @param {import('css-tree').Declaration} node
* @param {number} indent_level
* @returns {string} A formatted Declaration
*/
function print_declaration(node, indent_level) {
function print_declaration(node) {
let property = node.property;

// Lowercase the property, unless it's a custom property (starts with --)
Expand Down Expand Up @@ -483,10 +479,9 @@ export function format(css, { minify = false } = {}) {

/**
* @param {import('css-tree').CssNode} node
* @param {number} indent_level
* @returns {string} A formatted Stylesheet
*/
function print(node, indent_level) {
function print(node) {
let buffer = EMPTY_STRING;

/** @type {import('css-tree').List<import('css-tree').CssNode>} */
Expand All @@ -495,9 +490,9 @@ export function format(css, { minify = false } = {}) {

children.forEach((child, item) => {
if (child.type === TYPE_RULE) {
buffer += print_rule(child, indent_level);
buffer += print_rule(child);
} else if (child.type === TYPE_ATRULE) {
buffer += print_atrule(child, indent_level);
buffer += print_atrule(child);
} else {
buffer += print_unknown(child, indent_level);
}
Expand All @@ -510,7 +505,7 @@ export function format(css, { minify = false } = {}) {
return buffer;
}

return print(ast, 0);
return print(ast);
}

/**
Expand Down

0 comments on commit 85c0ac2

Please sign in to comment.