iterative / cml

♾️ CML - Continuous Machine Learning | CI/CD for ML
http://cml.dev
Apache License 2.0
3.99k stars 333 forks source link

Fix flawed `unist-util-visit` usage #1447

Closed 0x2b3bfa0 closed 3 months ago

0x2b3bfa0 commented 3 months ago

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
github-actions[bot] commented 3 months ago

Test Comment

github-actions[bot] commented 3 months ago

Test Comment

github-actions[bot] commented 3 months ago

Test Comment

github-actions[bot] commented 3 months ago

Test Comment

github-actions[bot] commented 3 months ago

Test Comment

github-actions[bot] commented 3 months ago

Test Comment