@@ -7,40 +7,129 @@ export declare function consumeComponentValue(ctx: Context, tokens: Array<CSSTok
7
7
advance : number ;
8
8
node : ComponentValue ;
9
9
} ;
10
- export declare class FunctionNode {
10
+ declare abstract class ContainerNodeBaseClass {
11
+ /**
12
+ * The contents of the `Function` or `Simple Block`.
13
+ * This is a list of component values.
14
+ */
15
+ value : Array < ComponentValue > ;
16
+ /**
17
+ * Retrieve the index of the given item in the current node.
18
+ * For most node types this will be trivially implemented as `this.value.indexOf(item)`.
19
+ */
20
+ indexOf ( item : ComponentValue ) : number | string ;
21
+ /**
22
+ * Retrieve the item at the given index in the current node.
23
+ * For most node types this will be trivially implemented as `this.value[index]`.
24
+ */
25
+ at ( index : number | string ) : ComponentValue | undefined ;
26
+ /**
27
+ * Iterates over each item in the `value` array of the current node.
28
+ *
29
+ * @param cb - The callback function to execute for each item.
30
+ * The function receives an object containing the current node (`node`), its parent (`parent`),
31
+ * and an optional `state` object.
32
+ * A second parameter is the index of the current node.
33
+ * The function can return `false` to stop the iteration.
34
+ *
35
+ * @param state - An optional state object that can be used to pass additional information to the callback function.
36
+ * The state object is cloned for each iteration. This means that changes to the state object are not reflected in the next iteration.
37
+ *
38
+ * @returns `false` if the iteration was halted, `undefined` otherwise.
39
+ *
40
+ * @template T - The type of the `state` object.
41
+ * @template U - The type of the current node.
42
+ */
43
+ forEach < T extends Record < string , unknown > , U extends ContainerNode > ( this : U , cb : ( entry : {
44
+ node : ComponentValue ;
45
+ parent : ContainerNode ;
46
+ state ?: T ;
47
+ } , index : number | string ) => boolean | void , state ?: T ) : false | undefined ;
48
+ /**
49
+ * Walks the current node and all its children.
50
+ *
51
+ * @param cb - The callback function to execute for each item.
52
+ * The function receives an object containing the current node (`node`), its parent (`parent`),
53
+ * and an optional `state` object.
54
+ * A second parameter is the index of the current node.
55
+ * The function can return `false` to stop the iteration.
56
+ *
57
+ * @param state - An optional state object that can be used to pass additional information to the callback function.
58
+ * The state object is cloned for each iteration. This means that changes to the state object are not reflected in the next iteration.
59
+ * However changes are passed down to child node iterations.
60
+ *
61
+ * @returns `false` if the iteration was halted, `undefined` otherwise.
62
+ *
63
+ * @template T - The type of the `state` object.
64
+ * @template U - The type of the current node.
65
+ */
66
+ walk < T extends Record < string , unknown > , U extends ContainerNode > ( this : U , cb : ( entry : {
67
+ node : ComponentValue ;
68
+ parent : ContainerNode ;
69
+ state ?: T ;
70
+ } , index : number | string ) => boolean | void , state ?: T ) : false | undefined ;
71
+ }
72
+ export declare class FunctionNode extends ContainerNodeBaseClass {
73
+ /**
74
+ * The node type
75
+ * Always `ComponentValueType.Function`
76
+ */
11
77
type : ComponentValueType ;
78
+ /**
79
+ * The token for the name of the function.
80
+ */
12
81
name : TokenFunction ;
82
+ /**
83
+ * The token for the closing parenthesis of the function.
84
+ * If the function is unclosed, this will be an EOF token.
85
+ */
13
86
endToken : CSSToken ;
14
- value : Array < ComponentValue > ;
15
87
constructor ( name : TokenFunction , endToken : CSSToken , value : Array < ComponentValue > ) ;
88
+ /**
89
+ * Retrieve the name of the current Function.
90
+ * This is the parsed and unescaped name of the function.
91
+ */
16
92
getName ( ) : string ;
17
93
/**
18
94
* Normalize the current Function:
19
95
* - if the "endToken" is EOF, replace with a ")-token"
20
96
*/
21
97
normalize ( ) : void ;
98
+ /**
99
+ * Retrieve the tokens for the current Function.
100
+ * This is the inverse of parsing from a list of tokens.
101
+ */
22
102
tokens ( ) : Array < CSSToken > ;
103
+ /**
104
+ * Convert the current Function to a string.
105
+ * This is not a true serialization.
106
+ * It is purely a concatenation of the string representation of the tokens.
107
+ */
23
108
toString ( ) : string ;
24
- indexOf ( item : ComponentValue ) : number | string ;
25
- at ( index : number | string ) : ComponentValue | undefined ;
26
- walk < T extends Record < string , unknown > > ( cb : ( entry : {
27
- node : ComponentValue ;
28
- parent : ContainerNode ;
29
- state ?: T ;
30
- } , index : number | string ) => boolean | void , state ?: T ) : false | undefined ;
109
+ /**
110
+ * A debug helper to convert the current object to a JSON representation.
111
+ * This is useful in asserts and to store large ASTs in files.
112
+ */
31
113
toJSON ( ) : unknown ;
114
+ /**
115
+ * Check if the current object is a FunctionNode.
116
+ * This is a type guard to help with type narrowing.
117
+ */
32
118
isFunctionNode ( ) : this is FunctionNode ;
119
+ /**
120
+ * Check if the given object is a FunctionNode.
121
+ * This is a type guard to help with type narrowing.
122
+ */
33
123
static isFunctionNode ( x : unknown ) : x is FunctionNode ;
34
124
}
35
125
export declare function consumeFunction ( ctx : Context , tokens : Array < CSSToken > ) : {
36
126
advance : number ;
37
127
node : FunctionNode ;
38
128
} ;
39
- export declare class SimpleBlockNode {
129
+ export declare class SimpleBlockNode extends ContainerNodeBaseClass {
40
130
type : ComponentValueType ;
41
131
startToken : CSSToken ;
42
132
endToken : CSSToken ;
43
- value : Array < ComponentValue > ;
44
133
constructor ( startToken : CSSToken , endToken : CSSToken , value : Array < ComponentValue > ) ;
45
134
/**
46
135
* Normalize the current Simple Block:
@@ -51,11 +140,6 @@ export declare class SimpleBlockNode {
51
140
toString ( ) : string ;
52
141
indexOf ( item : ComponentValue ) : number | string ;
53
142
at ( index : number | string ) : ComponentValue | undefined ;
54
- walk < T extends Record < string , unknown > > ( cb : ( entry : {
55
- node : ComponentValue ;
56
- parent : ContainerNode ;
57
- state ?: T ;
58
- } , index : number | string ) => boolean | void , state ?: T ) : false | undefined ;
59
143
toJSON ( ) : unknown ;
60
144
isSimpleBlockNode ( ) : this is SimpleBlockNode ;
61
145
static isSimpleBlockNode ( x : unknown ) : x is SimpleBlockNode ;
@@ -116,3 +200,4 @@ export declare class TokenNode {
116
200
isTokenNode ( ) : this is TokenNode ;
117
201
static isTokenNode ( x : unknown ) : x is TokenNode ;
118
202
}
203
+ export { } ;
0 commit comments