Skip to content

Fix flawed unist-util-visit usage#1447

Merged
0x2b3bfa0 merged 3 commits intomasterfrom
0x2b3bfa0-patch-1
May 9, 2024
Merged

Fix flawed unist-util-visit usage#1447
0x2b3bfa0 merged 3 commits intomasterfrom
0x2b3bfa0-patch-1

Conversation

@0x2b3bfa0
Copy link
Member

@0x2b3bfa0 0x2b3bfa0 commented May 8, 2024

It turns out that unist-util-visit takes a Visitor function, that may optionally return an Index to "specify the
sibling to traverse after [the current] node is traversed"
, but Array.prototype.push "returns the new length of the array", causing an erratic tree traversal.

Minimal example

const remark = require('remark');
const visit = require('unist-util-visit');

const report = `
# Heading 1
[![description](https://img.shields.io/badge/...)](https://studio.iterative.ai/team/...)
## Heading 2
![one](./one)
![two](./two)
![three](./three)
`;

function plugin(tree) {
  const nodes = [];

  // The visitor function returns the result of calling nodes.push()
  visit(tree, ['definition', 'image', 'link'], (node) => nodes.push(node));

  nodes.map(({url}) => console.log(url));
}

remark().use(() => plugin).process(report);
https://studio.iterative.ai/team/...
https://img.shields.io/badge/...
./one
./three
./three

@0x2b3bfa0 0x2b3bfa0 added the bug Something isn't working label May 8, 2024
@0x2b3bfa0 0x2b3bfa0 requested a review from shcheklein May 8, 2024 08:05
@0x2b3bfa0 0x2b3bfa0 self-assigned this May 8, 2024
@github-actions
Copy link
Contributor

github-actions bot commented May 8, 2024

Test Comment

@github-actions
Copy link
Contributor

github-actions bot commented May 8, 2024

Test Comment

@github-actions
Copy link
Contributor

github-actions bot commented May 8, 2024

Test Comment

@github-actions
Copy link
Contributor

github-actions bot commented May 8, 2024

Test Comment

@github-actions
Copy link
Contributor

github-actions bot commented May 8, 2024

Test Comment

@github-actions
Copy link
Contributor

github-actions bot commented May 8, 2024

Test Comment

Copy link
Member

@shcheklein shcheklein left a comment

Choose a reason for hiding this comment

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

thanks a lot @0x2b3bfa0 !

@0x2b3bfa0 0x2b3bfa0 merged commit 1c49104 into master May 9, 2024
@0x2b3bfa0 0x2b3bfa0 deleted the 0x2b3bfa0-patch-1 branch May 9, 2024 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants