|
| 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 | + type: "comment"; |
| 135 | + parent: Container; |
| 136 | + inline: boolean; |
| 137 | + text: string; |
| 138 | +} |
| 139 | + |
| 140 | +export interface Func extends ContainerBase { |
| 141 | + type: "func"; |
| 142 | + parent: Container; |
| 143 | + isColor: boolean; |
| 144 | + name: string; |
| 145 | + params: string; |
| 146 | +} |
| 147 | + |
| 148 | +export interface Interpolation extends ContainerBase { |
| 149 | + type: "interpolation"; |
| 150 | + parent: Container; |
| 151 | + params: string; |
| 152 | + prefix: string; |
| 153 | +} |
| 154 | + |
| 155 | +export interface Numeric extends NodeBase { |
| 156 | + type: "numeric"; |
| 157 | + parent: Container; |
| 158 | + unit: string; |
| 159 | + value: string; |
| 160 | +} |
| 161 | + |
| 162 | +export interface Operator extends NodeBase { |
| 163 | + type: "operator"; |
| 164 | + parent: Container; |
| 165 | + value: string; |
| 166 | +} |
| 167 | + |
| 168 | +export interface Punctuation extends NodeBase { |
| 169 | + type: "punctuation"; |
| 170 | + parent: Container; |
| 171 | + value: string; |
| 172 | +} |
| 173 | + |
| 174 | +export interface Quoted extends NodeBase { |
| 175 | + type: "quoted"; |
| 176 | + parent: Container; |
| 177 | + quote: string; |
| 178 | + value: string; |
| 179 | +} |
| 180 | + |
| 181 | +export interface UnicodeRange extends NodeBase { |
| 182 | + type: "unicodeRange"; |
| 183 | + parent: Container; |
| 184 | + name: string; |
| 185 | +} |
| 186 | + |
| 187 | +export interface Word extends NodeBase { |
| 188 | + type: "word"; |
| 189 | + parent: Container; |
| 190 | + isColor: boolean; |
| 191 | + isHex: boolean; |
| 192 | + isUrl: boolean; |
| 193 | + isVariable: boolean; |
| 194 | + value: string; |
| 195 | +} |
| 196 | + |
| 197 | +export function parse(css: string, options?: ParseOptions): Root; |
| 198 | + |
| 199 | +export interface ParseOptions { |
| 200 | + ignoreUnknownWords?: boolean; |
| 201 | + interpolation?: boolean | InterpolationOptions; |
| 202 | + variables?: VariablesOptions; |
| 203 | +} |
| 204 | + |
| 205 | +export interface InterpolationOptions { |
| 206 | + prefix: string; |
| 207 | +} |
| 208 | + |
| 209 | +export interface VariablesOptions { |
| 210 | + prefixes: string[]; |
| 211 | +} |
| 212 | + |
| 213 | +export const stringify: postcss.Stringifier; |
| 214 | + |
| 215 | +export function nodeToString(node: Node): string; |
0 commit comments