Skip to content

Commit 0938ca0

Browse files
committed
docs(v7): correct parser details, ESM usage, walker registration, and outdated options across README and docs
1 parent 1d367aa commit 0938ca0

File tree

14 files changed

+107
-247
lines changed

14 files changed

+107
-247
lines changed

README.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
[tests]: https://img.shields.io/circleci/project/github/shellscape/postcss-values-parser.svg
2-
[tests-url]: https://circleci.com/gh/shellscape/postcss-values-parser
3-
[cover]: https://codecov.io/gh/shellscape/postcss-values-parser/branch/master/graph/badge.svg
4-
[cover-url]: https://codecov.io/gh/shellscape/postcss-values-parser
1+
[actions]: https://github.com/shellscape/postcss-values-parser/actions/workflows/validate.yml/badge.svg?branch=master
2+
[actions-url]: https://github.com/shellscape/postcss-values-parser/actions/workflows/validate.yml
53
[size]: https://packagephobia.now.sh/badge?p=postcss-values-parser
64
[size-url]: https://packagephobia.now.sh/result?p=postcss-values-parser
75

86
<div align="center">
97
<img width="95" height="95" title="Philosopher’s stone, logo of PostCSS" src="http://postcss.github.io/postcss/logo.svg"><br/><br/>
108
</div>
119

12-
# postcss-values-parser [![tests][tests]][tests-url] [![cover][cover]][cover-url] [![size][size]][size-url]
10+
# postcss-values-parser [![actions][actions]][actions-url] [![size][size]][size-url]
1311

