UNPKG

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