Skip to content

Commit bf3150c

Browse files
authored
chore: improve some types and tests (#128)
1 parent 902ff08 commit bf3150c

File tree

4 files changed

+47
-38
lines changed

4 files changed

+47
-38
lines changed

index.js

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function lowercase(str) {
3838
*
3939
* Format a string of CSS using some simple rules
4040
* @param {string} css The original CSS
41-
* @param {Options} options
41+
* @param {Options} [options]
4242
* @returns {string} The formatted CSS
4343
*/
4444
export function format(css, {
@@ -50,10 +50,10 @@ export function format(css, {
5050
throw new TypeError('tab_size must be a number greater than 0')
5151
}
5252

53-
/** @type {number[]} */
53+
/** @type {number[]} [start0, end0, start1, end1, etc.]*/
5454
let comments = []
5555

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

81-
if (tab_size) {
81+
if (tab_size !== undefined) {
8282
return SPACE.repeat(tab_size * size)
8383
}
8484

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

@@ -113,7 +113,7 @@ export function format(css, {
113113
* @returns {string | undefined} The comment string, if found
114114
*/
115115
function print_comment(after, before) {
116-
if (minify || after === undefined || before === undefined) {
116+
if (minify === true || after === undefined || before === undefined) {
117117
return EMPTY_STRING
118118
}
119119

@@ -213,7 +213,7 @@ export function format(css, {
213213

214214
buffer += pseudo
215215

216-
if (child.children) {
216+
if (child.children !== null) {
217217
buffer += OPEN_PARENTHESES + print_simple_selector(child) + CLOSE_PARENTHESES
218218
}
219219
break
@@ -224,39 +224,37 @@ export function format(css, {
224224
buffer += print_simple_selector(selector_list_item)
225225
}
226226

227-
if (item.next && item.next.data.type === TYPE_SELECTOR) {
227+
if (item.next !== null && item.next.data.type === TYPE_SELECTOR) {
228228
buffer += COMMA + OPTIONAL_SPACE
229229
}
230230
})
231231
break
232232
}
233233
case 'Nth': {
234234
let nth = child.nth
235-
if (nth) {
236-
if (nth.type === 'AnPlusB') {
237-
let a = nth.a
238-
let b = nth.b
235+
if (nth.type === 'AnPlusB') {
236+
let a = nth.a
237+
let b = nth.b
239238

240-
if (a !== null) {
241-
buffer += a + 'n'
242-
}
243-
244-
if (a !== null && b !== null) {
245-
buffer += SPACE
246-
}
239+
if (a !== null) {
240+
buffer += a + 'n'
241+
}
247242

248-
if (b !== null) {
249-
// When (1n + x) but not (1n - x)
250-
if (a !== null && !b.startsWith('-')) {
251-
buffer += '+' + SPACE
252-
}
243+
if (a !== null && b !== null) {
244+
buffer += SPACE
245+
}
253246

254-
buffer += b
247+
if (b !== null) {
248+
// When (1n + x) but not (1n - x)
249+
if (a !== null && !b.startsWith('-')) {
250+
buffer += '+' + SPACE
255251
}
256-
} else {
257-
// For odd/even or maybe other identifiers later on
258-
buffer += substr(nth)
252+
253+
buffer += b
259254
}
255+
} else {
256+
// For odd/even or maybe other identifiers later on
257+
buffer += substr(nth)
260258
}
261259

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

273-
if (child.matcher && child.value) {
271+
if (child.matcher !== null && child.value !== null) {
274272
buffer += child.matcher
275273
buffer += QUOTE
276274

@@ -282,13 +280,17 @@ export function format(css, {
282280
buffer += QUOTE
283281
}
284282

285-
if (child.flags) {
283+
if (child.flags !== null) {
286284
buffer += SPACE + child.flags
287285
}
288286

289287
buffer += CLOSE_BRACKET
290288
break
291289
}
290+
case 'NestingSelector': {
291+
buffer += '&'
292+
break
293+
}
292294
default: {
293295
buffer += substr(child)
294296
break
@@ -440,7 +442,7 @@ export function format(css, {
440442
}
441443

442444
// Hacky: add a space in case of a `space toggle` during minification
443-
if (value === EMPTY_STRING && minify) {
445+
if (value === EMPTY_STRING && minify === true) {
444446
value += SPACE
445447
}
446448

@@ -545,12 +547,10 @@ export function format(css, {
545547
return indent(indent_level) + substr(node).trim()
546548
}
547549

548-
/** @type {import('css-tree').List<import('css-tree').CssNode>} */
549-
// @ts-expect-error Property 'children' does not exist on type 'AnPlusB', but we're never using that
550550
let children = ast.children
551551
let buffer = EMPTY_STRING
552552

553-
if (children.first) {
553+
if (children.first !== null) {
554554
let opening_comment = print_comment(0, start_offset(children.first))
555555
if (opening_comment) {
556556
buffer += opening_comment + NEWLINE

test/test.js renamed to test/api.test.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { suite } from 'uvu'
1+
import { test } from 'uvu'
22
import * as assert from 'uvu/assert'
33
import { format } from '../index.js'
44

5-
let test = suite('Stylesheet')
6-
75
test('empty input', () => {
86
let actual = format(``)
97
let expected = ``

test/selectors.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,17 @@ test('formats multiline selectors', () => {
149149
assert.is(actual, expected)
150150
})
151151

152+
test('format nesting selectors', () => {
153+
let actual = format(`
154+
& a {}
155+
b & c {}
156+
`)
157+
let expected = `& a {}
158+
159+
b & c {}`
160+
assert.is(actual, expected)
161+
})
162+
152163
test('forces attribute selectors to have quoted values', () => {
153164
let actual = format(`
154165
[title=foo],
File renamed without changes.

0 commit comments

Comments
 (0)