1- // @ts -expect-error Typing of css-tree is incomplete
2- import parse from 'css-tree/parser'
1+ import { parse } from '@eslint/css-tree'
32
43/**
5- * @typedef {import('css-tree').CssNode } CssNode
6- * @typedef {import('css-tree').List<CssNode> } List
7- * @typedef {import('css-tree').CssLocation } CssLocation
8- * @typedef {import('css-tree').Raw } Raw
9- * @typedef {import('css-tree').StyleSheet } StyleSheet
10- * @typedef {import('css-tree').Atrule } Atrule
11- * @typedef {import('css-tree').AtrulePrelude } AtrulePrelude
12- * @typedef {import('css-tree').Rule } Rule
13- * @typedef {import('css-tree').SelectorList } SelectorList
14- * @typedef {import('css-tree').Selector } Selector
15- * @typedef {import('css-tree').PseudoClassSelector } PseudoClassSelector
16- * @typedef {import('css-tree').PseudoElementSelector } PseudoElementSelector
17- * @typedef {import('css-tree').Block } Block
18- * @typedef {import('css-tree').Declaration } Declaration
19- * @typedef {import('css-tree').Value } Value
20- * @typedef {import('css-tree').Operator } Operator
4+ * @typedef {import('@eslint/ css-tree').CssNode } CssNode
5+ * @typedef {import('@eslint/ css-tree').List<CssNode> } List
6+ * @typedef {import('@eslint/ css-tree').CssLocation } CssLocation
7+ * @typedef {import('@eslint/ css-tree').Raw } Raw
8+ * @typedef {import('@eslint/ css-tree').StyleSheet } StyleSheet
9+ * @typedef {import('@eslint/ css-tree').Atrule } Atrule
10+ * @typedef {import('@eslint/ css-tree').AtrulePrelude } AtrulePrelude
11+ * @typedef {import('@eslint/ css-tree').Rule } Rule
12+ * @typedef {import('@eslint/ css-tree').SelectorList } SelectorList
13+ * @typedef {import('@eslint/ css-tree').Selector } Selector
14+ * @typedef {import('@eslint/ css-tree').PseudoClassSelector } PseudoClassSelector
15+ * @typedef {import('@eslint/ css-tree').PseudoElementSelector } PseudoElementSelector
16+ * @typedef {import('@eslint/ css-tree').Block } Block
17+ * @typedef {import('@eslint/ css-tree').Declaration } Declaration
18+ * @typedef {import('@eslint/ css-tree').Value } Value
19+ * @typedef {import('@eslint/ css-tree').Operator } Operator
2120 */
2221
2322const SPACE = ' '
@@ -61,11 +60,7 @@ function lowercase(str) {
6160 * @param {Options } [options]
6261 * @returns {string } The formatted CSS
6362 */
64- export function format ( css , {
65- minify = false ,
66- tab_size = undefined ,
67- } = Object . create ( null ) ) {
68-
63+ export function format ( css , { minify = false , tab_size = undefined } = Object . create ( null ) ) {
6964 if ( tab_size !== undefined && Number ( tab_size ) < 1 ) {
7065 throw new TypeError ( 'tab_size must be a number greater than 0' )
7166 }
@@ -75,10 +70,10 @@ export function format(css, {
7570
7671 /**
7772 * @param {string } _ The comment text
78- * @param {CssLocation } position
73+ * @param {CssLocation } loc
7974 */
80- function on_comment ( _ , position ) {
81- comments . push ( position . start . offset , position . end . offset )
75+ function on_comment ( _ , loc ) {
76+ comments . push ( loc . start . offset , loc . end . offset )
8277 }
8378
8479 /** @type {StyleSheet } */
@@ -122,13 +117,13 @@ export function format(css, {
122117
123118 /** @param {CssNode } node */
124119 function start_offset ( node ) {
125- let loc = /** @type {CssLocation } */ ( node . loc )
120+ let loc = /** @type {CssLocation } */ ( node . loc )
126121 return loc . start . offset
127122 }
128123
129124 /** @param {CssNode } node */
130125 function end_offset ( node ) {
131- let loc = /** @type {CssLocation } */ ( node . loc )
126+ let loc = /** @type {CssLocation } */ ( node . loc )
132127 return loc . end . offset
133128 }
134129
@@ -218,7 +213,10 @@ export function format(css, {
218213 }
219214 case 'Combinator' : {
220215 // putting spaces around `child.name` (+ > ~ or ' '), unless the combinator is ' '
221- buffer += SPACE
216+ // and the combinator is not the first in a nested selectorlist
217+ if ( child !== children . first ) {
218+ buffer += SPACE
219+ }
222220
223221 if ( child . name !== ' ' ) {
224222 buffer += child . name + SPACE
@@ -348,7 +346,7 @@ export function format(css, {
348346
349347 indent_level ++
350348
351- let opening_comment = print_comment ( start_offset ( node ) , start_offset ( /** @type {CssNode } */ ( children . first ) ) )
349+ let opening_comment = print_comment ( start_offset ( node ) , start_offset ( /** @type {CssNode } */ ( children . first ) ) )
352350 if ( opening_comment ) {
353351 buffer += indent ( indent_level ) + opening_comment + NEWLINE
354352 }
@@ -392,7 +390,7 @@ export function format(css, {
392390 }
393391 } )
394392
395- let closing_comment = print_comment ( end_offset ( /** @type {CssNode } */ ( children . last ) ) , end_offset ( node ) )
393+ let closing_comment = print_comment ( end_offset ( /** @type {CssNode } */ ( children . last ) ) , end_offset ( node ) )
396394 if ( closing_comment ) {
397395 buffer += NEWLINE + indent ( indent_level ) + closing_comment
398396 }
@@ -447,7 +445,7 @@ export function format(css, {
447445 let space = operator === '+' || operator === '-' ? SPACE : OPTIONAL_SPACE
448446 return `calc(${ left . trim ( ) } ${ space } ${ operator } ${ space } ${ right . trim ( ) } )`
449447 } )
450- . replace ( / s e l e c t o r | u r l | s u p p o r t s | l a y e r \( / ig , ( match ) => lowercase ( match ) ) // lowercase function names
448+ . replace ( / s e l e c t o r | u r l | s u p p o r t s | l a y e r \( / gi , ( match ) => lowercase ( match ) ) // lowercase function names
451449 }
452450
453451 /** @param {Declaration } node */
@@ -603,7 +601,7 @@ export function format(css, {
603601 }
604602 } )
605603
606- let closing_comment = print_comment ( end_offset ( /** @type {CssNode } */ ( children . last ) ) , end_offset ( ast ) )
604+ let closing_comment = print_comment ( end_offset ( /** @type {CssNode } */ ( children . last ) ) , end_offset ( ast ) )
607605 if ( closing_comment ) {
608606 buffer += NEWLINE + closing_comment
609607 }
0 commit comments