14-
A CSS property value parser built upon [PostCSS](https://github.com/postcss/postcss),
15-
following the same node and traversal patterns as PostCSS.
12+
A CSS property value parser that uses [css-tree](https://github.com/csstree/csstree) for parsing,
13+
and models nodes on top of PostCSS’s `Node`/`Container`/`Root` classes so the API feels familiar to PostCSS users.
1614

1715
This package powers part of [Prettier](https://prettier.io/). Please consider becoming a sponsor if you find this package useful or are a Prettier user. https://github.com/sponsors/shellscape
1816

@@ -26,16 +24,23 @@ npm install postcss-values-parser --save-dev
2624

2725
## Requirements
2826

29-
`postcss-values-parser` Node version v6.14.0+ and PostCSS v7.0.0+.
27+
- Node.js >= 20.19.0
28+
- PostCSS >= 8.4.14 (peer dependency)
29+
30+
Note: This package is ESM‑only. Use `import` in Node.js or load from CommonJS via dynamic import:
31+
32+
```js
33+
const mod = await import('postcss-values-parser');
34+
```
3035

3136
## Benefits
3237

33-
- Leverages PostCSS and its tokenizer under the hood
34-
- Doesn't strip characters; eg. parenthesis
38+
- Uses css-tree for fast, standards‑compliant parsing
39+
- Builds PostCSS‑style nodes for a familiar API
40+
- Doesn't strip characters; e.g., parentheses are preserved in the AST
3541
- Full [AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree) traversal
36-
- Ability to walk the AST for every Node type
37-
- Convenience methods to stringify Nodes
38-
- Follows PostCSS patterns for whitespace between Nodes
42+
- Optional walker helpers (`registerWalkers(Container)`) to walk by node type
43+
- Convenience methods to stringify nodes
3944
- Provides convenience properties for number units, colors, etc.
4045

4146
## Usage

docs/Comment.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Comment Node
22

3-
The `Comment` node inherits directly from `Node` in PostCSS. This node represents a CSS comment; either inline (`//`) or block (`/* */`).
3+
The `Comment` node inherits directly from `Node` in PostCSS. This node represents a CSS block comment (`/* … */`).
44

55
## Properties
66

77
### `inline`
88

99
Type: `Boolean`<br>
1010

11-
If `true`, indicates that the type of comment is "inline," or a comment that begins with `//`. If `false`, indicates that the comment is a traditional block comment.
11+
Always `false` for CSS values. Inline `//` comments are not part of standard CSS values and are not produced by the parser.
1212

1313
### `text`
1414

@@ -25,11 +25,10 @@ Value: `'comment'`
2525

2626
Type: `String`<br>
2727

28-
A `String` representation of the original comment including comment markers.
28+
The original comment including comment markers, e.g. `/* comment */`.
2929

3030
## Example Values
3131

3232
```css
33-
// na na na na na na na na batmannnnn
3433
/* joker cheats at poker */
3534
```

docs/Container.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ This class inherits all properties and methods from PostCSS's `Container` class.
5757
## Example Usage
5858

5959
```js
60-
const { parse } = require('postcss-values-parser');
60+
import { parse, Word } from 'postcss-values-parser';
6161

6262
const root = parse('calc(100px + 20%)');
6363
const func = root.nodes[0]; // This is a Func node, which extends Container
@@ -85,7 +85,9 @@ Container nodes have access to all walker methods for traversing their child nod
8585
- `walkType(type, callback)` - Walk through all nodes of a specific type
8686

8787
```js
88-
const { parse } = require('postcss-values-parser');
88+
import { parse, registerWalkers, Container } from 'postcss-values-parser';
89+
90+
registerWalkers(Container);
8991

9092
const root = parse('calc(100px + 20%) url("image.jpg")');
9193
const func = root.nodes[0]; // calc function
@@ -108,5 +110,5 @@ See the [Walker](./Walker.md) documentation for more details on walker methods.
108110
- Container nodes automatically handle source mapping and position tracking when nodes are added
109111
- Child nodes maintain references to their parent container
110112
- The Container class provides the foundation for complex nodes like `Func`, `Root`, and `Parentheses`
111-
- Walker methods are registered automatically when the module is loaded
113+
- Walker helpers must be registered once via `registerWalkers(Container)` before use
112114
- Walker methods traverse all descendants, not just direct children

docs/Examples.md

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -333,22 +333,7 @@ root.walkWords((word) => {
333333
});
334334
```
335335

336-
### SCSS/LESS Variables
337-
338-
```js
339-
const { parse } = require('postcss-values-parser');
340-
341-
// Parse SCSS variables
342-
const root = parse('$primary-color', {
343-
variables: { prefixes: ['--', '$'] }
344-
});
345-
346-
root.walkWords((word) => {
347-
if (word.isVariable) {
348-
console.log(`SCSS Variable: ${word.value}`);
349-
}
350-
});
351-
```
336+
> Note: In v7, `Word.isVariable` only detects CSS custom properties (values starting with `--`). SCSS/LESS variable prefixes are not detected.
352337
353338
## Error Handling
354339

@@ -627,4 +612,4 @@ describe('Value Parser', () => {
627612
- Consider caching parsed results for frequently used values
628613
- Custom stringifiers allow for powerful value transformations
629614
- The parser preserves source mapping information for debugging
630-
- All examples can be adapted for use in various build tools and frameworks
615+
- All examples can be adapted for use in various build tools and frameworks

docs/Exports.md

Lines changed: 16 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Exported Methods
1+
# Exported API
22

33
This module exports the following methods and classes:
44

5-
### `parse(css, options)`
5+
### `parse(css, options?)`
66

77
Returns: `Root`<br>
88

9-
Parses a given `String` and returns an AST with a `Root` node. If the input is an invalid CSS value, a `ParseError` is thrown.
9+
Parses a given string and returns an AST with a `Root` node. If the input is an invalid CSS value, a `ParseError` is thrown.
1010

1111
#### Parameters
1212

@@ -17,36 +17,13 @@ _Required_
1717

1818
#### `options`
1919

20-
Type: `ParseOptions`<br>
21-
_Optional_
20+
Type: `ParseOptions` (optional)
2221

23-
##### Properties
24-
25-
##### `ignoreUnknownWords`
26-
27-
Type: `Boolean`<br>
28-
Default: `false`
29-
30-
If `true`, will allow all unknown parts of the value to be parsed and added to the AST. If `false`, unknown values will throw `ParseError`.
31-
32-
##### `interpolation`
33-
34-
Type: `Boolean|InterpolationOptions`<br>
35-
Default: `false`
36-
37-
Set this option to enable parsing of interpolated values for languages such as SCSS. For example:
38-
`interpolation: { prefix: '@' }` will allow parsing of the interpolated value `@{batman}` which uses `@` as the "prefix". For SCSS one might use `interpolation: { prefix: '#' }`.
39-
40-
##### `variables`
41-
42-
Type: `VariablesOptions`<br>
43-
Default: `{ prefixes: ['--'] }`
44-
45-
Set this option to modify how variables are identified in a value. By default, this option is set to recognize CSS variables. For languages such as LESS and SCSS which have their own variable prefixes, additional prefixes can be added to the `prefixes` array.
22+
Reserved for future use. In v7, options are accepted by the signature but are not used by the parser.
4623

4724
### `stringify(node, builder)`
4825

49-
A `Function` with a signature matching `(bit) => {}` used to concatenate or manipulate each portion (or bit) of the Node's own AST. The `nodeToString` method makes use of this, as a simple example.
26+
A function used to concatenate or manipulate each portion (or bit) of a node during stringification. The `nodeToString` helper uses this under the hood.
5027

5128
#### Parameters
5229

@@ -79,7 +56,7 @@ Returns: `String`
7956

8057
### `registerWalkers(Container)`
8158

82-
Registers custom walker methods on the Container prototype to enable walking specific node types. This function is called automatically when the module is loaded, but can be called manually if needed.
59+
Registers custom walker methods on the Container prototype to enable walking specific node types. This function is not called automatically; call it once before using any `walk*` helpers.
8360

8461
#### Parameters
8562

@@ -92,7 +69,7 @@ The Container class to register walker methods on.
9269

9370
## Exported Classes
9471

95-
All Node classes are exported and can be imported individually:
72+
All node classes are exported and can be imported individually:
9673

9774
### Node Classes
9875

@@ -116,40 +93,16 @@ All Node classes are exported and can be imported individually:
11693

11794
### Type Definitions
11895

119-
- `ParseOptions` - Options interface for the parse function
120-
- `InterpolationOptions` - Options for interpolation parsing
121-
- `VariablesOptions` - Options for variable recognition
96+
- `ParseOptions` - Placeholder in v7 (forward‑compatibility)
12297
- `Stringifier` - Function interface for custom stringifiers
12398
- `Builder` - Function interface for string building during stringify
12499
- `NodeOptions` - Options interface for node construction
125100

126-
## Type Interfaces
101+
## Types
127102

128103
### `ParseOptions`
129104

130-
```typescript
131-
interface ParseOptions {
132-
ignoreUnknownWords?: boolean;
133-
interpolation?: boolean | InterpolationOptions;
134-
variables?: VariablesOptions;
135-
}
136-
```
137-
138-
### `InterpolationOptions`
139-
140-
```typescript
141-
interface InterpolationOptions {
142-
prefix: string;
143-
}
144-
```
145-
146-
### `VariablesOptions`
147-
148-
```typescript
149-
interface VariablesOptions {
150-
prefixes: string[];
151-
}
152-
```
105+
An empty placeholder interface in v7. Kept for forward‑compatibility.
153106

154107
### `Stringifier`
155108

@@ -181,19 +134,16 @@ interface NodeOptions {
181134

182135
```js
183136
// Import specific classes
184-
const { parse, Node, Container, Root } = require('postcss-values-parser');
137+
import { parse, Node, Container, Root } from 'postcss-values-parser';
185138

186139
// Import error classes
187-
const { ParseError, AstError } = require('postcss-values-parser');
140+
import { ParseError, AstError } from 'postcss-values-parser';
188141

189142
// Import utility functions
190-
const { stringify, nodeToString, registerWalkers } = require('postcss-values-parser');
143+
import { stringify, nodeToString, registerWalkers } from 'postcss-values-parser';
191144

192-
// Parse with options
193-
const root = parse('calc(100px + 20%)', {
194-
ignoreUnknownWords: true,
195-
variables: { prefixes: ['--', '$'] }
196-
});
145+
// Parse
146+
const root = parse('calc(100px + 20%)');
197147

198148
// Custom stringifier
199149
const customStringifier = (node, builder) => {

docs/Node.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ This class inherits all properties and methods from PostCSS's `Node` class. Plea
5656
## Example Usage
5757

5858
```js
59-
const { parse, Word } = require('postcss-values-parser');
59+
import { parse, Word } from 'postcss-values-parser';
6060

6161
const root = parse('bold italic');
6262
const firstNode = root.nodes[0];
@@ -78,4 +78,4 @@ console.log(newNode.value); // 'underline'
7878
- All nodes automatically maintain parent-child relationships when added to containers
7979
- Source mapping information is preserved throughout parsing and manipulation
8080
- The Node class provides the foundation for all AST traversal and manipulation operations
81-
- The `value` property is readonly and should be set during construction via the options parameter
81+
- The `value` property is readonly and should be set during construction via the options parameter

docs/Parentheses.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ This class inherits all properties and methods from the `Container` class and Po
6464
## Example Usage
6565

6666
```js
67-
const { parse } = require('postcss-values-parser');
67+
import { parse } from 'postcss-values-parser';
6868

6969
const root = parse('calc((100px + 20px) * 2)');
7070
const func = root.nodes[0]; // calc function
@@ -85,4 +85,4 @@ parentheses.nodes.forEach(node => {
8585
- Parentheses nodes are automatically created when the parser encounters parentheses groupings in CSS values
8686
- The content within parentheses is parsed as separate child nodes
8787
- Parentheses nodes maintain the structural integrity of grouped expressions
88-
- They are commonly found within function arguments and mathematical expressions
88+
- They are commonly found within function arguments and mathematical expressions

0 commit comments

Comments
 (0)