Skip to content

This PR contains: Improve types for stringify #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 44 additions & 23 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ 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<Node | object>): this;
replaceWith(...nodes: Array<ChildNode | object>): this;

// 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
Expand Down Expand Up @@ -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<Node | object | string>): this;
append(...nodes: Array<Node | object | string>): 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<ChildNode | object | string>): this;
append(...nodes: Array<ChildNode | object | string>): 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;
Expand All @@ -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
Expand All @@ -121,7 +129,7 @@ export type Node =
| UnicodeRange
| Word;

export type Container = Func | Interpolation;
export type Container = Root | Func | Interpolation;

export interface AtWord extends NodeBase {
type: "atrule";
Expand All @@ -141,6 +149,7 @@ export interface Func extends ContainerBase {
type: "func";
parent: Container;
isColor: boolean;
isVar: boolean;
name: string;
params: string;
}
Expand Down Expand Up @@ -210,6 +219,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;