node.d.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. import AtRule = require('./at-rule.js')
  2. import { AtRuleProps } from './at-rule.js'
  3. import Comment, { CommentProps } from './comment.js'
  4. import Container, { NewChild } from './container.js'
  5. import CssSyntaxError from './css-syntax-error.js'
  6. import Declaration, { DeclarationProps } from './declaration.js'
  7. import Document from './document.js'
  8. import Input from './input.js'
  9. import { Stringifier, Syntax } from './postcss.js'
  10. import Result from './result.js'
  11. import Root from './root.js'
  12. import Rule, { RuleProps } from './rule.js'
  13. import Warning, { WarningOptions } from './warning.js'
  14. declare namespace Node {
  15. export type ChildNode = AtRule.default | Comment | Declaration | Rule
  16. export type AnyNode =
  17. | AtRule.default
  18. | Comment
  19. | Declaration
  20. | Document
  21. | Root
  22. | Rule
  23. export type ChildProps =
  24. | AtRuleProps
  25. | CommentProps
  26. | DeclarationProps
  27. | RuleProps
  28. export interface Position {
  29. /**
  30. * Source line in file. In contrast to `offset` it starts from 1.
  31. */
  32. column: number
  33. /**
  34. * Source column in file.
  35. */
  36. line: number
  37. /**
  38. * Source offset in file. It starts from 0.
  39. */
  40. offset: number
  41. }
  42. export interface Range {
  43. /**
  44. * End position, exclusive.
  45. */
  46. end: Position
  47. /**
  48. * Start position, inclusive.
  49. */
  50. start: Position
  51. }
  52. /**
  53. * Source represents an interface for the {@link Node.source} property.
  54. */
  55. export interface Source {
  56. /**
  57. * The inclusive ending position for the source
  58. * code of a node.
  59. */
  60. end?: Position
  61. /**
  62. * The source file from where a node has originated.
  63. */
  64. input: Input
  65. /**
  66. * The inclusive starting position for the source
  67. * code of a node.
  68. */
  69. start?: Position
  70. }
  71. /**
  72. * Interface represents an interface for an object received
  73. * as parameter by Node class constructor.
  74. */
  75. export interface NodeProps {
  76. source?: Source
  77. }
  78. export interface NodeErrorOptions {
  79. /**
  80. * An ending index inside a node's string that should be highlighted as
  81. * source of error.
  82. */
  83. endIndex?: number
  84. /**
  85. * An index inside a node's string that should be highlighted as source
  86. * of error.
  87. */
  88. index?: number
  89. /**
  90. * Plugin name that created this error. PostCSS will set it automatically.
  91. */
  92. plugin?: string
  93. /**
  94. * A word inside a node's string, that should be highlighted as source
  95. * of error.
  96. */
  97. word?: string
  98. }
  99. // eslint-disable-next-line @typescript-eslint/no-shadow
  100. class Node extends Node_ {}
  101. export { Node as default }
  102. }
  103. /**
  104. * It represents an abstract class that handles common
  105. * methods for other CSS abstract syntax tree nodes.
  106. *
  107. * Any node that represents CSS selector or value should
  108. * not extend the `Node` class.
  109. */
  110. declare abstract class Node_ {
  111. /**
  112. * It represents parent of the current node.
  113. *
  114. * ```js
  115. * root.nodes[0].parent === root //=> true
  116. * ```
  117. */
  118. parent: Container | Document | undefined
  119. /**
  120. * It represents unnecessary whitespace and characters present
  121. * in the css source code.
  122. *
  123. * Information to generate byte-to-byte equal node string as it was
  124. * in the origin input.
  125. *
  126. * The properties of the raws object are decided by parser,
  127. * the default parser uses the following properties:
  128. *
  129. * * `before`: the space symbols before the node. It also stores `*`
  130. * and `_` symbols before the declaration (IE hack).
  131. * * `after`: the space symbols after the last child of the node
  132. * to the end of the node.
  133. * * `between`: the symbols between the property and value
  134. * for declarations, selector and `{` for rules, or last parameter
  135. * and `{` for at-rules.
  136. * * `semicolon`: contains true if the last child has
  137. * an (optional) semicolon.
  138. * * `afterName`: the space between the at-rule name and its parameters.
  139. * * `left`: the space symbols between `/*` and the comment’s text.
  140. * * `right`: the space symbols between the comment’s text
  141. * and <code>*&#47;</code>.
  142. * - `important`: the content of the important statement,
  143. * if it is not just `!important`.
  144. *
  145. * PostCSS filters out the comments inside selectors, declaration values
  146. * and at-rule parameters but it stores the origin content in raws.
  147. *
  148. * ```js
  149. * const root = postcss.parse('a {\n color:black\n}')
  150. * root.first.first.raws //=> { before: '\n ', between: ':' }
  151. * ```
  152. */
  153. raws: any
  154. /**
  155. * It represents information related to origin of a node and is required
  156. * for generating source maps.
  157. *
  158. * The nodes that are created manually using the public APIs
  159. * provided by PostCSS will have `source` undefined and
  160. * will be absent in the source map.
  161. *
  162. * For this reason, the plugin developer should consider
  163. * duplicating nodes as the duplicate node will have the
  164. * same source as the original node by default or assign
  165. * source to a node created manually.
  166. *
  167. * ```js
  168. * decl.source.input.from //=> '/home/ai/source.css'
  169. * decl.source.start //=> { line: 10, column: 2 }
  170. * decl.source.end //=> { line: 10, column: 12 }
  171. * ```
  172. *
  173. * ```js
  174. * // Incorrect method, source not specified!
  175. * const prefixed = postcss.decl({
  176. * prop: '-moz-' + decl.prop,
  177. * value: decl.value
  178. * })
  179. *
  180. * // Correct method, source is inherited when duplicating.
  181. * const prefixed = decl.clone({
  182. * prop: '-moz-' + decl.prop
  183. * })
  184. * ```
  185. *
  186. * ```js
  187. * if (atrule.name === 'add-link') {
  188. * const rule = postcss.rule({
  189. * selector: 'a',
  190. * source: atrule.source
  191. * })
  192. *
  193. * atrule.parent.insertBefore(atrule, rule)
  194. * }
  195. * ```
  196. */
  197. source?: Node.Source
  198. /**
  199. * It represents type of a node in
  200. * an abstract syntax tree.
  201. *
  202. * A type of node helps in identification of a node
  203. * and perform operation based on it's type.
  204. *
  205. * ```js
  206. * const declaration = new Declaration({
  207. * prop: 'color',
  208. * value: 'black'
  209. * })
  210. *
  211. * declaration.type //=> 'decl'
  212. * ```
  213. */
  214. type: string
  215. constructor(defaults?: object)
  216. /**
  217. * Insert new node after current node to current node’s parent.
  218. *
  219. * Just alias for `node.parent.insertAfter(node, add)`.
  220. *
  221. * ```js
  222. * decl.after('color: black')
  223. * ```
  224. *
  225. * @param newNode New node.
  226. * @return This node for methods chain.
  227. */
  228. after(
  229. newNode: Node | Node.ChildProps | readonly Node[] | string | undefined
  230. ): this
  231. /**
  232. * It assigns properties to an existing node instance.
  233. *
  234. * ```js
  235. * decl.assign({ prop: 'word-wrap', value: 'break-word' })
  236. * ```
  237. *
  238. * @param overrides New properties to override the node.
  239. *
  240. * @return `this` for method chaining.
  241. */
  242. assign(overrides: object): this
  243. /**
  244. * Insert new node before current node to current node’s parent.
  245. *
  246. * Just alias for `node.parent.insertBefore(node, add)`.
  247. *
  248. * ```js
  249. * decl.before('content: ""')
  250. * ```
  251. *
  252. * @param newNode New node.
  253. * @return This node for methods chain.
  254. */
  255. before(
  256. newNode: Node | Node.ChildProps | readonly Node[] | string | undefined
  257. ): this
  258. /**
  259. * Clear the code style properties for the node and its children.
  260. *
  261. * ```js
  262. * node.raws.before //=> ' '
  263. * node.cleanRaws()
  264. * node.raws.before //=> undefined
  265. * ```
  266. *
  267. * @param keepBetween Keep the `raws.between` symbols.
  268. */
  269. cleanRaws(keepBetween?: boolean): void
  270. /**
  271. * It creates clone of an existing node, which includes all the properties
  272. * and their values, that includes `raws` but not `type`.
  273. *
  274. * ```js
  275. * decl.raws.before //=> "\n "
  276. * const cloned = decl.clone({ prop: '-moz-' + decl.prop })
  277. * cloned.raws.before //=> "\n "
  278. * cloned.toString() //=> -moz-transform: scale(0)
  279. * ```
  280. *
  281. * @param overrides New properties to override in the clone.
  282. *
  283. * @return Duplicate of the node instance.
  284. */
  285. clone(overrides?: object): this
  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. * Shortcut to clone the node and insert the resulting cloned node
  296. * before the current node.
  297. *
  298. * ```js
  299. * decl.cloneBefore({ prop: '-moz-' + decl.prop })
  300. * ```
  301. *
  302. * @param overrides Mew properties to override in the clone.
  303. *
  304. * @return New node
  305. */
  306. cloneBefore(overrides?: object): this
  307. /**
  308. * It creates an instance of the class `CssSyntaxError` and parameters passed
  309. * to this method are assigned to the error instance.
  310. *
  311. * The error instance will have description for the
  312. * error, original position of the node in the
  313. * source, showing line and column number.
  314. *
  315. * If any previous map is present, it would be used
  316. * to get original position of the source.
  317. *
  318. * The Previous Map here is referred to the source map
  319. * generated by previous compilation, example: Less,
  320. * Stylus and Sass.
  321. *
  322. * This method returns the error instance instead of
  323. * throwing it.
  324. *
  325. * ```js
  326. * if (!variables[name]) {
  327. * throw decl.error(`Unknown variable ${name}`, { word: name })
  328. * // CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
  329. * // color: $black
  330. * // a
  331. * // ^
  332. * // background: white
  333. * }
  334. * ```
  335. *
  336. * @param message Description for the error instance.
  337. * @param options Options for the error instance.
  338. *
  339. * @return Error instance is returned.
  340. */
  341. error(message: string, options?: Node.NodeErrorOptions): CssSyntaxError
  342. /**
  343. * Returns the next child of the node’s parent.
  344. * Returns `undefined` if the current node is the last child.
  345. *
  346. * ```js
  347. * if (comment.text === 'delete next') {
  348. * const next = comment.next()
  349. * if (next) {
  350. * next.remove()
  351. * }
  352. * }
  353. * ```
  354. *
  355. * @return Next node.
  356. */
  357. next(): Node.ChildNode | undefined
  358. /**
  359. * Get the position for a word or an index inside the node.
  360. *
  361. * @param opts Options.
  362. * @return Position.
  363. */
  364. positionBy(opts?: Pick<WarningOptions, 'index' | 'word'>): Node.Position
  365. /**
  366. * Convert string index to line/column.
  367. *
  368. * @param index The symbol number in the node’s string.
  369. * @return Symbol position in file.
  370. */
  371. positionInside(index: number): Node.Position
  372. /**
  373. * Returns the previous child of the node’s parent.
  374. * Returns `undefined` if the current node is the first child.
  375. *
  376. * ```js
  377. * const annotation = decl.prev()
  378. * if (annotation.type === 'comment') {
  379. * readAnnotation(annotation.text)
  380. * }
  381. * ```
  382. *
  383. * @return Previous node.
  384. */
  385. prev(): Node.ChildNode | undefined
  386. /**
  387. * Get the range for a word or start and end index inside the node.
  388. * The start index is inclusive; the end index is exclusive.
  389. *
  390. * @param opts Options.
  391. * @return Range.
  392. */
  393. rangeBy(
  394. opts?: Pick<WarningOptions, 'endIndex' | 'index' | 'word'>
  395. ): Node.Range
  396. /**
  397. * Returns a `raws` value. If the node is missing
  398. * the code style property (because the node was manually built or cloned),
  399. * PostCSS will try to autodetect the code style property by looking
  400. * at other nodes in the tree.
  401. *
  402. * ```js
  403. * const root = postcss.parse('a { background: white }')
  404. * root.nodes[0].append({ prop: 'color', value: 'black' })
  405. * root.nodes[0].nodes[1].raws.before //=> undefined
  406. * root.nodes[0].nodes[1].raw('before') //=> ' '
  407. * ```
  408. *
  409. * @param prop Name of code style property.
  410. * @param defaultType Name of default value, it can be missed
  411. * if the value is the same as prop.
  412. * @return {string} Code style value.
  413. */
  414. raw(prop: string, defaultType?: string): string
  415. /**
  416. * It removes the node from its parent and deletes its parent property.
  417. *
  418. * ```js
  419. * if (decl.prop.match(/^-webkit-/)) {
  420. * decl.remove()
  421. * }
  422. * ```
  423. *
  424. * @return `this` for method chaining.
  425. */
  426. remove(): this
  427. /**
  428. * Inserts node(s) before the current node and removes the current node.
  429. *
  430. * ```js
  431. * AtRule: {
  432. * mixin: atrule => {
  433. * atrule.replaceWith(mixinRules[atrule.params])
  434. * }
  435. * }
  436. * ```
  437. *
  438. * @param nodes Mode(s) to replace current one.
  439. * @return Current node to methods chain.
  440. */
  441. replaceWith(...nodes: NewChild[]): this
  442. /**
  443. * Finds the Root instance of the node’s tree.
  444. *
  445. * ```js
  446. * root.nodes[0].nodes[0].root() === root
  447. * ```
  448. *
  449. * @return Root parent.
  450. */
  451. root(): Root
  452. /**
  453. * Fix circular links on `JSON.stringify()`.
  454. *
  455. * @return Cleaned object.
  456. */
  457. toJSON(): object
  458. /**
  459. * It compiles the node to browser readable cascading style sheets string
  460. * depending on it's type.
  461. *
  462. * ```js
  463. * new Rule({ selector: 'a' }).toString() //=> "a {}"
  464. * ```
  465. *
  466. * @param stringifier A syntax to use in string generation.
  467. * @return CSS string of this node.
  468. */
  469. toString(stringifier?: Stringifier | Syntax): string
  470. /**
  471. * It is a wrapper for {@link Result#warn}, providing convenient
  472. * way of generating warnings.
  473. *
  474. * ```js
  475. * Declaration: {
  476. * bad: (decl, { result }) => {
  477. * decl.warn(result, 'Deprecated property: bad')
  478. * }
  479. * }
  480. * ```
  481. *
  482. * @param result The `Result` instance that will receive the warning.
  483. * @param message Description for the warning.
  484. * @param options Options for the warning.
  485. *
  486. * @return `Warning` instance is returned
  487. */
  488. warn(result: Result, message: string, options?: WarningOptions): Warning
  489. /**
  490. * If this node isn't already dirty, marks it and its ancestors as such. This
  491. * indicates to the LazyResult processor that the {@link Root} has been
  492. * modified by the current plugin and may need to be processed again by other
  493. * plugins.
  494. */
  495. protected markDirty(): void
  496. }
  497. declare class Node extends Node_ {}
  498. export = Node