Skip to content

Commit 962d75e

Browse files
authored
fix(types): add missing index argument to each/walk callback (#289)
1 parent 3c072b8 commit 962d75e

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"!**/__tests__"
3434
],
3535
"scripts": {
36-
"pretest": "eslint src && tsc --noEmit postcss-selector-parser.d.ts",
36+
"typecheck": "tsc --noEmit --strict postcss-selector-parser.d.ts postcss-selector-parser.test.ts",
37+
"pretest": "eslint src && npm run typecheck",
3738
"prepare": "del-cli dist && BABEL_ENV=publish babel src --out-dir dist --ignore /__tests__/",
3839
"lintfix": "eslint --fix src",
3940
"report": "nyc report --reporter=html",

postcss-selector-parser.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,9 @@ declare namespace parser {
237237
empty(): this;
238238
insertAfter(oldNode: Child, newNode: Child): this;
239239
insertBefore(oldNode: Child, newNode: Child): this;
240-
each(callback: (node: Child) => boolean | void): boolean | undefined;
240+
each(callback: (node: Child, index: number) => boolean | void): boolean | undefined;
241241
walk(
242-
callback: (node: Node) => boolean | void
242+
callback: (node: Node, index: number) => boolean | void
243243
): boolean | undefined;
244244
walkAttributes(
245245
callback: (node: Attribute) => boolean | void

postcss-selector-parser.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as parser from './postcss-selector-parser';
2+
3+
parser((root) => {
4+
root.each((node, index) => {
5+
node as parser.Selector;
6+
index as number;
7+
});
8+
root.walk((node, index) => {
9+
node as parser.Selector;
10+
index as number;
11+
});
12+
}).processSync("a b > c");

src/__tests__/container.mjs

+8-2
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ test('container#prepend', (t) => {
2222

2323
test('container#each', (t) => {
2424
let str = '';
25+
let indexes = [];
2526
parse('h1, h2:not(h3, h4)', (selectors) => {
26-
selectors.each((selector) => {
27+
selectors.each((selector, index) => {
2728
if (selector.first.type === 'tag') {
2829
str += selector.first.value;
2930
}
31+
indexes.push(index);
3032
});
3133
});
3234
t.deepEqual(str, 'h1h2');
35+
t.deepEqual(indexes, [0, 1]);
3336
});
3437

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

6467
test('container#walk', (t) => {
6568
let str = '';
69+
let indexes = [];
6670
parse('h1, h2:not(h3, h4)', (selectors) => {
67-
selectors.walk((selector) => {
71+
selectors.walk((selector, index) => {
6872
if (selector.type === 'tag') {
6973
str += selector.value;
74+
indexes.push(index);
7075
}
7176
});
7277
});
7378
t.deepEqual(str, 'h1h2h3h4');
79+
t.deepEqual(indexes, [0, 0, 0, 0]);
7480
});
7581

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

0 commit comments

Comments
 (0)