result.d.ts 4.2 KB

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