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

prevOpen Source→ast-monkey-utilnext

ast-monkey-util3.3.1

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

No dependencies whatsoever. This package declares no dependencies or devDependencies.

Permalink to InstallationInstallation

Permalink to Quick TakeQuick Take

Permalink to ExamplesExamples

  • 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 traversal, identify the node you need, amend its path with this package, then use object-path to get, set, or delete that path.

For programmatic lookup, pass the callback’s innerObj.pathSegments array directly to these helpers and then to object-path. The array overloads preserve keys that contain dots or are empty. Don’t join pathSegments, and don’t use the callback’s legacy dot-joined innerObj.path when exact key boundaries matter.

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"

Pass a segment array to preserve exact key boundaries. parent() then returns the penultimate segment without splitting keys that contain dots:

console.log(parent(["a.b", "c"]));
// => "a.b"

An array with fewer than two segments has no parent key, so parent() returns null.

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"

pathNext() processes a digit-only final segment as a decimal string, so the result remains exact beyond Number.MAX_SAFE_INTEGER.

With a segment array, pathNext() returns a new array and increments only the final segment:

console.log(pathNext(["a.b", "children", "0"]));
// => ["a.b", "children", "1"]

The input array isn’t modified. An empty array or an array whose final segment is empty is returned unchanged as a new array.

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"

pathPrev() uses the same exact decimal-string arithmetic and returns null when the final segment is zero or isn’t numeric.

With a segment array, pathPrev() returns a new array whose final segment is decremented. It returns null for an empty array or when the final segment is zero, empty, or nonnumeric.

console.log(pathPrev(["a.b", "children", "2"]));
// => ["a.b", "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"));
// => "0"

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

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

For a root-level string path, pathUp() returns the intentional fallback "0".

With a segment array, pathUp() returns a new array after removing its final two segments. Arrays with two or fewer segments return the root fallback ["0"]:

console.log(pathUp(["a.b", "children", "0"]));
// => ["a.b"]

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:

Permalink to changelogChangelog

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