From fd8006d940bb869cffed7b51172ed24e442890e1 Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Wed, 29 Jan 2020 00:36:35 -0800 Subject: [PATCH 1/4] Improve types for stringify We shouldn't re-use PostCSS's type because it expects PostCSS statement nodes rather than value nodes. --- lib/index.d.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index a6af47d..5fc4efa 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -28,7 +28,7 @@ export interface NodeBase { // Inherited from postcss.ContainerBase with no changes. source?: postcss.NodeSource; raws: postcss.NodeRaws; - toString(stringifier?: postcss.Stringifier | postcss.Syntax): string; + toString(stringifier?: Stringifier | Syntax): string; error( message: string, options?: postcss.NodeErrorOptions @@ -210,6 +210,18 @@ export interface VariablesOptions { prefixes: string[]; } -export const stringify: postcss.Stringifier; +interface Syntax { + stringify?: Stringifier; +} + +interface Builder { + (part: string, node?: Node, type?: "start" | "end"): void; +} + +export interface Stringifier { + (node: Node, builder: Builder): void; +} + +export const stringify: Stringifier; export function nodeToString(node: Node): string; From 42c5d521a8d4d9e1ca9770396550e8940dbd9cd9 Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Wed, 29 Jan 2020 00:38:20 -0800 Subject: [PATCH 2/4] Add a typing for Func.isVar --- lib/index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/index.d.ts b/lib/index.d.ts index 5fc4efa..dc14b04 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -141,6 +141,7 @@ export interface Func extends ContainerBase { type: "func"; parent: Container; isColor: boolean; + isVar: boolean; name: string; params: string; } From db0f332f261c4c3e9d4bc37f2d05214225c2bb10 Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Wed, 29 Jan 2020 00:51:07 -0800 Subject: [PATCH 3/4] 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. --- lib/index.d.ts | 48 ++++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index dc14b04..7e0134c 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -18,12 +18,12 @@ import * as postcss from "postcss"; export interface NodeBase { // Inherited from postcss.ContainerBase, but with our Node type. - next(): Node | void; - prev(): Node | void; - before(newNode: Node | object | string | Node[]): this; - after(newNode: Node | object | string | Node[]): this; + next(): ChildNode | void; + prev(): ChildNode | void; + before(newNode: ChildNode | object | string | ChildNode[]): this; + after(newNode: ChildNode | object | string | ChildNode[]): this; root(): Root; - replaceWith(...nodes: Array): this; + replaceWith(...nodes: Array): this; // Inherited from postcss.ContainerBase with no changes. source?: postcss.NodeSource; @@ -66,33 +66,39 @@ export interface ContainerBase extends NodeBase { walkWords(callback: (word: Word, index: number) => any): boolean | void; walkType( type: string, - callback: (node: Node, index: number) => any + callback: (node: ChildNode, index: number) => any ): boolean | void; // Inherited from postcss.ContainerBase, but with our Node type. - nodes: Node[]; - first?: Node; - last?: Node; - index(child: Node | number): number; + nodes: ChildNode[]; + first?: ChildNode; + last?: ChildNode; + index(child: ChildNode | number): number; every( - callback: (node: Node, index: number, nodes: Node[]) => any, + callback: (node: ChildNode, index: number, nodes: ChildNode[]) => any, thisArg?: any ): boolean; some( - callback: (node: Node, index: number, nodes: Node[]) => boolean, + callback: (node: ChildNode, index: number, nodes: ChildNode[]) => boolean, thisArg?: any ): boolean; - each(callback: (node: Node, index: number) => any): boolean | void; - walk(callback: (node: Node, index: number) => any): boolean | void; + each(callback: (node: ChildNode, index: number) => any): boolean | void; + walk(callback: (node: ChildNode, index: number) => any): boolean | void; walkAtWords(callback: (atWord: AtWord, index: number) => any): boolean | void; walkComments( callback: (comment: Comment, index: number) => any ): boolean | void; - prepend(...nodes: Array): this; - append(...nodes: Array): this; - insertBefore(oldNode: Node | number, newNode: Node | object | string): this; - insertAfter(oldNode: Node | number, newNode: Node | object | string): this; - removeChild(child: Node | number): this; + prepend(...nodes: Array): this; + append(...nodes: Array): this; + insertBefore( + oldNode: ChildNode | number, + newNode: ChildNode | object | string + ): this; + insertAfter( + oldNode: ChildNode | number, + newNode: ChildNode | object | string + ): this; + removeChild(child: ChildNode | number): this; // Inherited from postcss.ContainerBase with no changes. clone(overrides?: object): this; @@ -109,7 +115,9 @@ export interface Root extends ContainerBase { }): postcss.Result; } -export type Node = +export type Node = Root | ChildNode; + +export type ChildNode = | AtWord | Comment | Func From 3e1182e2a6019222bf981f197d1337ee5ddc1886 Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Tue, 4 Feb 2020 00:08:25 -0800 Subject: [PATCH 4/4] Mark Root as a Container --- lib/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.d.ts b/lib/index.d.ts index 7e0134c..97488dd 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -129,7 +129,7 @@ export type ChildNode = | UnicodeRange | Word; -export type Container = Func | Interpolation; +export type Container = Root | Func | Interpolation; export interface AtWord extends NodeBase { type: "atrule";