Skip to content

Commit

Permalink
Cast Result.root type from process() options
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Aug 14, 2023
1 parent 0ef29c2 commit 7d51976
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 33 deletions.
21 changes: 13 additions & 8 deletions lib/lazy-result.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Document from './document.js'
import { SourceMap } from './postcss.js'
import Processor from './processor.js'
import Result, { Message, ResultOptions } from './result.js'
Expand All @@ -18,7 +19,9 @@ declare namespace LazyResult {
* const lazy = postcss([autoprefixer]).process(css)
* ```
*/
declare class LazyResult_ implements PromiseLike<Result> {
declare class LazyResult_<RootNode = Document | Root>
implements PromiseLike<Result<RootNode>>
{
/**
* Processes input CSS through synchronous and asynchronous plugins
* and calls onRejected for each error thrown in any plugin.
Expand All @@ -33,7 +36,7 @@ declare class LazyResult_ implements PromiseLike<Result> {
* })
* ```
*/
catch: Promise<Result>['catch']
catch: Promise<Result<RootNode>>['catch']

/**
* Processes input CSS through synchronous and asynchronous plugins
Expand All @@ -47,7 +50,7 @@ declare class LazyResult_ implements PromiseLike<Result> {
* })
* ```
*/
finally: Promise<Result>['finally']
finally: Promise<Result<RootNode>>['finally']

/**
* Processes input CSS through synchronous and asynchronous plugins
Expand All @@ -62,7 +65,7 @@ declare class LazyResult_ implements PromiseLike<Result> {
* })
* ```
*/
then: Promise<Result>['then']
then: Promise<Result<RootNode>>['then']

/**
* @param processor Processor used for this transformation.
Expand All @@ -76,7 +79,7 @@ declare class LazyResult_ implements PromiseLike<Result> {
*
* @return Result with output content.
*/
async(): Promise<Result>
async(): Promise<Result<RootNode>>

/**
* An alias for the `css` property. Use it with syntaxes
Expand Down Expand Up @@ -145,7 +148,7 @@ declare class LazyResult_ implements PromiseLike<Result> {
*
* PostCSS runners should always use `LazyResult#then`.
*/
get root(): Root
get root(): RootNode

/**
* Returns the default string description of an object.
Expand All @@ -158,7 +161,7 @@ declare class LazyResult_ implements PromiseLike<Result> {
*
* @return Result with output content.
*/
sync(): Result
sync(): Result<RootNode>

/**
* Alias for the `LazyResult#css` property.
Expand All @@ -180,6 +183,8 @@ declare class LazyResult_ implements PromiseLike<Result> {
warnings(): Warning[]
}

declare class LazyResult extends LazyResult_ {}
declare class LazyResult<
RootNode = Document | Root
> extends LazyResult_<RootNode> {}

export = LazyResult
12 changes: 6 additions & 6 deletions lib/no-work-result.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ declare namespace NoWorkResult {
* let root = noWorkResult.root // now css is parsed because we accessed the root
* ```
*/
declare class NoWorkResult_ implements LazyResult {
catch: Promise<Result>['catch']
finally: Promise<Result>['finally']
then: Promise<Result>['then']
declare class NoWorkResult_ implements LazyResult<Root> {
catch: Promise<Result<Root>>['catch']
finally: Promise<Result<Root>>['finally']
then: Promise<Result<Root>>['then']
constructor(processor: Processor, css: string, opts: ResultOptions)
async(): Promise<Result>
async(): Promise<Result<Root>>
get content(): string
get css(): string
get map(): SourceMap
Expand All @@ -36,7 +36,7 @@ declare class NoWorkResult_ implements LazyResult {
get processor(): Processor
get root(): Root
get [Symbol.toStringTag](): string
sync(): Result
sync(): Result<Root>
toString(): string
warnings(): Warning[]
}
Expand Down
12 changes: 6 additions & 6 deletions lib/postcss.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,11 @@ declare namespace postcss {
(data: object[]): Node[]
}

export interface Syntax {
export interface Syntax<RootNode = Document | Root> {
/**
* Function to generate AST by string.
*/
parse?: Parser
parse?: Parser<RootNode>

/**
* Class to generate string by AST.
Expand Down Expand Up @@ -304,7 +304,7 @@ declare namespace postcss {
sourcesContent?: boolean
}

export interface ProcessOptions {
export interface ProcessOptions<RootNode = Document | Root> {
/**
* The path of the CSS source file. You should always set `from`,
* because it is used in source map generation and syntax error messages.
Expand All @@ -319,17 +319,17 @@ declare namespace postcss {
/**
* Function to generate AST by string.
*/
parser?: Parser | Syntax
parser?: Parser<RootNode> | Syntax<RootNode>

/**
* Class to generate string by AST.
*/
stringifier?: Stringifier | Syntax
stringifier?: Stringifier | Syntax<RootNode>

/**
* Object with parse and stringify.
*/
syntax?: Syntax
syntax?: Syntax<RootNode>

/**
* The path where you'll put the output CSS file. You should always set `to`
Expand Down
8 changes: 6 additions & 2 deletions lib/processor.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Document from './document.js'
import LazyResult from './lazy-result.js'
import NoWorkResult from './no-work-result.js'
import {
Expand Down Expand Up @@ -72,9 +73,12 @@ declare class Processor_ {
* @return Promise proxy.
*/
process(
css: { toString(): string } | LazyResult | Result | Root | string,
options?: ProcessOptions
css: { toString(): string } | LazyResult | Result | Root | string
): LazyResult | NoWorkResult
process<RootNode extends Document | Root = Root>(
css: { toString(): string } | LazyResult | Result | Root | string,
options: ProcessOptions<RootNode>
): LazyResult<RootNode>

/**
* Adds a plugin to be used as a CSS processor.
Expand Down
8 changes: 4 additions & 4 deletions lib/result.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ declare namespace Result {
* const result2 = postcss.parse(css).toResult()
* ```
*/
declare class Result_ {
declare class Result_<RootNode = Document | Root> {
/**
* A CSS string representing of `Result#root`.
*
Expand Down Expand Up @@ -141,14 +141,14 @@ declare class Result_ {
* root.toResult().root === root
* ```
*/
root: Document | Root
root: RootNode

/**
* @param processor Processor used for this transformation.
* @param root Root node after all transformations.
* @param opts Options from the `Processor#process` or `Root#toResult`.
*/
constructor(processor: Processor, root: Document | Root, opts: Result.ResultOptions)
constructor(processor: Processor, root: RootNode, opts: Result.ResultOptions)

/**
* An alias for the `Result#css` property.
Expand Down Expand Up @@ -201,6 +201,6 @@ declare class Result_ {
warnings(): Warning[]
}

declare class Result extends Result_ {}
declare class Result<RootNode = Document | Root> extends Result_<RootNode> {}

export = Result
22 changes: 15 additions & 7 deletions test/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import postcss, { PluginCreator, Result } from '../lib/postcss.js'
import postcss, { Document, PluginCreator } from '../lib/postcss.js'

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

plugin.postcss = true

const processResult: Promise<Result> | Result = postcss([
plugin
]).process('h1{color: black;}', { from: undefined })
postcss([plugin])
.process('h1{color: black;}', {
from: undefined
})
.then(result => {
console.log(result.root.parent)
console.log(result.css)
})

processResult.then((result: Result) => {
console.log(result.css)
})
function parseMarkdown(): Document {
return new Document()
}

let doc = postcss().process('a{}', { parser: parseMarkdown }).root
console.log(doc.toString())

export default plugin

0 comments on commit 7d51976

Please sign in to comment.