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. * If this node isn't already dirty, marks it and its ancestors as such. This
  218. * indicates to the LazyResult processor that the {@link Root} has been
  219. * modified by the current plugin and may need to be processed again by other
  220. * plugins.
  221. */
  222. protected markDirty(): void
  223. /**
  224. * Insert new node after current node to current node’s parent.
  225. *
  226. * Just alias for `node.parent.insertAfter(node, add)`.
  227. *
  228. * ```js
  229. * decl.after('color: black')
  230. * ```
  231. *
  232. * @param newNode New node.
  233. * @return This node for methods chain.
  234. */
  235. after(
  236. newNode: Node | Node.ChildProps | readonly Node[] | string | undefined
  237. ): this
  238. /**
  239. * It assigns properties to an existing node instance.
  240. *
  241. * ```js
  242. * decl.assign({ prop: 'word-wrap', value: 'break-word' })
  243. * ```
  244. *
  245. * @param overrides New properties to override the node.
  246. *
  247. * @return `this` for method chaining.
  248. */
  249. assign(overrides: object): this
  250. /**
  251. * Insert new node before current node to current node’s parent.
  252. *
  253. * Just alias for `node.parent.insertBefore(node, add)`.
  254. *
  255. * ```js
  256. * decl.before('content: ""')
  257. * ```
  258. *
  259. * @param newNode New node.
  260. * @return This node for methods chain.
  261. */
  262. before(
  263. newNode: Node | Node.ChildProps | readonly Node[] | string | undefined
  264. ): this
  265. /**
  266. * Clear the code style properties for the node and its children.
  267. *
  268. * ```js
  269. * node.raws.before //=> ' '
  270. * node.cleanRaws()
  271. * node.raws.before //=> undefined
  272. * ```
  273. *
  274. * @param keepBetween Keep the `raws.between` symbols.
  275. */
  276. cleanRaws(keepBetween?: boolean): void
  277. /**
  278. * It creates clone of an existing node, which includes all the properties
  279. * and their values, that includes `raws` but not `type`.
  280. *
  281. * ```js
  282. * decl.raws.before //=> "\n "
  283. * const cloned = decl.clone({ prop: '-moz-' + decl.prop })
  284. * cloned.raws.before //=> "\n "
  285. * cloned.toString() //=> -moz-transform: scale(0)
  286. * ```
  287. *
  288. * @param overrides New properties to override in the clone.
  289. *
  290. * @return Duplicate of the node instance.
  291. */
  292. clone(overrides?: object): this
  293. /**
  294. * Shortcut to clone the node and insert the resulting cloned node
  295. * after the current node.
  296. *
  297. * @param overrides New properties to override in the clone.
  298. * @return New node.
  299. */
  300. cloneAfter(overrides?: object): this
  301. /**
  302. * Shortcut to clone the node and insert the resulting cloned node
  303. * before the current node.
  304. *
  305. * ```js
  306. * decl.cloneBefore({ prop: '-moz-' + decl.prop })
  307. * ```
  308. *
  309. * @param overrides Mew properties to override in the clone.
  310. *
  311. * @return New node
  312. */
  313. cloneBefore(overrides?: object): this
  314. /**
  315. * It creates an instance of the class `CssSyntaxError` and parameters passed
  316. * to this method are assigned to the error instance.
  317. *
  318. * The error instance will have description for the
  319. * error, original position of the node in the
  320. * source, showing line and column number.
  321. *
  322. * If any previous map is present, it would be used
  323. * to get original position of the source.
  324. *
  325. * The Previous Map here is referred to the source map
  326. * generated by previous compilation, example: Less,
  327. * Stylus and Sass.
  328. *
  329. * This method returns the error instance instead of
  330. * throwing it.
  331. *
  332. * ```js
  333. * if (!variables[name]) {
  334. * throw decl.error(`Unknown variable ${name}`, { word: name })
  335. * // CssSyntaxError: postcss-vars:a.sass:4:3: Unknown variable $black
  336. * // color: $black
  337. * // a
  338. * // ^
  339. * // background: white
  340. * }
  341. * ```
  342. *
  343. * @param message Description for the error instance.
  344. * @param options Options for the error instance.
  345. *
  346. * @return Error instance is returned.
  347. */
  348. error(message: string, options?: Node.NodeErrorOptions): CssSyntaxError
  349. /**
  350. * Returns the next child of the node’s parent.
  351. * Returns `undefined` if the current node is the last child.
  352. *
  353. * ```js
  354. * if (comment.text === 'delete next') {
  355. * const next = comment.next()
  356. * if (next) {
  357. * next.remove()
  358. * }
  359. * }
  360. * ```
  361. *
  362. * @return Next node.
  363. */
  364. next(): Node.ChildNode | undefined
  365. /**
  366. * Get the position for a word or an index inside the node.
  367. *
  368. * @param opts Options.
  369. * @return Position.
  370. */
  371. positionBy(opts?: Pick<WarningOptions, 'index' | 'word'>): Node.Position
  372. /**
  373. * Convert string index to line/column.
  374. *
  375. * @param index The symbol number in the node’s string.
  376. * @return Symbol position in file.
  377. */
  378. positionInside(index: number): Node.Position
  379. /**
  380. * Returns the previous child of the node’s parent.
  381. * Returns `undefined` if the current node is the first child.
  382. *
  383. * ```js
  384. * const annotation = decl.prev()
  385. * if (annotation.type === 'comment') {
  386. * readAnnotation(annotation.text)
  387. * }
  388. * ```
  389. *
  390. * @return Previous node.
  391. */
  392. prev(): Node.ChildNode | undefined
  393. /**
  394. * Get the range for a word or start and end index inside the node.
  395. * The start index is inclusive; the end index is exclusive.
  396. *
  397. * @param opts Options.
  398. * @return Range.
  399. */
  400. rangeBy(
  401. opts?: Pick<WarningOptions, 'endIndex' | 'index' | 'word'>
  402. ): Node.Range
  403. /**
  404. * Returns a `raws` value. If the node is missing
  405. * the code style property (because the node was manually built or cloned),
  406. * PostCSS will try to autodetect the code style property by looking
  407. * at other nodes in the tree.
  408. *
  409. * ```js
  410. * const root = postcss.parse('a { background: white }')
  411. * root.nodes[0].append({ prop: 'color', value: 'black' })
  412. * root.nodes[0].nodes[1].raws.before //=> undefined
  413. * root.nodes[0].nodes[1].raw('before') //=> ' '
  414. * ```
  415. *
  416. * @param prop Name of code style property.
  417. * @param defaultType Name of default value, it can be missed
  418. * if the value is the same as prop.
  419. * @return {string} Code style value.
  420. */
  421. raw(prop: string, defaultType?: string): string
  422. /**
  423. * It removes the node from its parent and deletes its parent property.
  424. *
  425. * ```js
  426. * if (decl.prop.match(/^-webkit-/)) {
  427. * decl.remove()
  428. * }
  429. * ```
  430. *
  431. * @return `this` for method chaining.
  432. */
  433. remove(): this
  434. /**
  435. * Inserts node(s) before the current node and removes the current node.
  436. *
  437. * ```js
  438. * AtRule: {
  439. * mixin: atrule => {
  440. * atrule.replaceWith(mixinRules[atrule.params])
  441. * }
  442. * }
  443. * ```
  444. *
  445. * @param nodes Mode(s) to replace current one.
  446. * @return Current node to methods chain.
  447. */
  448. replaceWith(...nodes: NewChild[]): this
  449. /**
  450. * Finds the Root instance of the node’s tree.
  451. *
  452. * ```js
  453. * root.nodes[0].nodes[0].root() === root
  454. * ```
  455. *
  456. * @return Root parent.
  457. */
  458. root(): Root
  459. /**
  460. * Fix circular links on `JSON.stringify()`.
  461. *
  462. * @return Cleaned object.
  463. */
  464. toJSON(): object
  465. /**
  466. * It compiles the node to browser readable cascading style sheets string
  467. * depending on it's type.
  468. *
  469. * ```js
  470. * new Rule({ selector: 'a' }).toString() //=> "a {}"
  471. * ```
  472. *
  473. * @param stringifier A syntax to use in string generation.
  474. * @return CSS string of this node.
  475. */
  476. toString(stringifier?: Stringifier | Syntax): string
  477. /**
  478. * It is a wrapper for {@link Result#warn}, providing convenient
  479. * way of generating warnings.
  480. *
  481. * ```js
  482. * Declaration: {
  483. * bad: (decl, { result }) => {
  484. * decl.warn(result, 'Deprecated property: bad')
  485. * }
  486. * }
  487. * ```
  488. *
  489. * @param result The `Result` instance that will receive the warning.
  490. * @param message Description for the warning.
  491. * @param options Options for the warning.
  492. *
  493. * @return `Warning` instance is returned
  494. */
  495. warn(result: Result, message: string, options?: WarningOptions): Warning
  496. }
  497. declare class Node extends Node_ {}
  498. export = Node