UNPKG

8.46 kBTypeScriptView Raw
1// Type definitions for css
2// Project: https://github.com/reworkcss/css
3// Definitions by: Ilya Verbitskiy <https://github.com/ilich>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5
6/**
7 * CSS parser / stringifier for Node.js
8 */
9
10
11/**
12 * css.parse options
13 */
14export interface ParserOptions {
15 /** Silently fail on parse errors */
16 silent?: boolean | undefined;
17 /** The path to the file containing css. Makes errors and source maps more helpful, by letting them know where code comes from. */
18 source?: string | undefined;
19}
20
21/**
22 * css.stringify options
23 */
24export interface StringifyOptions {
25 /** The string used to indent the output. Defaults to two spaces. */
26 indent?: string | undefined;
27 /** Omit comments and extraneous whitespace. */
28 compress?: boolean | undefined;
29 /** Return a sourcemap along with the CSS output.
30 * Using the source option of css.parse is strongly recommended
31 * when creating a source map. Specify sourcemap: 'generator'
32 * to return the SourceMapGenerator object instead of serializing the source map.
33 */
34 sourcemap?: string | undefined;
35 /** (enabled by default, specify false to disable)
36 * Reads any source maps referenced by the input files
37 * when generating the output source map. When enabled,
38 * file system access may be required for reading the referenced source maps.
39 */
40 inputSourcemaps?: boolean | undefined;
41}
42
43/**
44 * Error thrown during parsing.
45 */
46export interface ParserError {
47 /** The full error message with the source position. */
48 message?: string | undefined;
49 /** The error message without position. */
50 reason?: string | undefined;
51 /** The value of options.source if passed to css.parse. Otherwise undefined. */
52 filename?: string | undefined;
53 line?: number | undefined;
54 column?: number | undefined;
55 /** The portion of code that couldn't be parsed. */
56 source?: string | undefined;
57}
58
59// ---------------------------------------------------------------------------------
60// AST Tree
61// ---------------------------------------------------------------------------------
62
63/**
64 * Information about a position in the code.
65 * The line and column numbers are 1-based: The first line is 1 and the first column of a line is 1 (not 0).
66 */
67export interface Position {
68 line?: number | undefined;
69 column?: number | undefined;
70}
71
72/**
73 * Base AST Tree Node.
74 */
75export interface Node {
76 /** The possible values are the ones listed in the Types section on https://github.com/reworkcss/css page. */
77 type?: string | undefined;
78 /** A reference to the parent node, or null if the node has no parent. */
79 parent?: Node | undefined;
80 /** Information about the position in the source string that corresponds to the node. */
81 position?: {
82 start?: Position | undefined;
83 end?: Position | undefined;
84 /** The value of options.source if passed to css.parse. Otherwise undefined. */
85 source?: string | undefined;
86 /** The full source string passed to css.parse. */
87 content?: string | undefined;
88 } | undefined;
89}
90
91export interface Rule extends Node {
92 /** The list of selectors of the rule, split on commas. Each selector is trimmed from whitespace and comments. */
93 selectors?: Array<string> | undefined;
94 /** Array of nodes with the types declaration and comment. */
95 declarations?: Array<Declaration | Comment> | undefined;
96}
97
98export interface Declaration extends Node {
99 /** The property name, trimmed from whitespace and comments. May not be empty. */
100 property?: string | undefined;
101 /** The value of the property, trimmed from whitespace and comments. Empty values are allowed. */
102 value?: string | undefined;
103}
104
105/**
106 * A rule-level or declaration-level comment. Comments inside selectors, properties and values etc. are lost.
107 */
108export interface Comment extends Node {
109 comment?: string | undefined;
110}
111
112/**
113 * The @charset at-rule.
114 */
115export interface Charset extends Node {
116 /** The part following @charset. */
117 charset?: string | undefined;
118}
119
120/**
121 * The @custom-media at-rule
122 */
123export interface CustomMedia extends Node {
124 /** The ---prefixed name. */
125 name?: string | undefined;
126 /** The part following the name. */
127 media?: string | undefined;
128}
129
130/**
131 * The @document at-rule.
132 */
133export interface Document extends Node {
134 /** The part following @document. */
135 document?: string | undefined;
136 /** The vendor prefix in @document, or undefined if there is none. */
137 vendor?: string | undefined;
138 /** Array of nodes with the types rule, comment and any of the at-rule types. */
139 rules?: Array<Rule | Comment | AtRule> | undefined;
140}
141
142/**
143 * The @font-face at-rule.
144 */
145export interface FontFace extends Node {
146 /** Array of nodes with the types declaration and comment. */
147 declarations?: Array<Declaration | Comment> | undefined;
148}
149
150/**
151 * The @host at-rule.
152 */
153export interface Host extends Node {
154 /** Array of nodes with the types rule, comment and any of the at-rule types. */
155 rules?: Array<Rule | Comment | AtRule> | undefined;
156}
157
158/**
159 * The @import at-rule.
160 */
161export interface Import extends Node {
162 /** The part following @import. */
163 import?: string | undefined;
164}
165
166/**
167 * The @keyframes at-rule.
168 */
169export interface KeyFrames extends Node {
170 /** The name of the keyframes rule. */
171 name?: string | undefined;
172 /** The vendor prefix in @keyframes, or undefined if there is none. */
173 vendor?: string | undefined;
174 /** Array of nodes with the types keyframe and comment. */
175 keyframes?: Array<KeyFrame | Comment> | undefined;
176}
177
178export interface KeyFrame extends Node {
179 /** The list of "selectors" of the keyframe rule, split on commas. Each “selector” is trimmed from whitespace. */
180 values?: Array<string> | undefined;
181 /** Array of nodes with the types declaration and comment. */
182 declarations?: Array<Declaration | Comment> | undefined;
183}
184
185/**
186 * The @media at-rule.
187 */
188export interface Media extends Node {
189 /** The part following @media. */
190 media?: string | undefined;
191 /** Array of nodes with the types rule, comment and any of the at-rule types. */
192 rules?: Array<Rule | Comment | AtRule> | undefined;
193}
194
195/**
196 * The @namespace at-rule.
197 */
198export interface Namespace extends Node {
199 /** The part following @namespace. */
200 namespace?: string | undefined;
201}
202
203/**
204 * The @page at-rule.
205 */
206export interface Page extends Node {
207 /** The list of selectors of the rule, split on commas. Each selector is trimmed from whitespace and comments. */
208 selectors?: Array<string> | undefined;
209 /** Array of nodes with the types declaration and comment. */
210 declarations?: Array<Declaration | Comment> | undefined;
211}
212
213/**
214 * The @supports at-rule.
215 */
216export interface Supports extends Node {
217 /** The part following @supports. */
218 supports?: string | undefined;
219 /** Array of nodes with the types rule, comment and any of the at-rule types. */
220 rules?: Array<Rule | Comment | AtRule> | undefined;
221}
222
223/** All at-rules. */
224export type AtRule = Charset | CustomMedia | Document | FontFace | Host | Import | KeyFrames | Media | Namespace | Page | Supports;
225
226/**
227 * A collection of rules
228 */
229export interface StyleRules {
230 /** Array of nodes with the types rule, comment and any of the at-rule types. */
231 rules: Array<Rule | Comment | AtRule>;
232 /** Array of Errors. Errors collected during parsing when option silent is true. */
233 parsingErrors?: Array<ParserError> | undefined
234}
235
236/**
237 * The root node returned by css.parse.
238 */
239export interface Stylesheet extends Node {
240 stylesheet?: StyleRules | undefined;
241}
242
243// ---------------------------------------------------------------------------------
244
245/**
246 * Accepts a CSS string and returns an AST object.
247 *
248 * @param {string} code - CSS code.
249 * @param {ParserOptions} options - CSS parser options.
250 * @return {Stylesheet} AST object built using provides CSS code.
251 */
252export function parse(code: string, options?: ParserOptions): Stylesheet;
253
254/**
255 * Accepts an AST object (as css.parse produces) and returns a CSS string.
256 *
257 * @param {Stylesheet} stylesheet - AST tree.
258 * @param {StringifyOptions} options - AST tree to string serializaiton options.
259 * @return {string} CSS code.
260 */
261export function stringify(stylesheet: Stylesheet, options?: StringifyOptions): string;