Skip to content

Commit 3179bfe

Browse files
committed
Add TypeScript typings
1 parent 75c2374 commit 3179bfe

File tree

2 files changed

+215
-0
lines changed

2 files changed

+215
-0
lines changed

lib/index.d.ts

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
/*
2+
Copyright © 2019 Andrew Powell
3+
4+
This Source Code Form is subject to the terms of the Mozilla Public
5+
License, v. 2.0. If a copy of the MPL was not distributed with this
6+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
7+
8+
The above copyright notice and this permission notice shall be
9+
included in all copies or substantial portions of this Source Code Form.
10+
*/
11+
12+
import * as postcss from "postcss";
13+
14+
// Even though the concrete classes extend PostCSS classes, we can't extend
15+
// PostCSS Node types here because they refer to statements that aren't
16+
// compatible with our value nodes. This unfortunately means that we need to
17+
// replicate a bunch of PostCSS's method declarations here.
18+
19+
export interface NodeBase {
20+
// 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;
25+
root(): Root;
26+
replaceWith(...nodes: Array<Node | object>): this;
27+
28+
// Inherited from postcss.ContainerBase with no changes.
29+
source?: postcss.NodeSource;
30+
raws: postcss.NodeRaws;
31+
toString(stringifier?: postcss.Stringifier | postcss.Syntax): string;
32+
error(
33+
message: string,
34+
options?: postcss.NodeErrorOptions
35+
): postcss.CssSyntaxError;
36+
warn(
37+
result: postcss.Result,
38+
text: string,
39+
opts?: postcss.WarningOptions
40+
): void;
41+
remove(): this;
42+
clone(overrides?: object): this;
43+
cloneBefore(overrides?: object): this;
44+
cloneAfter(overrides?: object): this;
45+
raw(prop: string, defaultType?: string): any;
46+
}
47+
48+
export interface ContainerBase extends NodeBase {
49+
walkFuncs(callback: (decl: Func, index: number) => any): boolean | void;
50+
walkInterpolations(
51+
callback: (interpolation: Interpolation, index: number) => any
52+
): boolean | void;
53+
walkNumerics(
54+
callback: (numeric: Numeric, index: number) => any
55+
): boolean | void;
56+
walkOperators(
57+
callback: (operator: Operator, index: number) => any
58+
): boolean | void;
59+
walkPunctuations(
60+
callback: (punctuation: Punctuation, index: number) => any
61+
): boolean | void;
62+
walkQuoteds(callback: (quoted: Quoted, index: number) => any): boolean | void;
63+
walkUnicodeRanges(
64+
callback: (unicodeRange: UnicodeRange, index: number) => any
65+
): boolean | void;
66+
walkWords(callback: (word: Word, index: number) => any): boolean | void;
67+
walkType(
68+
type: string,
69+
callback: (node: Node, index: number) => any
70+
): boolean | void;
71+
72+
// Inherited from postcss.ContainerBase, but with our Node type.
73+
nodes?: Node[];
74+
first?: Node;
75+
last?: Node;
76+
index(child: Node | number): number;
77+
every(
78+
callback: (node: Node, index: number, nodes: Node[]) => any,
79+
thisArg?: any
80+
): boolean;
81+
some(
82+
callback: (node: Node, index: number, nodes: Node[]) => boolean,
83+
thisArg?: any
84+
): boolean;
85+
each(callback: (node: Node, index: number) => any): boolean | void;
86+
walk(callback: (node: Node, index: number) => any): boolean | void;
87+
walkAtWords(callback: (atWord: AtWord, index: number) => any): boolean | void;
88+
walkComments(
89+
callback: (comment: Comment, index: number) => any
90+
): 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;
96+
97+
// Inherited from postcss.ContainerBase with no changes.
98+
clone(overrides?: object): this;
99+
remove(): this;
100+
removeAll(): this;
101+
}
102+
103+
export interface Root extends ContainerBase {
104+
type: "root";
105+
parent: undefined;
106+
toResult(options?: {
107+
to?: string;
108+
map?: postcss.SourceMapOptions;
109+
}): postcss.Result;
110+
}
111+
112+
export type Node =
113+
| AtWord
114+
| Comment
115+
| Func
116+
| Interpolation
117+
| Numeric
118+
| Operator
119+
| Punctuation
120+
| Quoted
121+
| UnicodeRange
122+
| Word;
123+
124+
export type Container = Func | Interpolation;
125+
126+
export interface AtWord extends NodeBase {
127+
type: 'atrule';
128+
parent: Container;
129+
name: string;
130+
params: string;
131+
}
132+
133+
export interface Comment extends NodeBase {
134+
parent: Container;
135+
inline: boolean;
136+
text: string;
137+
}
138+
139+
export interface Func extends ContainerBase {
140+
type: "func";
141+
parent: Container;
142+
isColor: boolean;
143+
name: string;
144+
params: string;
145+
}
146+
147+
export interface Interpolation extends ContainerBase {
148+
type: "interpolation";
149+
parent: Container;
150+
params: string;
151+
prefix: string;
152+
}
153+
154+
export interface Numeric extends NodeBase {
155+
type: "numeric";
156+
parent: Container;
157+
unit: string;
158+
value: string;
159+
}
160+
161+
export interface Operator extends NodeBase {
162+
type: "operator";
163+
parent: Container;
164+
value: string;
165+
}
166+
167+
export interface Punctuation extends NodeBase {
168+
type: "punctuation";
169+
parent: Container;
170+
value: string;
171+
}
172+
173+
export interface Quoted extends NodeBase {
174+
type: "quoted";
175+
parent: Container;
176+
quote: string;
177+
value: string;
178+
}
179+
180+
export interface UnicodeRange extends NodeBase {
181+
type: "unicodeRange";
182+
parent: Container;
183+
name: string;
184+
}
185+
186+
export interface Word extends NodeBase {
187+
type: "word";
188+
parent: Container;
189+
isColor: boolean;
190+
isHex: boolean;
191+
isUrl: boolean;
192+
isVariable: boolean;
193+
value: boolean;
194+
}
195+
196+
export function parse(css: string, options: ParseOptions): Root;
197+
198+
export interface ParseOptions {
199+
ignoreUnknownWords?: boolean;
200+
interpolation?: boolean | InterpolationOptions;
201+
variables?: VariablesOptions;
202+
}
203+
204+
export interface InterpolationOptions {
205+
prefix: string;
206+
}
207+
208+
export interface VariablesOptions {
209+
prefixes: string[];
210+
}
211+
212+
export const stringify: postcss.Stringifier;
213+
214+
export function nodeToString(node: Node): string;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"homepage": "https://github.com/shellscape/postcss-values-parser",
1313
"bugs": "https://github.com/shellscape/postcss-values-parser/issues",
1414
"main": "lib/index.js",
15+
"types": "lib/index.d.js",
1516
"engines": {
1617
"node": ">=6.14.4"
1718
},

0 commit comments

Comments
 (0)