Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions postcss-selector-parser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ export = parser;
type Diff<T, U> = T extends U ? never : T;

// TODO: Conditional types in TS 1.8 will really clean this up.
declare function parser(): parser.Processor<never>;
declare function parser<Transform>(processor: parser.AsyncProcessor<Transform>): parser.Processor<Transform, never>;
declare function parser(processor: parser.AsyncProcessor<void>): parser.Processor<never, never>;
declare function parser<Transform>(processor: parser.SyncProcessor<Transform>): parser.Processor<Transform>;
declare function parser(processor: parser.SyncProcessor<void>): parser.Processor<never>;
declare function parser<Transform>(processor?: parser.SyncProcessor<Transform> | parser.AsyncProcessor<Transform>): parser.Processor<Transform>;
declare function parser(processor?: undefined, options?: Partial<parser.Options>): parser.Processor<never>;
declare function parser<Transform>(processor: parser.AsyncProcessor<Transform>, options?: Partial<parser.Options>): parser.Processor<Transform, never>;
declare function parser(processor: parser.AsyncProcessor<void>, options?: Partial<parser.Options>): parser.Processor<never, never>;
declare function parser<Transform>(processor: parser.SyncProcessor<Transform>, options?: Partial<parser.Options>): parser.Processor<Transform>;
declare function parser(processor: parser.SyncProcessor<void>, options?: Partial<parser.Options>): parser.Processor<never>;
declare function parser<Transform>(processor?: parser.SyncProcessor<Transform> | parser.AsyncProcessor<Transform>, options?: Partial<parser.Options>): parser.Processor<Transform>;

/*~ If you want to expose types from your module as well, you can
*~ place them in this block. Often you will want to describe the
Expand Down
10 changes: 10 additions & 0 deletions postcss-selector-parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import * as parser from './postcss-selector-parser';

parser(undefined, { lossless: false }).processSync('a > b');
parser(() => {}, { lossless: false }).processSync('a > b');
parser(() => 1, { lossless: false }).transformSync('a > b') satisfies number;
declare const asyncProcessor: parser.AsyncProcessor<number>;
declare const asyncVoidProcessor: parser.AsyncProcessor<void>;
parser(asyncVoidProcessor, { lossless: false }).process('a > b');
parser(asyncProcessor, { lossless: false }).transform('a > b') satisfies Promise<number>;
// @ts-expect-error An async processor cannot be called synchronously.
parser(asyncProcessor, { lossless: false }).processSync('a > b');

parser((root) => {
root.each((node, index) => {
node satisfies parser.Selector;
Expand Down
36 changes: 36 additions & 0 deletions src/__tests__/parser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,42 @@ test("no operation", (t) => {
t.notThrows(() => parser().processSync("h1 h2 h3"));
});

test("default options work sync and can be overridden", (t) => {
const processor = parser(undefined, { lossless: false });
t.is(processor.processSync("a > b"), "a>b");
t.is(processor.processSync("a > b", { lossless: true }), "a > b");
t.is(processor.processSync("a > b"), "a>b");
});

test("default options work async and can be overridden", async (t) => {
const processor = parser(undefined, { lossless: false });
t.is(await processor.process("a > b"), "a>b");
t.is(await processor.process("a > b", { lossless: true }), "a > b");
t.is(await processor.process("a > b"), "a>b");
});

test("default maxNestingDepth works and can be overridden", (t) => {
const processor = parser(undefined, { maxNestingDepth: 1 });
const selector = ":not(:not(a))";
t.throws(() => processor.processSync(selector), { instanceOf: Error });
t.is(processor.processSync(selector, { maxNestingDepth: 10 }), selector);
});

test("default updateSelector works async and can be overridden", async (t) => {
const processor = parser(
(root) =>
root.walkClasses((node) => {
node.value = "bar";
}),
{ updateSelector: false },
);
const rule = { selector: ".foo" };
t.is(await processor.process(rule), ".bar");
t.is(rule.selector, ".foo");
t.is(await processor.process(rule, { updateSelector: true }), ".bar");
t.is(rule.selector, ".bar");
});

test("empty selector string", (t) => {
t.notThrows(() => {
return parser((selectors) => {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Processor from "./processor";
import * as selectors from "./selectors";

const parser = (processor?: any) => new Processor(processor);
const parser = (processor?: any, options?: any) => new Processor(processor, options);

Object.assign(parser, selectors);

Expand Down