blob: 52bdb7709db0b2caa72ec0b1de40f6dec6c1635d [file] [log] [blame]
Mathias Bynens79e2cf02020-05-29 16:46:17 +02001// Type definitions for postcss-selector-parser 2.2.3
2// Definitions by: Chris Eppstein <chris@eppsteins.net>
3
4/*~ Note that ES6 modules cannot directly export callable functions.
5 *~ This file should be imported using the CommonJS-style:
6 *~ import x = require('someLibrary');
7 *~
8 *~ Refer to the documentation to understand common
9 *~ workarounds for this limitation of ES6 modules.
10 */
11
12/*~ This declaration specifies that the function
13 *~ is the exported object from the file
14 */
15export = parser;
16
17// TODO: Conditional types in TS 1.8 will really clean this up.
18declare function parser(): parser.Processor<never>;
19declare function parser<Transform>(processor: parser.AsyncProcessor<Transform>): parser.Processor<Transform, never>;
20declare function parser(processor: parser.AsyncProcessor<void>): parser.Processor<never, never>;
21declare function parser<Transform>(processor: parser.SyncProcessor<Transform>): parser.Processor<Transform>;
22declare function parser(processor: parser.SyncProcessor<void>): parser.Processor<never>;
23declare function parser<Transform>(processor?: parser.SyncProcessor<Transform> | parser.AsyncProcessor<Transform>): parser.Processor<Transform>;
24
25/*~ If you want to expose types from your module as well, you can
26 *~ place them in this block. Often you will want to describe the
27 *~ shape of the return type of the function; that type should
28 *~ be declared in here, as this example shows.
29 */
30declare namespace parser {
31 /* copied from postcss -- so we don't need to add a dependency */
32 type ErrorOptions = {
33 plugin?: string;
34 word?: string;
35 index?: number
36 };
37 /* the bits we use of postcss.Rule, copied from postcss -- so we don't need to add a dependency */
38 type PostCSSRuleNode = {
39 selector: string
40 /**
41 * @returns postcss.CssSyntaxError but it's a complex object, caller
42 * should cast to it if they have a dependency on postcss.
43 */
44 error(message: string, options?: ErrorOptions): Error;
45 };
46 /** Accepts a string */
47 type Selectors = string | PostCSSRuleNode
48 type ProcessorFn<ReturnType = void> = (root: parser.Root) => ReturnType;
49 type SyncProcessor<Transform = void> = ProcessorFn<Transform>;
50 type AsyncProcessor<Transform = void> = ProcessorFn<PromiseLike<Transform>>;
51
52 const TAG: "tag";
53 const STRING: "string";
54 const SELECTOR: "selector";
55 const ROOT: "root";
56 const PSEUDO: "pseudo";
57 const NESTING: "nesting";
58 const ID: "id";
59 const COMMENT: "comment";
60 const COMBINATOR: "combinator";
61 const CLASS: "class";
62 const ATTRIBUTE: "attribute";
63 const UNIVERSAL: "universal";
64
65 interface NodeTypes {
66 tag: Tag,
67 string: String,
68 selector: Selector,
69 root: Root,
70 pseudo: Pseudo,
71 nesting: Nesting,
72 id: Identifier,
73 comment: Comment,
74 combinator: Combinator,
75 class: ClassName,
76 attribute: Attribute,
77 universal: Universal
78 }
79
80 type Node = NodeTypes[keyof NodeTypes];
81
82 function isNode(node: any): node is Node;
83
84 interface Options {
85 /**
86 * Preserve whitespace when true. Default: false;
87 */
88 lossless: boolean;
89 /**
90 * When true and a postcss.Rule is passed, set the result of
91 * processing back onto the rule when done. Default: false.
92 */
93 updateSelector: boolean;
94 }
95 class Processor<
96 TransformType = never,
97 SyncSelectorsType extends Selectors | never = Selectors
98 > {
99 res: Root;
100 readonly result: String;
101 ast(selectors: Selectors, options?: Partial<Options>): Promise<Root>;
102 astSync(selectors: SyncSelectorsType, options?: Partial<Options>): Root;
103 transform(selectors: Selectors, options?: Partial<Options>): Promise<TransformType>;
104 transformSync(selectors: SyncSelectorsType, options?: Partial<Options>): TransformType;
105 process(selectors: Selectors, options?: Partial<Options>): Promise<string>;
106 processSync(selectors: SyncSelectorsType, options?: Partial<Options>): string;
107 }
108 interface ParserOptions {
109 css: string;
110 error: (message: string, options: ErrorOptions) => Error;
111 options: Options;
112 }
113 class Parser {
114 input: ParserOptions;
115 lossy: boolean;
116 position: number;
117 root: Root;
118 selectors: string;
119 current: Selector;
120 constructor(input: ParserOptions);
121 /**
122 * Raises an error, if the processor is invoked on
123 * a postcss Rule node, a better error message is raised.
124 */
125 error(message: string, options?: ErrorOptions): void;
126 }
127 interface NodeSource {
128 start?: {
129 line: number,
130 column: number
131 },
132 end?: {
133 line: number,
134 column: number
135 }
136 }
137 interface SpaceAround {
138 before: string;
139 after: string;
140 }
141 interface Spaces extends SpaceAround {
142 [spaceType: string]: string | Partial<SpaceAround> | undefined;
143 }
144 interface NodeOptions<Value = string> {
145 value: Value;
146 spaces?: Partial<Spaces>;
147 source?: NodeSource;
148 sourceIndex?: number;
149 }
150 interface Base<
151 Value extends string | undefined = string,
152 ParentType extends Container | undefined = Container | undefined
153 > {
154 type: keyof NodeTypes;
155 parent: ParentType;
156 value: Value;
157 spaces: Spaces;
158 source?: NodeSource;
159 sourceIndex: number;
160 rawSpaceBefore: string;
161 rawSpaceAfter: string;
162 remove(): Node;
163 replaceWith(...nodes: Node[]): Node;
164 next(): Node;
165 prev(): Node;
166 clone(opts: {[override: string]:any}): Node;
167 /**
168 * Return whether this node includes the character at the position of the given line and column.
169 * Returns undefined if the nodes lack sufficient source metadata to determine the position.
170 * @param line 1-index based line number relative to the start of the selector.
171 * @param column 1-index based column number relative to the start of the selector.
172 */
173 isAtPosition(line: number, column: number): boolean | undefined;
174 /**
175 * Some non-standard syntax doesn't follow normal escaping rules for css,
176 * this allows the escaped value to be specified directly, allowing illegal characters to be
177 * directly inserted into css output.
178 * @param name the property to set
179 * @param value the unescaped value of the property
180 * @param valueEscaped optional. the escaped value of the property.
181 */
182 setPropertyAndEscape(name: string, value: any, valueEscaped: string): void;
183 /**
184 * When you want a value to passed through to CSS directly. This method
185 * deletes the corresponding raw value causing the stringifier to fallback
186 * to the unescaped value.
187 * @param name the property to set.
188 * @param value The value that is both escaped and unescaped.
189 */
190 setPropertyWithoutEscape(name: string, value: any): void;
191 /**
192 * Some non-standard syntax doesn't follow normal escaping rules for css.
193 * This allows non standard syntax to be appended to an existing property
194 * by specifying the escaped value. By specifying the escaped value,
195 * illegal characters are allowed to be directly inserted into css output.
196 * @param {string} name the property to set
197 * @param {any} value the unescaped value of the property
198 * @param {string} valueEscaped optional. the escaped value of the property.
199 */
200 appendToPropertyAndEscape(name: string, value: any, valueEscaped: string): void;
201 toString(): string;
202 }
203 interface ContainerOptions extends NodeOptions {
204 nodes?: Array<Node>;
205 }
206 interface Container<Value extends string | undefined = string> extends Base<Value> {
207 nodes: Array<Node>;
208 append(selector: Selector): Container;
209 prepend(selector: Selector): Container;
210 at(index: number): Node;
211 /**
212 * Return the most specific node at the line and column number given.
213 * The source location is based on the original parsed location, locations aren't
214 * updated as selector nodes are mutated.
215 *
216 * Note that this location is relative to the location of the first character
217 * of the selector, and not the location of the selector in the overall document
218 * when used in conjunction with postcss.
219 *
220 * If not found, returns undefined.
221 * @param line The line number of the node to find. (1-based index)
222 * @param col The column number of the node to find. (1-based index)
223 */
224 atPosition(line: number, column: number): Node;
225 index(child: Node): number;
226 readonly first: Node;
227 readonly last: Node;
228 readonly length: number;
229 removeChild(child: Node): Container;
230 removeAll(): Container;
231 empty(): Container;
232 insertAfter(oldNode: Node, newNode: Node): Container;
233 insertBefore(oldNode: Node, newNode: Node): Container;
234 each(callback: (node: Node) => boolean | void): boolean | undefined;
235 walk(callback: (node: Node) => boolean | void): boolean | undefined;
236 walkAttributes(callback: (node: Node) => boolean | void): boolean | undefined;
237 walkClasses(callback: (node: Node) => boolean | void): boolean | undefined;
238 walkCombinators(callback: (node: Node) => boolean | void): boolean | undefined;
239 walkComments(callback: (node: Node) => boolean | void): boolean | undefined;
240 walkIds(callback: (node: Node) => boolean | void): boolean | undefined;
241 walkNesting(callback: (node: Node) => boolean | void): boolean | undefined;
242 walkPseudos(callback: (node: Node) => boolean | void): boolean | undefined;
243 walkTags(callback: (node: Node) => boolean | void): boolean | undefined;
244 split(callback: (node: Node) => boolean): [Node[], Node[]];
245 map(callback: (node: Node) => Node): Node[];
246 reduce<T>(callback: (node: Node) => Node, memo: T): T;
247 every(callback: (node: Node) => boolean): boolean;
248 some(callback: (node: Node) => boolean): boolean;
249 filter(callback: (node: Node) => boolean): Node[];
250 sort(callback: (nodeA: Node, nodeB: Node) => number): Node[];
251 toString(): string;
252 }
253 function isContainer(node: any): node is Root | Selector | Pseudo;
254
255 interface NamespaceOptions<Value extends string | undefined = string> extends NodeOptions<Value> {
256 namespace?: string | true;
257 }
258 interface Namespace<Value extends string | undefined = string> extends Base<Value> {
259 /** alias for namespace */
260 ns: string | true;
261 /**
262 * namespace prefix.
263 */
264 namespace: string | true;
265 /**
266 * If a namespace exists, prefix the value provided with it, separated by |.
267 */
268 qualifiedName(value: string): string;
269 /**
270 * A string representing the namespace suitable for output.
271 */
272 readonly namespaceString: string;
273 }
274 function isNamespace(node: any): node is Attribute | Tag;
275
276 interface Root extends Container<undefined> {
277 type: "root";
278 /**
279 * Raises an error, if the processor is invoked on
280 * a postcss Rule node, a better error message is raised.
281 */
282 error(message: string, options?: ErrorOptions): Error;
283 nodeAt(line: number, column: number): Node
284 }
285 function root(opts: ContainerOptions): Root;
286 function isRoot(node: any): node is Root;
287
288 interface Selector extends Container {
289 type: "selector";
290 }
291 function selector(opts: ContainerOptions): Selector;
292 function isSelector(node: any): node is Selector;
293
294 interface Combinator extends Base {
295 type: "combinator"
296 }
297 function combinator(opts: NodeOptions): Combinator;
298 function isCombinator(node: any): node is Combinator;
299
300 interface ClassName extends Base {
301 type: "class";
302 }
303 function className(opts: NamespaceOptions): ClassName;
304 function isClassName(node: any): node is ClassName;
305
306 type AttributeOperator = "=" | "~=" | "|=" | "^=" | "$=" | "*=";
307 type QuoteMark = '"' | "'" | null;
308 interface PreferredQuoteMarkOptions {
309 quoteMark?: QuoteMark;
310 preferCurrentQuoteMark?: boolean;
311 }
312 interface SmartQuoteMarkOptions extends PreferredQuoteMarkOptions {
313 smart?: boolean;
314 }
315 interface AttributeOptions extends NamespaceOptions<string | undefined> {
316 attribute: string;
317 operator?: AttributeOperator;
318 insensitive?: boolean;
319 quoteMark?: QuoteMark;
320 /** @deprecated Use quoteMark instead. */
321 quoted?: boolean;
322 spaces?: {
323 before?: string;
324 after?: string;
325 attribute?: Partial<SpaceAround>;
326 operator?: Partial<SpaceAround>;
327 value?: Partial<SpaceAround>;
328 insensitive?: Partial<SpaceAround>;
329 }
330 raws: {
331 unquoted?: string;
332 attribute?: string;
333 operator?: string;
334 value?: string;
335 insensitive?: string;
336 spaces?: {
337 attribute?: Partial<Spaces>;
338 operator?: Partial<Spaces>;
339 value?: Partial<Spaces>;
340 insensitive?: Partial<Spaces>;
341 }
342 };
343 }
344 interface Attribute extends Namespace<string | undefined> {
345 type: "attribute";
346 attribute: string;
347 operator?: AttributeOperator;
348 insensitive?: boolean;
349 quoteMark: QuoteMark;
350 quoted?: boolean;
351 spaces: {
352 before: string;
353 after: string;
354 attribute?: Partial<Spaces>;
355 operator?: Partial<Spaces>;
356 value?: Partial<Spaces>;
357 insensitive?: Partial<Spaces>;
358 }
359 raws: {
360 /** @deprecated The attribute value is unquoted, use that instead.. */
361 unquoted?: string;
362 attribute?: string;
363 operator?: string;
364 /** The value of the attribute with quotes and escapes. */
365 value?: string;
366 insensitive?: string;
367 spaces?: {
368 attribute?: Partial<Spaces>;
369 operator?: Partial<Spaces>;
370 value?: Partial<Spaces>;
371 insensitive?: Partial<Spaces>;
372 }
373 };
374 /**
375 * The attribute name after having been qualified with a namespace.
376 */
377 readonly qualifiedAttribute: string;
378
379 /**
380 * The case insensitivity flag or an empty string depending on whether this
381 * attribute is case insensitive.
382 */
383 readonly insensitiveFlag : 'i' | '';
384
385 /**
386 * Returns the attribute's value quoted such that it would be legal to use
387 * in the value of a css file. The original value's quotation setting
388 * used for stringification is left unchanged. See `setValue(value, options)`
389 * if you want to control the quote settings of a new value for the attribute or
390 * `set quoteMark(mark)` if you want to change the quote settings of the current
391 * value.
392 *
393 * You can also change the quotation used for the current value by setting quoteMark.
394 **/
395 getQuotedValue(options?: SmartQuoteMarkOptions): string;
396
397 /**
398 * Set the unescaped value with the specified quotation options. The value
399 * provided must not include any wrapping quote marks -- those quotes will
400 * be interpreted as part of the value and escaped accordingly.
401 * @param value
402 */
403 setValue(value: string, options?: SmartQuoteMarkOptions): void;
404
405 /**
406 * Intelligently select a quoteMark value based on the value's contents. If
407 * the value is a legal CSS ident, it will not be quoted. Otherwise a quote
408 * mark will be picked that minimizes the number of escapes.
409 *
410 * If there's no clear winner, the quote mark from these options is used,
411 * then the source quote mark (this is inverted if `preferCurrentQuoteMark` is
412 * true). If the quoteMark is unspecified, a double quote is used.
413 **/
414 smartQuoteMark(options: PreferredQuoteMarkOptions): QuoteMark;
415
416 /**
417 * Selects the preferred quote mark based on the options and the current quote mark value.
418 * If you want the quote mark to depend on the attribute value, call `smartQuoteMark(opts)`
419 * instead.
420 */
421 preferredQuoteMark(options: PreferredQuoteMarkOptions): QuoteMark
422
423 /**
424 * returns the offset of the attribute part specified relative to the
425 * start of the node of the output string.
426 *
427 * * "ns" - alias for "namespace"
428 * * "namespace" - the namespace if it exists.
429 * * "attribute" - the attribute name
430 * * "attributeNS" - the start of the attribute or its namespace
431 * * "operator" - the match operator of the attribute
432 * * "value" - The value (string or identifier)
433 * * "insensitive" - the case insensitivity flag;
434 * @param part One of the possible values inside an attribute.
435 * @returns -1 if the name is invalid or the value doesn't exist in this attribute.
436 */
437 offsetOf(part: "ns" | "namespace" | "attribute" | "attributeNS" | "operator" | "value" | "insensitive"): number;
438 }
439 function attribute(opts: AttributeOptions): Attribute;
440 function isAttribute(node: any): node is Attribute;
441
442 interface Pseudo extends Container {
443 type: "pseudo";
444 }
445 function pseudo(opts: ContainerOptions): Pseudo;
446 /**
447 * Checks wether the node is the Psuedo subtype of node.
448 */
449 function isPseudo(node: any): node is Pseudo;
450
451 /**
452 * Checks wether the node is, specifically, a pseudo element instead of
453 * pseudo class.
454 */
455 function isPseudoElement(node: any): node is Pseudo;
456
457 /**
458 * Checks wether the node is, specifically, a pseudo class instead of
459 * pseudo element.
460 */
461 function isPseudoClass(node: any): node is Pseudo;
462
463
464 interface Tag extends Namespace {
465 type: "tag";
466 }
467 function tag(opts: NamespaceOptions): Tag;
468 function isTag(node: any): node is Tag;
469
470 interface Comment extends Base {
471 type: "comment";
472 }
473 function comment(opts: NodeOptions): Comment;
474 function isComment(node: any): node is Comment;
475
476 interface Identifier extends Base {
477 type: "id";
478 }
479 function id(opts: any): any;
480 function isIdentifier(node: any): node is Identifier;
481
482 interface Nesting extends Base {
483 type: "nesting";
484 }
485 function nesting(opts: any): any;
486 function isNesting(node: any): node is Nesting;
487
488 interface String extends Base {
489 type: "string";
490 }
491 function string(opts: NodeOptions): String;
492 function isString(node: any): node is String;
493
494 interface Universal extends Base {
495 type: "universal";
496 }
497 function universal(opts?: NamespaceOptions): any;
498 function isUniversal(node: any): node is Universal;
499}