Skip to content

Commit 94f301a

Browse files
authored
chore: Improve types for stringify (#100)
* Improve types for stringify We shouldn't re-use PostCSS's type because it expects PostCSS statement nodes rather than value nodes. * Add a typing for Func.isVar * Separate out ChildNode and Node types This matches PostCSS's type structure. It allows Root to be a Node (and thus passable to stringify()) without also allowing it to be used as a child. * Mark Root as a Container
1 parent 6b891ea commit 94f301a

File tree

1 file changed

+44
-23
lines changed

1 file changed

+44
-23
lines changed

lib/index.d.ts

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ import * as postcss from "postcss";
1818

1919
export interface NodeBase {
2020
// Inherited from postcss.ContainerBase, but with our Node type.
21-
next(): Node | void;
22-
prev(): Node | void;
23-
before(newNode: Node | object | string | Node[]): this;
24-
after(newNode: Node | object | string | Node[]): this;
21+
next(): ChildNode | void;
22+
prev(): ChildNode | void;
23+
before(newNode: ChildNode | object | string | ChildNode[]): this;
24+
after(newNode: ChildNode | object | string | ChildNode[]): this;
2525
root(): Root;
26-
replaceWith(...nodes: Array<Node | object>): this;
26+
replaceWith(...nodes: Array<ChildNode | object>): this;
2727

2828
// Inherited from postcss.ContainerBase with no changes.
2929
source?: postcss.NodeSource;
3030
raws: postcss.NodeRaws;
31-
toString(stringifier?: postcss.Stringifier | postcss.Syntax): string;
31+
toString(stringifier?: Stringifier | Syntax): string;
3232
error(
3333
message: string,
3434
options?: postcss.NodeErrorOptions
@@ -66,33 +66,39 @@ export interface ContainerBase extends NodeBase {
6666
walkWords(callback: (word: Word, index: number) => any): boolean | void;
6767
walkType(
6868
type: string,
69-
callback: (node: Node, index: number) => any
69+
callback: (node: ChildNode, index: number) => any
7070
): boolean | void;
7171

7272
// Inherited from postcss.ContainerBase, but with our Node type.
73-
nodes: Node[];
74-
first?: Node;
75-
last?: Node;
76-
index(child: Node | number): number;
73+
nodes: ChildNode[];
74+
first?: ChildNode;
75+
last?: ChildNode;
76+
index(child: ChildNode | number): number;
7777
every(
78-
callback: (node: Node, index: number, nodes: Node[]) => any,
78+
callback: (node: ChildNode, index: number, nodes: ChildNode[]) => any,
7979
thisArg?: any
8080
): boolean;
8181
some(
82-
callback: (node: Node, index: number, nodes: Node[]) => boolean,
82+
callback: (node: ChildNode, index: number, nodes: ChildNode[]) => boolean,
8383
thisArg?: any
8484
): boolean;
85-
each(callback: (node: Node, index: number) => any): boolean | void;
86-
walk(callback: (node: Node, index: number) => any): boolean | void;
85+
each(callback: (node: ChildNode, index: number) => any): boolean | void;
86+
walk(callback: (node: ChildNode, index: number) => any): boolean | void;
8787
walkAtWords(callback: (atWord: AtWord, index: number) => any): boolean | void;
8888
walkComments(
8989
callback: (comment: Comment, index: number) => any
9090
): boolean | void;
91-
prepend(...nodes: Array<Node | object | string>): this;
92-
append(...nodes: Array<Node | object | string>): this;
93-
insertBefore(oldNode: Node | number, newNode: Node | object | string): this;
94-
insertAfter(oldNode: Node | number, newNode: Node | object | string): this;
95-
removeChild(child: Node | number): this;
91+
prepend(...nodes: Array<ChildNode | object | string>): this;
92+
append(...nodes: Array<ChildNode | object | string>): this;
93+
insertBefore(
94+
oldNode: ChildNode | number,
95+
newNode: ChildNode | object | string
96+
): this;
97+
insertAfter(
98+
oldNode: ChildNode | number,
99+
newNode: ChildNode | object | string
100+
): this;
101+
removeChild(child: ChildNode | number): this;
96102

97103
// Inherited from postcss.ContainerBase with no changes.
98104
clone(overrides?: object): this;
@@ -109,7 +115,9 @@ export interface Root extends ContainerBase {
109115
}): postcss.Result;
110116
}
111117

112-
export type Node =
118+
export type Node = Root | ChildNode;
119+
120+
export type ChildNode =
113121
| AtWord
114122
| Comment
115123
| Func
@@ -121,7 +129,7 @@ export type Node =
121129
| UnicodeRange
122130
| Word;
123131

124-
export type Container = Func | Interpolation;
132+
export type Container = Root | Func | Interpolation;
125133

126134
export interface AtWord extends NodeBase {
127135
type: "atrule";
@@ -141,6 +149,7 @@ export interface Func extends ContainerBase {
141149
type: "func";
142150
parent: Container;
143151
isColor: boolean;
152+
isVar: boolean;
144153
name: string;
145154
params: string;
146155
}
@@ -210,6 +219,18 @@ export interface VariablesOptions {
210219
prefixes: string[];
211220
}
212221

213-
export const stringify: postcss.Stringifier;
222+
interface Syntax {
223+
stringify?: Stringifier;
224+
}
225+
226+
interface Builder {
227+
(part: string, node?: Node, type?: "start" | "end"): void;
228+
}
229+
230+
export interface Stringifier {
231+
(node: Node, builder: Builder): void;
232+
}
233+
234+
export const stringify: Stringifier;
214235

215236
export function nodeToString(node: Node): string;

0 commit comments

Comments
 (0)