result.d.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import {
  2. Document,
  3. Node,
  4. Plugin,
  5. ProcessOptions,
  6. Root,
  7. SourceMap,
  8. TransformCallback,
  9. Warning,
  10. WarningOptions
  11. } from './postcss.js'
  12. import Processor from './processor.js'
  13. declare namespace Result {
  14. export interface Message {
  15. [others: string]: any
  16. /**
  17. * Source PostCSS plugin name.
  18. */
  19. plugin?: string
  20. /**
  21. * Message type.
  22. */
  23. type: string
  24. }
  25. export interface ResultOptions extends ProcessOptions {
  26. /**
  27. * The CSS node that was the source of the warning.
  28. */
  29. node?: Node
  30. /**
  31. * Name of plugin that created this warning. `Result#warn` will fill it
  32. * automatically with `Plugin#postcssPlugin` value.
  33. */
  34. plugin?: string
  35. }
  36. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  37. export { Result_ as default }
  38. }
  39. /**
  40. * Provides the result of the PostCSS transformations.
  41. *
  42. * A Result instance is returned by `LazyResult#then`
  43. * or `Root#toResult` methods.
  44. *
  45. * ```js
  46. * postcss([autoprefixer]).process(css).then(result => {
  47. * console.log(result.css)
  48. * })
  49. * ```
  50. *
  51. * ```js
  52. * const result2 = postcss.parse(css).toResult()
  53. * ```
  54. */
  55. declare class Result_ {
  56. /**
  57. * A CSS string representing of `Result#root`.
  58. *
  59. * ```js
  60. * postcss.parse('a{}').toResult().css //=> "a{}"
  61. * ```
  62. */
  63. css: string
  64. /**
  65. * Last runned PostCSS plugin.
  66. */
  67. lastPlugin: Plugin | TransformCallback
  68. /**
  69. * An instance of `SourceMapGenerator` class from the `source-map` library,
  70. * representing changes to the `Result#root` instance.
  71. *
  72. * ```js
  73. * result.map.toJSON() //=> { version: 3, file: 'a.css', … }
  74. * ```
  75. *
  76. * ```js
  77. * if (result.map) {
  78. * fs.writeFileSync(result.opts.to + '.map', result.map.toString())
  79. * }
  80. * ```
  81. */
  82. map: SourceMap
  83. /**
  84. * Contains messages from plugins (e.g., warnings or custom messages).
  85. * Each message should have type and plugin properties.
  86. *
  87. * ```js
  88. * AtRule: {
  89. * import: (atRule, { result }) {
  90. * const importedFile = parseImport(atRule)
  91. * result.messages.push({
  92. * type: 'dependency',
  93. * plugin: 'postcss-import',
  94. * file: importedFile,
  95. * parent: result.opts.from
  96. * })
  97. * }
  98. * }
  99. * ```
  100. */
  101. messages: Result.Message[]
  102. /**
  103. * Options from the `Processor#process` or `Root#toResult` call
  104. * that produced this Result instance.]
  105. *
  106. * ```js
  107. * root.toResult(opts).opts === opts
  108. * ```
  109. */
  110. opts: Result.ResultOptions
  111. /**
  112. * The Processor instance used for this transformation.
  113. *
  114. * ```js
  115. * for (const plugin of result.processor.plugins) {
  116. * if (plugin.postcssPlugin === 'postcss-bad') {
  117. * throw 'postcss-good is incompatible with postcss-bad'
  118. * }
  119. * })
  120. * ```
  121. */
  122. processor: Processor
  123. /**
  124. * Root node after all transformations.
  125. *
  126. * ```js
  127. * root.toResult().root === root
  128. * ```
  129. */
  130. root: Document | Root
  131. /**
  132. * @param processor Processor used for this transformation.
  133. * @param root Root node after all transformations.
  134. * @param opts Options from the `Processor#process` or `Root#toResult`.
  135. */
  136. constructor(processor: Processor, root: Document | Root, opts: Result.ResultOptions)
  137. /**
  138. * An alias for the `Result#css` property.
  139. * Use it with syntaxes that generate non-CSS output.
  140. *
  141. * ```js
  142. * result.css === result.content
  143. * ```
  144. */
  145. get content(): string
  146. /**
  147. * Returns for `Result#css` content.
  148. *
  149. * ```js
  150. * result + '' === result.css
  151. * ```
  152. *
  153. * @return String representing of `Result#root`.
  154. */
  155. toString(): string
  156. /**
  157. * Creates an instance of `Warning` and adds it to `Result#messages`.
  158. *
  159. * ```js
  160. * if (decl.important) {
  161. * result.warn('Avoid !important', { node: decl, word: '!important' })
  162. * }
  163. * ```
  164. *
  165. * @param text Warning message.
  166. * @param opts Warning options.
  167. * @return Created warning.
  168. */
  169. warn(message: string, options?: WarningOptions): Warning
  170. /**
  171. * Returns warnings from plugins. Filters `Warning` instances
  172. * from `Result#messages`.
  173. *
  174. * ```js
  175. * result.warnings().forEach(warn => {
  176. * console.warn(warn.toString())
  177. * })
  178. * ```
  179. *
  180. * @return Warnings from plugins.
  181. */
  182. warnings(): Warning[]
  183. }
  184. declare class Result extends Result_ {}
  185. export = Result