Skip to Content
  • Website
Codsen
  • Home
  • Open Source
  • Articles
  • About

prevOpen Source→ast-monkey-utilnext

ast-monkey-util3.2.0

Utility library of AST helper functions

Downloads per monthChangelogMIT Licenselibera manifesto
  • the top
  • Installation
  • Quick Take
  • Examples
  • IDEA
  • API — PARENT()
  • API — PATHNEXT()
  • API — PATHPREV()
  • API — PATHUP()
  • API — VERSION
  • Changelog

Installation

Quick Take

Examples

  • Find the parent segment
  • Move to the next sibling path
  • Move to the previous sibling path
  • Move up one AST level

Idea

codsen-parser and emlint both use object-path notation. This utility program contains helper functions to traverse the paths.

Conceptually, you’d use ast-monkey-traverse, identify the node you need, then get its path (from the same program, from callbacks), then amend that path (using this program), then use object-path to get/set/delete that path.

API — parent()

The function parent() is imported like this:

It’s a function which takes one input argument:

It calculates the parent key, for example:

console.log(parent("a"));
// => null

console.log(parent("0"));
// => null

console.log(parent("a.b"));
// => "a"

console.log(parent("a.0"));
// => "a"

console.log(parent("a.0.c"));
// => "0"

API — pathNext()

The function pathNext() is imported like this:

It’s a function which takes one input argument:

It takes (a string) path and increments the last digit:

console.log(pathNext("0"));
// => "1"

console.log(pathNext("9.children.3"));
// => "9.children.4"

console.log(pathNext("9.children.1.children.0"));
// => "9.children.1.children.1"

API — pathPrev()

The function pathPrev() is imported like this:

It’s a function which takes one input argument:

It takes (a string) path and decrements the last digit:

console.log(pathPrev("0"));
// => null

console.log(pathPrev("9.children.33"));
// => "9.children.32"

console.log(pathPrev("9.children.1.children.2"));
// => "9.children.1.children.1"

API — pathUp()

The function pathUp() is imported like this:

It’s a function which takes one input argument:

It takes (a string) path and goes “one level” up, discarding the last two path parts:

console.log(pathUp("1"));
// => null

console.log(pathUp("9.children.3"));
// => "9"

console.log(pathUp("9.children.1.children.2"));
// => "9.children.1"

Practically, if you think, codsen-parser always outputs an array. It contains zero or more plain objects, each representing a tag, a chunk of text, a comment tag and so on.

Since root element is array, paths of those plain objects are digits: 0, 1, 5.children.0 and so on.

In codsen-parser AST’s, child nodes are nested within children key — its value is array:

The following HTML:

<a>text</a>

Would yield AST (many keys omitted):

[
  {
    "type": "tag",
    "start": 0,
    "end": 3,
    "value": "<a>",
    "attribs": [],
    "children": [
      {
        "type": "text",
        "start": 3,
        "end": 7,
        "value": "text"
      }
    ]
  },
  {
    "type": "tag",
    "start": 7,
    "end": 11,
    "value": "</a>",
    "attribs": [],
    "children": []
  }
]

Thus, a text node for value “text” (one with "start": 3 above) is at the path 0.children.0 (first element’s first child node) and “going up” would mean “0″ — that’s splitting by dot into an array and discarding the last two elements from that array, then joining it back with a dot.

0 . children . 0
        ^      ^
    these two are removed during the "go up" action

API — version

You can import version:

Changelog

Open Changelog
↑ back to top
prev next

Copyright

All rights reserved © Roy Revelt 2026
All our open source packages are under MIT licenceopens in a new tab

Activities

🐛 See a bug? Raise an issueopens in a new tab
💘 Check out the Indiewebopens in a new tab and Libera manifestoopens in a new tab