Skip to content

Commit be8b1df

Browse files
loiloevilebottnawi
authored andcommitted
feat: add TypeScript definitions (TrySound#52)
1 parent 79294f7 commit be8b1df

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed

lib/index.d.ts

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
declare namespace postcssValueParser {
2+
interface BaseNode {
3+
/**
4+
* The offset inside the CSS value at which the node starts
5+
*/
6+
sourceIndex: number;
7+
8+
/**
9+
* The node's characteristic value
10+
*/
11+
value: string;
12+
}
13+
14+
interface ClosableNode {
15+
/**
16+
* Whether the parsed CSS value ended before the node was properly closed
17+
*/
18+
unclosed?: true;
19+
}
20+
21+
interface AdjacentAwareNode {
22+
/**
23+
* The token at the start of the node
24+
*/
25+
before: string;
26+
27+
/**
28+
* The token at the end of the node
29+
*/
30+
after: string;
31+
}
32+
33+
interface CommentNode extends BaseNode, ClosableNode {
34+
type: "comment";
35+
}
36+
37+
interface DivNode extends BaseNode, AdjacentAwareNode {
38+
type: "div";
39+
}
40+
41+
interface FunctionNode extends BaseNode, ClosableNode, AdjacentAwareNode {
42+
type: "function";
43+
44+
/**
45+
* Nodes inside the function
46+
*/
47+
nodes: Node[];
48+
}
49+
50+
interface SpaceNode extends BaseNode {
51+
type: "space";
52+
}
53+
54+
interface StringNode extends BaseNode, ClosableNode {
55+
type: "string";
56+
57+
/**
58+
* The quote type delimiting the string
59+
*/
60+
quote: '"' | "'";
61+
}
62+
63+
interface UnicodeRangeNode extends BaseNode {
64+
type: "unicode-range";
65+
}
66+
67+
interface WordNode extends BaseNode {
68+
type: "word";
69+
}
70+
71+
/**
72+
* Any node parsed from a CSS value
73+
*/
74+
type Node =
75+
| CommentNode
76+
| DivNode
77+
| FunctionNode
78+
| SpaceNode
79+
| StringNode
80+
| UnicodeRangeNode
81+
| WordNode;
82+
83+
interface CustomStringifierCallback {
84+
/**
85+
* @param node The node to stringify
86+
* @returns The serialized CSS representation of the node
87+
*/
88+
(nodes: Node): string;
89+
}
90+
91+
interface WalkCallback {
92+
/**
93+
* @param node The currently visited node
94+
* @param index The index of the node in the series of parsed nodes
95+
* @param nodes The series of parsed nodes
96+
* @returns Returning `false` will prevent traversal of descendant nodes (only applies if `bubble` was set to `true` in the `walk()` call)
97+
*/
98+
(node: Node, index: number, nodes: Node[]): void | false;
99+
}
100+
101+
/**
102+
* A CSS dimension, decomposed into its numeric and unit parts
103+
*/
104+
interface Dimension {
105+
number: string;
106+
unit: string;
107+
}
108+
109+
/**
110+
* A wrapper around a parsed CSS value that allows for inspecting and walking nodes
111+
*/
112+
interface ParsedValue {
113+
/**
114+
* The series of parsed nodes
115+
*/
116+
nodes: Node[];
117+
118+
/**
119+
* Walk all parsed nodes, applying a callback
120+
*
121+
* @param callback A visitor callback that will be executed for each node
122+
* @param bubble When set to `true`, walking will be done inside-out instead of outside-in
123+
*/
124+
walk(callback: WalkCallback, bubble: boolean = false): this;
125+
}
126+
127+
interface ValueParser {
128+
/**
129+
* Decompose a CSS dimension into its numeric and unit part
130+
*
131+
* @param value The dimension to decompose
132+
* @returns An object representing `number` and `unit` part of the dimension or `false` if the decomposing fails
133+
*/
134+
unit(value: string): Dimension | false;
135+
136+
/**
137+
* Serialize a series of nodes into a CSS value
138+
*
139+
* @param nodes The nodes to stringify
140+
* @param custom A custom stringifier callback
141+
* @returns The generated CSS value
142+
*/
143+
stringify(nodes: Node[], custom?: CustomStringifierCallback): string;
144+
145+
/**
146+
* Walk a series of nodes, applying a callback
147+
*
148+
* @param nodes The nodes to walk
149+
* @param callback A visitor callback that will be executed for each node
150+
* @param bubble When set to `true`, walking will be done inside-out instead of outside-in
151+
*/
152+
walk(nodes: Node[], callback: WalkCallback, bubble: boolean = false): void;
153+
154+
/**
155+
* Parse a CSS value into a series of nodes to operate on
156+
*
157+
* @param value The value to parse
158+
*/
159+
new (value: string): ParsedValue;
160+
161+
/**
162+
* Parse a CSS value into a series of nodes to operate on
163+
*
164+
* @param value The value to parse
165+
*/
166+
(value: string): ParsedValue;
167+
}
168+
}
169+
170+
declare const postcssValueParser: postcssValueParser.ValueParser;
171+
172+
export = postcssValueParser;

0 commit comments

Comments
 (0)