Skip to content

Commit

Permalink
some DRY-er newline management (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartveneman authored Dec 13, 2023
1 parent 33a956d commit aa83274
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import parse from 'css-tree/parser'

const NEWLINE = '\n'

/**
* Indent a string
* @param {number} size
Expand Down Expand Up @@ -81,7 +83,7 @@ function print_selectorlist(node, indent_level, css) {
}

if (selector !== node.children.last) {
buffer += `,\n`
buffer += `,` + NEWLINE
}
}
return buffer
Expand Down Expand Up @@ -191,7 +193,7 @@ function print_block(node, indent_level, css) {
return ' {}'
}

let buffer = ' {\n'
let buffer = ' {' + NEWLINE

indent_level++

Expand All @@ -200,25 +202,25 @@ function print_block(node, indent_level, css) {
for (let child of children) {
if (child.type === 'Declaration') {
buffer += print_declaration(child, indent_level, css) + ';'
} else if (child.type === 'Rule') {
} else {
if (prev_type === 'Declaration') {
buffer += '\n'
buffer += NEWLINE
}
buffer += print_rule(child, indent_level, css)
} else if (child.type === 'Atrule') {
if (prev_type === 'Declaration') {
buffer += '\n'

if (child.type === 'Rule') {
buffer += print_rule(child, indent_level, css)
} else if (child.type === 'Atrule') {
buffer += print_atrule(child, indent_level, css)
} else {
buffer += print_unknown(child, indent_level, css)
}
buffer += print_atrule(child, indent_level, css)
} else {
buffer += print_unknown(child, indent_level, css)
}

if (child !== children.last) {
buffer += '\n'
buffer += NEWLINE

if (child.type !== 'Declaration') {
buffer += '\n'
buffer += NEWLINE
}
}

Expand All @@ -227,7 +229,7 @@ function print_block(node, indent_level, css) {

indent_level--

buffer += '\n'
buffer += NEWLINE
buffer += indent(indent_level) + '}'

return buffer
Expand Down Expand Up @@ -400,7 +402,7 @@ function print(node, indent_level = 0, css) {
}

if (child !== children.last) {
buffer += '\n\n'
buffer += NEWLINE + NEWLINE
}
}

Expand Down

0 comments on commit aa83274

Please sign in to comment.