Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(types): add missing index argument to each/walk callback #289

Merged
Merged
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
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
Copy link
Contributor Author

@ybiquitous ybiquitous Mar 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note] I added this file to test the .d.ts file, but I can remove the file if unnecessary.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine with it

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add more tests in future when we need it

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