UNPKG

11.3 kBTypeScriptView Raw
1import Declaration, { DeclarationProps } from './declaration.js'
2import Comment, { CommentProps } from './comment.js'
3import { Stringifier, Syntax } from './postcss.js'
4import AtRule, { AtRuleProps } from './at-rule.js'
5import Rule, { RuleProps } from './rule.js'
6import { WarningOptions } from './warning.js'
7import CssSyntaxError from './css-syntax-error.js'
8import Container from './container.js'
9import Result from './result.js'
10import Input from './input.js'
11import Root from './root.js'
12
13export type ChildNode = AtRule | Rule | Declaration | Comment
14
15export type AnyNode = AtRule | Rule | Declaration | Comment | Root
16
17export type ChildProps =
18 | AtRuleProps
19 | RuleProps
20 | DeclarationProps
21 | CommentProps
22
23export interface Position {
24 /**
25 * Source offset in file. It starts from 0.
26 */
27 offset: number
28
29 /**
30 * Source line in file. In contrast to `offset` it starts from 1.
31 */
32 column: number
33
34 /**
35 * Source column in file.
36 */
37 line: number
38}
39
40export interface Source {
41 /**
42 * The file source of the node.
43 */
44 input: Input
45 /**
46 * The starting position of the node’s source.
47 */
48 start?: Position
49 /**
50 * The ending position of the node's source.
51 */
52 end?: Position
53}
54
55export interface NodeProps {
56 source?: Source
57}
58
59interface NodeErrorOptions {
60 /**
61 * Plugin name that created this error. PostCSS will set it automatically.
62 */
63 plugin?: string
64 /**
65 * A word inside a node's string, that should be highlighted as source
66 * of error.
67 */
68 word?: string
69 /**
70 * An index inside a node's string that should be highlighted as source
71 * of error.
72 */
73 index?: number
74}
75
76/**
77 * All node classes inherit the following common methods.
78 *
79 * You should not extend this classes to create AST for selector or value
80 * parser.
81 */
82export default abstract class Node {
83 /**
84 * tring representing the node’s type. Possible values are `root`, `atrule`,
85 * `rule`, `decl`, or `comment`.
86 *
87 * ```js
88 * new Declaration({ prop: 'color', value: 'black' }).type //=> 'decl'
89 * ```
90 */
91 type: string
92
93 /**
94 * The node’s parent node.
95 *
96 * ```js
97 * root.nodes[0].parent === root
98 * ```
99 */
100 parent: Container | undefined
101
102 /**
103 * The input source of the node.
104 *
105 * The property is used in source map generation.
106 *
107 * If you create a node manually (e.g., with `postcss.decl()`),
108 * that node will not have a `source` property and will be absent
109 * from the source map. For this reason, the plugin developer should
110 * consider cloning nodes to create new ones (in which case the new node’s
111 * source will reference the original, cloned node) or setting
112 * the `source` property manually.
113 *
114 * ```js
115 * decl.source.input.from //=> '/home/ai/a.sass'
116 * decl.source.start //=> { line: 10, column: 2 }
117 * decl.source.end //=> { line: 10, column: 12 }
118 * ```
119 *
120 * ```js
121 * // Bad
122 * const prefixed = postcss.decl({
123 * prop: '-moz-' + decl.prop,
124 * value: decl.value
125 * })
126 *
127 * // Good
128 * const prefixed = decl.clone({ prop: '-moz-' + decl.prop })
129 * ```
130 *
131 * ```js
132 * if (atrule.name === 'add-link') {
133 * const rule = postcss.rule({ selector: 'a', source: atrule.source })
134 * atrule.parent.insertBefore(atrule, rule)
135 * }
136 * ```
137 */
138 source?: Source
139
140 /**
141 * Information to generate byte-to-byte equal node string as it was
142 * in the origin input.
143 *
144 * Every parser saves its own properties,
145 * but the default CSS parser uses:
146 *
147 * * `before`: the space symbols before the node. It also stores `*`
148 * and `_` symbols before the declaration (IE hack).
149 * * `after`: the space symbols after the last child of the node
150 * to the end of the node.
151 * * `between`: the symbols between the property and value
152 * for declarations, selector and `{` for rules, or last parameter
153 * and `{` for at-rules.
154 * * `semicolon`: contains true if the last child has
155 * an (optional) semicolon.
156 * * `afterName`: the space between the at-rule name and its parameters.
157 * * `left`: the space symbols between `/*` and the comment’s text.
158 * * `right`: the space symbols between the comment’s text
159 * and <code>*&#47;</code>.
160 * * `important`: the content of the important statement,
161 * if it is not just `!important`.
162 *
163 * PostCSS cleans selectors, declaration values and at-rule parameters
164 * from comments and extra spaces, but it stores origin content in raws
165 * properties. As such, if you don’t change a declaration’s value,
166 * PostCSS will use the raw value with comments.
167 *
168 * ```js
169 * const root = postcss.parse('a {\n color:black\n}')
170 * root.first.first.raws //=> { before: '\n ', between: ':' }
171 * ```
172 */
173 raws: any
174
175 /**
176 * @param defaults Value for node properties.
177 */
178 constructor (defaults?: object)
179
180 /**
181 * Returns a `CssSyntaxError` instance containing the original position
182 * of the node in the source, showing line and column numbers and also
183 * a small excerpt to facilitate debugging.
184 *
185 * If present, an input source map will be used to get the original position
186 * of the source, even from a previous compilation step
187 * (e.g., from Sass compilation).
188 *
189 * This method produces very useful error messages.
190 *
191 * ```js
192 * if (!variables[name]) {
193 * throw decl.error(`Unknown variable ${name}`, { word: name })
194 * // CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
195 * // color: $black
196 * // a
197 * // ^
198 * // background: white
199 * }
200 * ```
201 *
202 * @param message Error description.
203 * @param opts Options.
204 *
205 * @return Error object to throw it.
206 */
207 error (message: string, options?: NodeErrorOptions): CssSyntaxError
208
209 /**
210 * This method is provided as a convenience wrapper for `Result#warn`.
211 *
212 * ```js
213 * Declaration: {
214 * bad: (decl, { result }) => {
215 * decl.warn(result, 'Deprecated property bad')
216 * }
217 * }
218 * ```
219 *
220 * @param result The `Result` instance that will receive the warning.
221 * @param text Warning message.
222 * @param opts Warning Options.
223 *
224 * @return Created warning object.
225 */
226 warn (result: Result, text: string, opts?: WarningOptions): void
227
228 /**
229 * Removes the node from its parent and cleans the parent properties
230 * from the node and its children.
231 *
232 * ```js
233 * if (decl.prop.match(/^-webkit-/)) {
234 * decl.remove()
235 * }
236 * ```
237 *
238 * @return Node to make calls chain.
239 */
240 remove (): this
241
242 /**
243 * Returns a CSS string representing the node.
244 *
245 * ```js
246 * new Rule({ selector: 'a' }).toString() //=> "a {}"
247 * ```
248 *
249 * @param stringifier A syntax to use in string generation.
250 * @return CSS string of this node.
251 */
252 toString (stringifier?: Stringifier | Syntax): string
253
254 /**
255 * Returns an exact clone of the node.
256 *
257 * The resulting cloned node and its (cloned) children will retain
258 * code style properties.
259 *
260 * ```js
261 * decl.raws.before //=> "\n "
262 * const cloned = decl.clone({ prop: '-moz-' + decl.prop })
263 * cloned.raws.before //=> "\n "
264 * cloned.toString() //=> -moz-transform: scale(0)
265 * ```
266 *
267 * @param overrides New properties to override in the clone.
268 * @return Clone of the node.
269 */
270 clone (overrides?: object): this
271
272 /**
273 * Shortcut to clone the node and insert the resulting cloned node
274 * before the current node.
275 *
276 * ```js
277 * decl.cloneBefore({ prop: '-moz-' + decl.prop })
278 * ```
279 *
280 * @param overrides Mew properties to override in the clone.
281 *
282 * @return New node
283 */
284 cloneBefore (overrides?: object): this
285
286 /**
287 * Shortcut to clone the node and insert the resulting cloned node
288 * after the current node.
289 *
290 * @param overrides New properties to override in the clone.
291 * @return New node.
292 */
293 cloneAfter (overrides?: object): this
294
295 /**
296 * Inserts node(s) before the current node and removes the current node.
297 *
298 * ```js
299 * AtRule: {
300 * mixin: atrule => {
301 * atrule.replaceWith(mixinRules[atrule.params])
302 * }
303 * }
304 * ```
305 *
306 * @param nodes Mode(s) to replace current one.
307 * @return Current node to methods chain.
308 */
309 replaceWith (
310 ...nodes: (ChildNode | ChildProps | ChildNode[] | ChildProps[])[]
311 ): this
312
313 /**
314 * Returns the next child of the node’s parent.
315 * Returns `undefined` if the current node is the last child.
316 *
317 * ```js
318 * if (comment.text === 'delete next') {
319 * const next = comment.next()
320 * if (next) {
321 * next.remove()
322 * }
323 * }
324 * ```
325 *
326 * @return Next node.
327 */
328 next (): ChildNode | undefined
329
330 /**
331 * Returns the previous child of the node’s parent.
332 * Returns `undefined` if the current node is the first child.
333 *
334 * ```js
335 * const annotation = decl.prev()
336 * if (annotation.type === 'comment') {
337 * readAnnotation(annotation.text)
338 * }
339 * ```
340 *
341 * @return Previous node.
342 */
343 prev (): ChildNode | undefined
344
345 /**
346 * Insert new node before current node to current node’s parent.
347 *
348 * Just alias for `node.parent.insertBefore(node, add)`.
349 *
350 * ```js
351 * decl.before('content: ""')
352 * ```
353 *
354 * @param newNode New node.
355 * @return This node for methods chain.
356 */
357 before (newNode: Node | ChildProps | string | Node[]): this
358
359 /**
360 * Insert new node after current node to current node’s parent.
361 *
362 * Just alias for `node.parent.insertAfter(node, add)`.
363 *
364 * ```js
365 * decl.after('color: black')
366 * ```
367 *
368 * @param newNode New node.
369 * @return This node for methods chain.
370 */
371 after (newNode: Node | ChildProps | string | Node[]): this
372
373 /**
374 * Finds the Root instance of the node’s tree.
375 *
376 * ```js
377 * root.nodes[0].nodes[0].root() === root
378 * ```
379 *
380 * @return Root parent.
381 */
382 root (): Root
383
384 /**
385 * Returns a `Node#raws` value. If the node is missing
386 * the code style property (because the node was manually built or cloned),
387 * PostCSS will try to autodetect the code style property by looking
388 * at other nodes in the tree.
389 *
390 * ```js
391 * const root = postcss.parse('a { background: white }')
392 * root.nodes[0].append({ prop: 'color', value: 'black' })
393 * root.nodes[0].nodes[1].raws.before //=> undefined
394 * root.nodes[0].nodes[1].raw('before') //=> ' '
395 * ```
396 *
397 * @param prop Name of code style property.
398 * @param defaultType Name of default value, it can be missed
399 * if the value is the same as prop.
400 * @return {string} Code style value.
401 */
402 raw (prop: string, defaultType?: string): string
403
404 /**
405 * Clear the code style properties for the node and its children.
406 *
407 * ```js
408 * node.raws.before //=> ' '
409 * node.cleanRaws()
410 * node.raws.before //=> undefined
411 * ```
412 *
413 * @param keepBetween Keep the `raws.between` symbols.
414 */
415 cleanRaws (keepBetween?: boolean): void
416
417 /**
418 * Fix circular links on `JSON.stringify()`.
419 *
420 * @return Cleaned object.
421 */
422 toJSON (): object
423
424 /**
425 * Convert string index to line/column.
426 *
427 * @param index The symbol number in the node’s string.
428 * @return Symbol position in file.
429 */
430 positionInside (index: number): Position
431}
432
\No newline at end of file