Skip to content

Commit

Permalink
fix(types): add missing index argument to each/walk callback
Browse files Browse the repository at this point in the history
`container.each(callback)` and `container.walk(callback)`
support the second argument `index` in the `callback` function,
but the argument is not reflected on the type definitions.

See the API document:
- `each`: https://github.com/postcss/postcss-selector-parser/blob/3c072b84259b1966fda4823ee7f7228cabe2a164/API.md?plain=1#L462-L463
- `walk`: https://github.com/postcss/postcss-selector-parser/blob/3c072b84259b1966fda4823ee7f7228cabe2a164/API.md?plain=1#L478-L479
  • Loading branch information
ybiquitous committed Mar 12, 2024
1 parent 3c072b8 commit 6214f6a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"!**/__tests__"
],
"scripts": {
"pretest": "eslint src && tsc --noEmit postcss-selector-parser.d.ts",
"typecheck": "tsc --noEmit --strict postcss-selector-parser.d.ts postcss-selector-parser.test.ts",
"pretest": "eslint src && npm run typecheck",
"prepare": "del-cli dist && BABEL_ENV=publish babel src --out-dir dist --ignore /__tests__/",
"lintfix": "eslint --fix src",
"report": "nyc report --reporter=html",
Expand Down
4 changes: 2 additions & 2 deletions postcss-selector-parser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,9 @@ declare namespace parser {
empty(): this;
insertAfter(oldNode: Child, newNode: Child): this;
insertBefore(oldNode: Child, newNode: Child): this;
each(callback: (node: Child) => boolean | void): boolean | undefined;
each(callback: (node: Child, index: number) => boolean | void): boolean | undefined;
walk(
callback: (node: Node) => boolean | void
callback: (node: Node, index: number) => boolean | void
): boolean | undefined;
walkAttributes(
callback: (node: Attribute) => boolean | void
Expand Down
12 changes: 12 additions & 0 deletions postcss-selector-parser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as parser from './postcss-selector-parser';

parser((root) => {
root.each((node, index) => {
node as parser.Selector;
index as number;
});
root.walk((node, index) => {
node as parser.Selector;
index as number;
});
}).processSync("a b > c");
10 changes: 8 additions & 2 deletions src/__tests__/container.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ test('container#prepend', (t) => {

test('container#each', (t) => {
let str = '';
let indexes = [];
parse('h1, h2:not(h3, h4)', (selectors) => {
selectors.each((selector) => {
selectors.each((selector, index) => {
if (selector.first.type === 'tag') {
str += selector.first.value;
}
indexes.push(index);
});
});
t.deepEqual(str, 'h1h2');
t.deepEqual(indexes, [0, 1]);
});

test('container#each (safe iteration)', (t) => {
Expand Down Expand Up @@ -63,14 +66,17 @@ test('container#each (early exit)', (t) => {

test('container#walk', (t) => {
let str = '';
let indexes = [];
parse('h1, h2:not(h3, h4)', (selectors) => {
selectors.walk((selector) => {
selectors.walk((selector, index) => {
if (selector.type === 'tag') {
str += selector.value;
indexes.push(index);
}
});
});
t.deepEqual(str, 'h1h2h3h4');
t.deepEqual(indexes, [0, 0, 0, 0]);
});

test('container#walk (safe iteration)', (t) => {
Expand Down

0 comments on commit 6214f6a

Please sign in to comment.