Skip to content

Commit

Permalink
nvm
Browse files Browse the repository at this point in the history
  • Loading branch information
bartveneman committed Oct 24, 2024
1 parent 87e4139 commit 56db99b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 15 deletions.
26 changes: 12 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,16 +308,16 @@ export function format(css, { minify = false } = {}) {

indent_level++

let comment = print_comment(start_offset(node), start_offset(/** @type {import('css-tree').CssNode} */(children.first)))
if (comment) {
buffer += indent(indent_level) + comment + NEWLINE
let opening_comment = print_comment(start_offset(node), start_offset(/** @type {import('css-tree').CssNode} */(children.first)))
if (opening_comment) {
buffer += indent(indent_level) + opening_comment + NEWLINE
}

children.forEach((child, item) => {
if (item.prev !== null) {
let before_comment = print_comment(end_offset(item.prev.data), start_offset(child))
if (before_comment) {
buffer += indent(indent_level) + before_comment + NEWLINE
let comment = print_comment(end_offset(item.prev.data), start_offset(child))
if (comment) {
buffer += indent(indent_level) + comment + NEWLINE
}
}

Expand Down Expand Up @@ -350,17 +350,15 @@ export function format(css, { minify = false } = {}) {
buffer += NEWLINE
}
}

let comment = print_comment(end_offset(/** @type {import('css-tree').CssNode} */(children.last)), end_offset(node))
if (comment) {
buffer += NEWLINE + indent(indent_level) + comment
}
})

indent_level--
let closing_comment = print_comment(end_offset(/** @type {import('css-tree').CssNode} */(children.last)), end_offset(node))
if (closing_comment) {
buffer += NEWLINE + indent(indent_level) + closing_comment
}

buffer += NEWLINE
buffer += indent(indent_level) + CLOSE_BRACE
indent_level--
buffer += NEWLINE + indent(indent_level) + CLOSE_BRACE

return buffer
}
Expand Down
57 changes: 56 additions & 1 deletion test/comments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,61 @@ selector {}
assert.is(actual, expected)
})

test('comment before rule and atrule should not be separated by newline', () => {
let actual = format(`
/* comment 1 */
selector {}
/* comment 2 */
@media (min-width: 1000px) {
/* comment 3 */
selector {}
/* comment 4 */
}
`)
let expected = `/* comment 1 */
selector {}
/* comment 2 */
@media (min-width: 1000px) {
/* comment 3 */
selector {}
/* comment 4 */
}`
assert.is(actual, expected)
})

test('a declaration after multiple comments starts on a new line', () => {
let actual = format(`
selector {
/* comment 1 */
/* comment 2 */
--custom-property: value;
/* comment 3 */
/* comment 4 */
--custom-property: value;
test('multiple comments in between rules and atrules', () => {
/* comment 5 */
/* comment 6 */
--custom-property: value;
}
`)
let expected = `selector {
/* comment 1 */
/* comment 2 */
--custom-property: value;
/* comment 3 */
/* comment 4 */
--custom-property: value;
/* comment 5 */
/* comment 6 */
--custom-property: value;
}`
assert.is(actual, expected)
})

test.skip('multiple comments in between rules and atrules', () => {
let actual = format(`
/* comment 1 */
/* comment 1.1 */
Expand All @@ -371,15 +423,18 @@ test('multiple comments in between rules and atrules', () => {
let expected = `/* comment 1 */
/* comment 1.1 */
selector {}
/* comment 2 */
/* comment 2.1 */
@media (min-width: 1000px) {
/* comment 3 */
/* comment 3.1 */
selector {}
/* comment 4 */
/* comment 4.1 */
}
/* comment 5 */
/* comment 5.1 */`
assert.is(actual, expected)
Expand Down

0 comments on commit 56db99b

Please sign in to comment.