question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

This is a doc suggestion + a more general question.

It may be worth putting in the docs some thoughts on what the contract is wrt mutating the tree. In particular, modifying individual nodes is fine, but unist-util-visit assumes the visitor does not mutate the number of children of the parent.

This is a bit of a pitfall, as it’s not uncommon to want to traverse and mutate adjacent children. For example, see https://github.com/jackycute/remark-html-emoji-image/blob/master/index.js#L66-L69 — but it appears that approach is buggy, since unist-util-visit visits children sequentially by index.

More generally, I’d be interested to know what approach is best to handle the fully mutating use case. https://github.com/syntax-tree/unist-util-map and https://github.com/eush77/unist-util-filter are listed but cover different use cases. One easy option (but may not be the best) is to make a visit-dynamic variant, something like:

'use strict';

module.exports = visit;

/**
 * A mutating version of https://github.com/syntax-tree/unist-util-visit
 *
 * Traverse a Unist tree, while also allowing addition/deletion of children.
 * Visitor should return number of nodes added/deleted (positive for addition, negative for removal,
 * 0 if only visited), or false to terminate. To avoid confusion, we don't support reverse traversal.
 */
function visit(tree, type, visitor) {
  if (typeof type === 'function') {
    visitor = type;
    type = null;
  }

  one(tree);

  /* Visit a single node. */
  function one(node, index, parent) {
    let result;

    index = index || (parent ? 0 : null);

    if (!type || node.type === type) {
      result = visitor(node, index, parent || null);
    }

    if (node.children && result !== false) {
      all(node);
    }

    return result;
  }

  /* Visit children in `parent`, possibly adding or removing children. */
  function all(parent) {
    let child;
    let index = 0;
    while (index < parent.children.length) {
      child = parent.children[index];

      let delta = 0;
      if (child) {
        delta = one(child, index, parent);
        if (delta === false) {  // Abort.
          return;
        }
      }

      index += 1 + (delta || 0);
    }
  }
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
wooormcommented, Nov 17, 2017

Sweet, thanks! I added you to the org, so feel free to create a utility here and we’ll help maintain it!

How much code would it add in this utility to have this added here though, without breaking current things?

1reaction
jlevycommented, Nov 17, 2017

Hey @wooorm ! Cool, thanks for the response. Well, good to hear my understanding matches yours. 😃

We ended up using visit-dynamic variant I put above, and it’s been working fine for our use cases. I’m not sure if I’ll have bandwidth to publish/maintain it myself right now — perhaps we can set it up under syntax-tree as an alternative to this visitor, if you think it’s useful? You’re welcome to the code above!

Also just PR’ed a doc clarification.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mutation | Definition, Causes, Types, & Facts | Britannica
mutation, an alteration in the genetic material (the genome) of a cell of a living organism or of a virus that is more...
Read more >
Mutation - Simple English Wikipedia, the free encyclopedia
In biology, a mutation is a change in the genetic material. This means changes to the DNA or to the chromosomes which carry...
Read more >
What is a mutation? - YourGenome
A mutation is a change that occurs in our DNA sequence, either due to mistakes when the DNA is copied or as the...
Read more >
Mutation Notes
Mutation Notes. Mutations- changes to genetic material. Two types: Gene Mutations and Chromosomal Mutations. Gene Mutations (a.k.a. point mutations)- ...
Read more >
What Are Mutations?Definition, Causes and Effects of ... - Byju's
The DNA sequence is specific to each organism. It can sometimes undergo changes in its base-pairs sequence. It is termed as a mutation....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found