Commit cde5e32
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
3 files changed
+477
-248
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
55 | 87 | | |
56 | 88 | | |
57 | 89 | | |
| |||
0 commit comments