Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 35 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function lowercase(str) {
*
* Format a string of CSS using some simple rules
* @param {string} css The original CSS
* @param {Options} options
* @param {Options} [options]
* @returns {string} The formatted CSS
*/
export function format(css, {
Expand All @@ -50,10 +50,10 @@ export function format(css, {
throw new TypeError('tab_size must be a number greater than 0')
}

/** @type {number[]} */
/** @type {number[]} [start0, end0, start1, end1, etc.]*/
let comments = []

/** @type {import('css-tree').CssNode} */
/** @type {import('css-tree').StyleSheet} */
let ast = parse(css, {
positions: true,
parseAtrulePrelude: false,
Expand All @@ -76,9 +76,9 @@ export function format(css, {
* @returns {string} A string with [size] tabs/spaces
*/
function indent(size) {
if (minify) return EMPTY_STRING
if (minify === true) return EMPTY_STRING

if (tab_size) {
if (tab_size !== undefined) {
return SPACE.repeat(tab_size * size)
}

Expand All @@ -90,7 +90,7 @@ export function format(css, {
let loc = node.loc
// If the node has no location, return an empty string
// This is necessary for space toggles
if (!loc) return EMPTY_STRING
if (loc === undefined || loc === null) return EMPTY_STRING
return css.slice(loc.start.offset, loc.end.offset)
}

Expand All @@ -113,7 +113,7 @@ export function format(css, {
* @returns {string | undefined} The comment string, if found
*/
function print_comment(after, before) {
if (minify || after === undefined || before === undefined) {
if (minify === true || after === undefined || before === undefined) {
return EMPTY_STRING
}

Expand Down Expand Up @@ -213,7 +213,7 @@ export function format(css, {

buffer += pseudo

if (child.children) {
if (child.children !== null) {
buffer += OPEN_PARENTHESES + print_simple_selector(child) + CLOSE_PARENTHESES
}
break
Expand All @@ -224,39 +224,37 @@ export function format(css, {
buffer += print_simple_selector(selector_list_item)
}

if (item.next && item.next.data.type === TYPE_SELECTOR) {
if (item.next !== null && item.next.data.type === TYPE_SELECTOR) {
buffer += COMMA + OPTIONAL_SPACE
}
})
break
}
case 'Nth': {
let nth = child.nth
if (nth) {
if (nth.type === 'AnPlusB') {
let a = nth.a
let b = nth.b
if (nth.type === 'AnPlusB') {
let a = nth.a
let b = nth.b

if (a !== null) {
buffer += a + 'n'
}

if (a !== null && b !== null) {
buffer += SPACE
}
if (a !== null) {
buffer += a + 'n'
}

if (b !== null) {
// When (1n + x) but not (1n - x)
if (a !== null && !b.startsWith('-')) {
buffer += '+' + SPACE
}
if (a !== null && b !== null) {
buffer += SPACE
}

buffer += b
if (b !== null) {
// When (1n + x) but not (1n - x)
if (a !== null && !b.startsWith('-')) {
buffer += '+' + SPACE
}
} else {
// For odd/even or maybe other identifiers later on
buffer += substr(nth)

buffer += b
}
} else {
// For odd/even or maybe other identifiers later on
buffer += substr(nth)
}

if (child.selector !== null) {
Expand All @@ -270,7 +268,7 @@ export function format(css, {
buffer += OPEN_BRACKET
buffer += child.name.name

if (child.matcher && child.value) {
if (child.matcher !== null && child.value !== null) {
buffer += child.matcher
buffer += QUOTE

Expand All @@ -282,13 +280,17 @@ export function format(css, {
buffer += QUOTE
}

if (child.flags) {
if (child.flags !== null) {
buffer += SPACE + child.flags
}

buffer += CLOSE_BRACKET
break
}
case 'NestingSelector': {
buffer += '&'
break
}
default: {
buffer += substr(child)
break
Expand Down Expand Up @@ -440,7 +442,7 @@ export function format(css, {
}

// Hacky: add a space in case of a `space toggle` during minification
if (value === EMPTY_STRING && minify) {
if (value === EMPTY_STRING && minify === true) {
value += SPACE
}

Expand Down Expand Up @@ -545,12 +547,10 @@ export function format(css, {
return indent(indent_level) + substr(node).trim()
}

/** @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 (children.first) {
if (children.first !== null) {
let opening_comment = print_comment(0, start_offset(children.first))
if (opening_comment) {
buffer += opening_comment + NEWLINE
Expand Down
4 changes: 1 addition & 3 deletions test/test.js → test/api.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { suite } from 'uvu'
import { test } from 'uvu'
import * as assert from 'uvu/assert'
import { format } from '../index.js'

let test = suite('Stylesheet')

test('empty input', () => {
let actual = format(``)
let expected = ``
Expand Down
11 changes: 11 additions & 0 deletions test/selectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ test('formats multiline selectors', () => {
assert.is(actual, expected)
})

test('format nesting selectors', () => {
let actual = format(`
& a {}
b & c {}
`)
let expected = `& a {}

b & c {}`
assert.is(actual, expected)
})

test('forces attribute selectors to have quoted values', () => {
let actual = format(`
[title=foo],
Expand Down
File renamed without changes.