Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ast-monkey-traverse

codsen50.8kMIT4.0.19TypeScript support: included

Utility library to traverse AST

ast, by, delete, drop, from, helper, html, key, monkey, nested, object, parsed, plain, posthtml, posthtml-tree, posthtmltree, traverse, tree, utility, value, xml

readme

ast-monkey-traverse

Utility library to traverse AST

page on codsen.com page on npm page on github Downloads per month changelog MIT Licence

Install

This package is pure ESM. If you're not ready yet, install an older version of this program, 2.1.0 (npm i ast-monkey-traverse@2.1.0).

npm i ast-monkey-traverse

Quick Take

import { strict as assert } from "assert";

import { traverse } from "ast-monkey-traverse";

const paths = [];
const source = {
  a: {
    foo: {
      bar: [
        {
          foo: "c",
        },
      ],
      d: {
        e: {
          foo: "f",
        },
      },
    },
  },
};

traverse(source, (key, val, innerObj) => {
  // if currently an object is traversed, you get both "key" and "val"
  // if it's array, only "key" is present, "val" is undefined
  let current = val !== undefined ? val : key;
  if (
    // it's object (not array)
    val !== undefined &&
    // and has the key we need
    key === "foo"
  ) {
    // push the path to array in the outer scope
    paths.push(innerObj.path);
  }
  return current;
});

// notice object-path notation "a.foo.bar.0.foo" - array segments use dots too:
assert.deepEqual(paths, ["a.foo", "a.foo.bar.0.foo", "a.foo.d.e.foo"]);

Documentation

Please visit codsen.com for a full description of the API.

Contributing

To report bugs or request features or assistance, raise an issue on GitHub.

Licence

MIT License.

Copyright © 2010-2025 Roy Revelt and other contributors.

ok codsen star

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

4.0.0 (2022-12-01)

BREAKING CHANGES

  • Minimum supported Node version is v14.18; we're dropping v12 support

3.1.0 (2022-08-12)

Features

3.0.0 (2021-09-09)

Features

BREAKING CHANGES

  • programs now are in ES Modules and won't work with Common JS require()

2.1.0 (2021-05-24)

Features

  • config file based major bump blacklisting (e15f9bb)

2.0.15 (2021-04-11)

Reverts

  • Revert "chore: setup refresh" (23cf206)

2.0.1 (2021-01-28)

Fixed

  • add testStats to npmignore (f3c84e9)

2.0.0 (2021-01-23)

Features

  • rewrite in TS, named export is now used (8983e69)

BREAKING CHANGES

  • named export is now used - use: import { traverse } from ... instead of import traverse from ...

1.13.0 (2020-11-28)

Accidental version bump during migration to SourceHut. Sorry about that.

1.12.0 (2019-11-27)

Features

1.11.0 (2019-01-31)

Features

  • Add a new key, parentType in a callback innerObj (6e16b99)

1.10.0 (2019-01-20)

  • Various documentation and setup tweaks after we migrated to monorepo
  • Setup refresh: updated dependencies and all config files using automated tools

1.4.0 (2018-10-13)

  • Updated all dependencies and restored coverage tracking both via terminal when testing and through coveralls.io

1.3.0 (2018-06-11)

GitHub sold us out. In the meantime, we:

  • Migrated to BitBucket (to host repo + perform CI) and Codacy (for code quality audit)
  • Dropped BitHound (RIP) and Travis
  • Removed package-lock

1.2.0 (2018-05-02)

  • Set up Prettier
  • Removed package.lock and .editorconfig
  • Wired Rollup to remove comments from non-dev builds. This means we can now leave the console.logs in the source code — Rollup will remove from production code.

1.1.0 (2018-01-03)

  • New key in the innerObj callback - innerObj.path. It's interoperable with object-path.

1.0.1 (2017-12-22)

  • First public release.