Skip to content

Commit e2d7a60

Browse files
committed
Raise a compiler error if a synchronous processor method is invoked with an async processor callback.
1 parent 5255da3 commit e2d7a60

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

postcss-selector-parser.d.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
*/
1515
export = parser;
1616

17-
declare function parser(): parser.Processor;
18-
declare function parser<Transform extends any>(processor: parser.SyncProcessor<Transform>): parser.Processor<Transform>;
19-
declare function parser(processor: parser.SyncProcessor): parser.Processor;
20-
declare function parser<Transform extends any>(processor: parser.AsyncProcessor<Transform>): parser.Processor<Transform, string>;
21-
declare function parser(processor: parser.AsyncProcessor): parser.Processor<never, string>;
22-
declare function parser<Transform>(processor?: parser.SyncProcessor<Transform> | parser.AsyncProcessor<Transform>): parser.Processor<Transform, string>;
17+
declare function parser(): parser.Processor<never>;
18+
declare function parser<Transform extends any>(processor: parser.AsyncProcessor<Transform>): parser.Processor<Transform, never>;
19+
declare function parser(processor: parser.AsyncProcessor): parser.Processor<never>;
20+
declare function parser<Transform extends any>(processor: parser.SyncProcessor<Transform>): parser.Processor<Transform, never>;
21+
declare function parser(processor: parser.SyncProcessor): parser.Processor<never>;
22+
declare function parser<Transform>(processor?: parser.SyncProcessor<Transform> | parser.AsyncProcessor<Transform>): parser.Processor<Transform>;
2323

2424
/*~ If you want to expose types from your module as well, you can
2525
*~ place them in this block. Often you will want to describe the
@@ -45,7 +45,7 @@ declare namespace parser {
4545
/** Accepts a string */
4646
type Selectors = string | PostCSSRuleNode
4747
type SyncProcessor<Transform = void> = (root: parser.Root) => Transform
48-
type AsyncProcessor<Transform = void> = (root: parser.Root) => void | PromiseLike<Transform>
48+
type AsyncProcessor<Transform = void> = (root: parser.Root) => Transform | PromiseLike<Transform>
4949

5050
const TAG: "tag";
5151
const STRING: "string";
@@ -90,15 +90,18 @@ declare namespace parser {
9090
*/
9191
updateSelector: boolean;
9292
}
93-
class Processor<TransformType = never, AsyncProcessType extends string | never = never> {
93+
class Processor<
94+
TransformType = never,
95+
SyncSelectorsType extends Selectors | never = Selectors
96+
> {
9497
res: Root;
9598
readonly result: String;
9699
ast(selectors: Selectors, options?: Partial<Options>): Promise<Root>;
97-
astSync(selectors: Selectors, options?: Partial<Options>): Root;
100+
astSync(selectors: SyncSelectorsType, options?: Partial<Options>): Root;
98101
transform(selectors: Selectors, options?: Partial<Options>): Promise<TransformType>;
99-
transformSync(selectors: Selectors, options?: Partial<Options>): TransformType;
100-
process(selectors: Selectors, options?: Partial<Options>): Promise<AsyncProcessType>;
101-
processSync(selectors: Selectors, options?: Partial<Options>): string;
102+
transformSync(selectors: SyncSelectorsType, options?: Partial<Options>): TransformType;
103+
process(selectors: Selectors, options?: Partial<Options>): Promise<string>;
104+
processSync(selectors: SyncSelectorsType, options?: Partial<Options>): string;
102105
}
103106
interface ParserOptions {
104107
css: string;

src/processor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export default class Processor {
5050
let root = this._root(rule, error, options);
5151
let transform = this.func(root);
5252
if (transform && typeof transform.then === "function") {
53-
throw new Error("Cannot selector processor returned a promise to a synchronous call.");
53+
throw new Error("Selector processor returned a promise to a synchronous call.");
5454
}
5555
let string = undefined;
5656
if (options.updateSelector && typeof rule !== "string") {

0 commit comments

Comments
 (0)