Skip to content

Commit cde5e32

Browse files
bartvenemanclaude
andauthored
Refactor: Extract formatting functions and improve type safety (#197)
This is a re-creation of #191 ## Summary This PR refactors the CSS formatting library to extract lower-level formatting functions as public exports and improves type safety by using type guards instead of node type constants. ## Key Changes - **Extracted public formatting functions**: `format_value()`, `format_declaration()`, `format_selector()`, `format_atrule_prelude()`, and `unquote()` are now exported as standalone functions that can be used independently of the main `format()` function - **Replaced type checking with type guards**: Changed from checking `node.type === NODE.CONSTANT` to using type guard functions like `is_function()`, `is_declaration()`, `is_rule()`, etc., improving type safety and reducing reliance on magic constants - **Updated imports from css-parser**: Now imports type guards and type definitions directly from `@projectwallace/css-parser` instead of node type constants - **Improved function signatures**: Formatting functions now accept `FormatOptions` parameter for consistent minification control across all formatters - **Updated dependencies**: Bumped `@projectwallace/css-parser` from `^0.13.5` to `~0.14.7` to support new type guards and types - **Enhanced test coverage**: Added comprehensive tests for all newly exported functions including `format_atrule_prelude()`, `format_selector()`, `format_declaration()`, `format_value()`, and `unquote()` - **Updated documentation**: Added README section documenting the new partial formatters API ## Implementation Details - The refactored functions maintain backward compatibility with the existing `format()` and `minify()` APIs - Type guards provide better IDE support and compile-time safety compared to runtime type checking - All formatting logic remains unchanged; this is purely a refactoring for better API design and maintainability - Version bumped to 3.0.2 as a minor feature release https://claude.ai/code/session_01M2d2VuMLMKQSFTL7GniQny Co-authored-by: Claude <noreply@anthropic.com>
1 parent e42ff29 commit cde5e32

File tree

3 files changed

+477
-248
lines changed

3 files changed

+477
-248
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,38 @@ Need more examples?
5252
- [StackBlitz example using CommonJS](https://stackblitz.com/edit/stackblitz-starters-phchci?file=index.js)
5353
- [StackBlitz example using ES Modules](https://stackblitz.com/edit/stackblitz-starters-hrhsed?file=index.js)
5454

55+
### Partial formatters
56+
57+
The package also exports lower-level formatters for individual CSS constructs:
58+
59+
```js
60+
import {
61+
format_value,
62+
format_declaration,
63+
format_selector,
64+
format_atrule_prelude,
65+
} from '@projectwallace/format-css'
66+
67+
// Format a CSS value (e.g. the right-hand side of a declaration)
68+
format_value(node.value)
69+
70+
// Format a single CSS declaration (property + value)
71+
format_declaration(node)
72+
73+
// Format a single CSS selector
74+
format_selector(node)
75+
76+
// Format an at-rule prelude string (e.g. the query part of @media)
77+
format_atrule_prelude(node.prelude.text)
78+
```
79+
80+
All of these accept the same options as `format()`. However, `tab_size` has no effect on them since they do not produce indented output.
81+
82+
```js
83+
format_declaration(node, { minify: true })
84+
format_selector(node, { minify: true })
85+
```
86+
5587
## Formatting rules
5688

5789
1. Every **AtRule** starts on a new line

0 commit comments

Comments
 (0)