UNPKG

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