UNPKG

20.9 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 | undefined;
168 prev(): Node | undefined;
169 clone(opts?: {[override: string]:any}): this;
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: Child): this;
215 prepend(selector: Child): 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(): this;
237 empty(): this;
238 insertAfter(oldNode: Child, newNode: Child): this;
239 insertBefore(oldNode: Child, newNode: Child): this;
240 each(callback: (node: Child, index: number) => boolean | void): boolean | undefined;
241 walk(
242 callback: (node: Node, index: number) => 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 CombinatorRaws {
343 value?: string;
344 spaces?: {
345 before?: string;
346 after?: string;
347 };
348 }
349 interface Combinator extends Base {
350 type: "combinator";
351 raws?: CombinatorRaws;
352 }
353 function combinator(opts: NodeOptions): Combinator;
354 function isCombinator(node: any): node is Combinator;
355
356 interface ClassName extends Base {
357 type: "class";
358 }
359 function className(opts: NamespaceOptions): ClassName;
360 function isClassName(node: any): node is ClassName;
361
362 type AttributeOperator = "=" | "~=" | "|=" | "^=" | "$=" | "*=";
363 type QuoteMark = '"' | "'" | null;
364 interface PreferredQuoteMarkOptions {
365 quoteMark?: QuoteMark;
366 preferCurrentQuoteMark?: boolean;
367 }
368 interface SmartQuoteMarkOptions extends PreferredQuoteMarkOptions {
369 smart?: boolean;
370 }
371 interface AttributeOptions extends NamespaceOptions<string | undefined> {
372 attribute: string;
373 operator?: AttributeOperator;
374 insensitive?: boolean;
375 quoteMark?: QuoteMark;
376 /** @deprecated Use quoteMark instead. */
377 quoted?: boolean;
378 spaces?: {
379 before?: string;
380 after?: string;
381 attribute?: Partial<SpaceAround>;
382 operator?: Partial<SpaceAround>;
383 value?: Partial<SpaceAround>;
384 insensitive?: Partial<SpaceAround>;
385 }
386 raws: {
387 unquoted?: string;
388 attribute?: string;
389 operator?: string;
390 value?: string;
391 insensitive?: string;
392 spaces?: {
393 attribute?: Partial<Spaces>;
394 operator?: Partial<Spaces>;
395 value?: Partial<Spaces>;
396 insensitive?: Partial<Spaces>;
397 }
398 };
399 }
400 interface Attribute extends Namespace<string | undefined> {
401 type: "attribute";
402 attribute: string;
403 operator?: AttributeOperator;
404 insensitive?: boolean;
405 quoteMark: QuoteMark;
406 quoted?: boolean;
407 spaces: {
408 before: string;
409 after: string;
410 attribute?: Partial<Spaces>;
411 operator?: Partial<Spaces>;
412 value?: Partial<Spaces>;
413 insensitive?: Partial<Spaces>;
414 }
415 raws: {
416 /** @deprecated The attribute value is unquoted, use that instead.. */
417 unquoted?: string;
418 attribute?: string;
419 operator?: string;
420 /** The value of the attribute with quotes and escapes. */
421 value?: string;
422 insensitive?: string;
423 spaces?: {
424 attribute?: Partial<Spaces>;
425 operator?: Partial<Spaces>;
426 value?: Partial<Spaces>;
427 insensitive?: Partial<Spaces>;
428 }
429 };
430 /**
431 * The attribute name after having been qualified with a namespace.
432 */
433 readonly qualifiedAttribute: string;
434
435 /**
436 * The case insensitivity flag or an empty string depending on whether this
437 * attribute is case insensitive.
438 */
439 readonly insensitiveFlag : 'i' | '';
440
441 /**
442 * Returns the attribute's value quoted such that it would be legal to use
443 * in the value of a css file. The original value's quotation setting
444 * used for stringification is left unchanged. See `setValue(value, options)`
445 * if you want to control the quote settings of a new value for the attribute or
446 * `set quoteMark(mark)` if you want to change the quote settings of the current
447 * value.
448 *
449 * You can also change the quotation used for the current value by setting quoteMark.
450 **/
451 getQuotedValue(options?: SmartQuoteMarkOptions): string;
452
453 /**
454 * Set the unescaped value with the specified quotation options. The value
455 * provided must not include any wrapping quote marks -- those quotes will
456 * be interpreted as part of the value and escaped accordingly.
457 * @param value
458 */
459 setValue(value: string, options?: SmartQuoteMarkOptions): void;
460
461 /**
462 * Intelligently select a quoteMark value based on the value's contents. If
463 * the value is a legal CSS ident, it will not be quoted. Otherwise a quote
464 * mark will be picked that minimizes the number of escapes.
465 *
466 * If there's no clear winner, the quote mark from these options is used,
467 * then the source quote mark (this is inverted if `preferCurrentQuoteMark` is
468 * true). If the quoteMark is unspecified, a double quote is used.
469 **/
470 smartQuoteMark(options: PreferredQuoteMarkOptions): QuoteMark;
471
472 /**
473 * Selects the preferred quote mark based on the options and the current quote mark value.
474 * If you want the quote mark to depend on the attribute value, call `smartQuoteMark(opts)`
475 * instead.
476 */
477 preferredQuoteMark(options: PreferredQuoteMarkOptions): QuoteMark
478
479 /**
480 * returns the offset of the attribute part specified relative to the
481 * start of the node of the output string.
482 *
483 * * "ns" - alias for "namespace"
484 * * "namespace" - the namespace if it exists.
485 * * "attribute" - the attribute name
486 * * "attributeNS" - the start of the attribute or its namespace
487 * * "operator" - the match operator of the attribute
488 * * "value" - The value (string or identifier)
489 * * "insensitive" - the case insensitivity flag;
490 * @param part One of the possible values inside an attribute.
491 * @returns -1 if the name is invalid or the value doesn't exist in this attribute.
492 */
493 offsetOf(part: "ns" | "namespace" | "attribute" | "attributeNS" | "operator" | "value" | "insensitive"): number;
494 }
495 function attribute(opts: AttributeOptions): Attribute;
496 function isAttribute(node: any): node is Attribute;
497
498 interface Pseudo extends Container<string, Selector> {
499 type: "pseudo";
500 }
501 function pseudo(opts: ContainerOptions): Pseudo;
502 /**
503 * Checks whether the node is the Pseudo subtype of node.
504 */
505 function isPseudo(node: any): node is Pseudo;
506
507 /**
508 * Checks whether the node is, specifically, a pseudo element instead of
509 * pseudo class.
510 */
511 function isPseudoElement(node: any): node is Pseudo;
512
513 /**
514 * Checks whether the node is, specifically, a pseudo class instead of
515 * pseudo element.
516 */
517 function isPseudoClass(node: any): node is Pseudo;
518
519
520 interface Tag extends Namespace {
521 type: "tag";
522 }
523 function tag(opts: NamespaceOptions): Tag;
524 function isTag(node: any): node is Tag;
525
526 interface Comment extends Base {
527 type: "comment";
528 }
529 function comment(opts: NodeOptions): Comment;
530 function isComment(node: any): node is Comment;
531
532 interface Identifier extends Base {
533 type: "id";
534 }
535 function id(opts: any): Identifier;
536 function isIdentifier(node: any): node is Identifier;
537
538 interface Nesting extends Base {
539 type: "nesting";
540 }
541 function nesting(opts?: any): Nesting;
542 function isNesting(node: any): node is Nesting;
543
544 interface String extends Base {
545 type: "string";
546 }
547 function string(opts: NodeOptions): String;
548 function isString(node: any): node is String;
549
550 interface Universal extends Base {
551 type: "universal";
552 }
553 function universal(opts?: NamespaceOptions): Universal;
554 function isUniversal(node: any): node is Universal;
555}
556
\No newline at end of file