Skip to content

Commit

Permalink
Merge pull request #1463 from ludofischer/fix-lazyresult-type
Browse files Browse the repository at this point in the history
fix(typescript): ensure LazyResult complies with Promise interface.
  • Loading branch information
ai committed Nov 19, 2020
2 parents f8fe285 + 362ac8d commit 0d89106
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/lazy-result.d.ts
Expand Up @@ -13,7 +13,7 @@ import Root from './root.js'
* const lazy = postcss([autoprefixer]).process(css)
* ```
*/
export default class LazyResult {
export default class LazyResult implements Promise<Result> {
/**
* Processes input CSS through synchronous and asynchronous plugins
* and calls `onFulfilled` with a Result instance. If a plugin throws
Expand Down Expand Up @@ -66,6 +66,12 @@ export default class LazyResult {
*/
constructor (processor: Processor, css: string, opts: ResultOptions)

/**
* Returns the default string description of an object.
* Required to implement the Promise interface.
*/
get [Symbol.toStringTag] (): string

/**
* Returns a `Processor` instance, which will be used
* for CSS transformations.
Expand Down
4 changes: 4 additions & 0 deletions lib/lazy-result.js
Expand Up @@ -114,6 +114,10 @@ class LazyResult {
})
}

get [Symbol.toStringTag] () {
return 'LazyResult'
}

get processor () {
return this.result.processor
}
Expand Down
5 changes: 5 additions & 0 deletions test/lazy-result.test.ts
Expand Up @@ -57,3 +57,8 @@ it('executes on finally callback', () => {
.finally(mockCallback)
.then(() => expect(mockCallback).toHaveBeenCalledTimes(1))
})

it('prints its object type', () => {
let result = new LazyResult(processor, 'a {}', {})
expect(Object.prototype.toString.call(result)).toEqual('[object LazyResult]')
})
17 changes: 16 additions & 1 deletion test/types.ts
@@ -1,4 +1,4 @@
import { PluginCreator } from '../lib/postcss.js'
import postcss, { Result, PluginCreator, SourceMap } from '../lib/postcss.js'

const plugin: PluginCreator<string> = prop => {
return {
Expand All @@ -14,4 +14,19 @@ const plugin: PluginCreator<string> = prop => {

plugin.postcss = true

interface StyleCompileResults {
code: string
map: SourceMap | undefined
}

const processResult: Promise<Result> | Result = postcss([
plugin
]).process('h1{color: black;}', { from: undefined })
const processed:
| StyleCompileResults
| Promise<StyleCompileResults> = processResult.then(result => ({
code: result.css,
map: result.map
}))

export default plugin

0 comments on commit 0d89106

Please sign in to comment.