From 962d75e16eed3fb0d709dad53d852005813fa311 Mon Sep 17 00:00:00 2001 From: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> Date: Wed, 13 Mar 2024 22:25:57 +0900 Subject: [PATCH] fix(types): add missing `index` argument to `each`/`walk` callback (#289) --- package.json | 3 ++- postcss-selector-parser.d.ts | 4 ++-- postcss-selector-parser.test.ts | 12 ++++++++++++ src/__tests__/container.mjs | 10 ++++++++-- 4 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 postcss-selector-parser.test.ts diff --git a/package.json b/package.json index d1f6be8..bce9147 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/postcss-selector-parser.d.ts b/postcss-selector-parser.d.ts index fbf040e..9163ab1 100644 --- a/postcss-selector-parser.d.ts +++ b/postcss-selector-parser.d.ts @@ -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 diff --git a/postcss-selector-parser.test.ts b/postcss-selector-parser.test.ts new file mode 100644 index 0000000..bd7a01a --- /dev/null +++ b/postcss-selector-parser.test.ts @@ -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"); diff --git a/src/__tests__/container.mjs b/src/__tests__/container.mjs index f418349..28c4c3e 100644 --- a/src/__tests__/container.mjs +++ b/src/__tests__/container.mjs @@ -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) => { @@ -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) => {