magicast icon indicating copy to clipboard operation
magicast copied to clipboard

Best way to traverse nodes?

Open grikomsn opened this issue 2 years ago • 2 comments

I'm new to the AST world and trying magicast to do some codemod stuff. Currently I'm using @babel/types#traverse function which I assumed was the same as @babel/traverse.

When I do this snippet below using @babel/types#traverse, things work as intended:

import * as fs from "node:fs/promises";
import * as path from "node:path";
import * as t from "@babel/types";
import * as m from "magicast";

const filename = path.join(process.cwd(), "src/client.ts");
const content = await fs.readFile(filename, { encoding: "utf-8" });
const mod = m.parseModule(content);

t.assertProgram(mod.$ast);
t.traverse(mod.$ast, {
  enter: (p) => {
    ...
  },
});

But when I swap the traverse method using @babel/traverse, program errors out and shows this error:

  import * as fs from "node:fs/promises";
  import * as path from "node:path";
- import * as t from "@babel/types";
+ import traverse from "@babel/traverse";
  import * as m from "magicast";

  const filename = path.join(process.cwd(), "src/client.ts");
  const content = await fs.readFile(filename, { encoding: "utf-8" });
  const mod = m.parseModule(content);

  t.assertProgram(mod.$ast);
- t.traverse(mod.$ast, {
+ traverse(mod.$ast, {
    enter: (p) => {
      ...
    },
  });
/Users/grikomsn/Workspace/.../node_modules/.pnpm/@[email protected]/node_modules/@babel/traverse/lib/scope/index.js:740
    throw new Error("Couldn't find a Program");
          ^


Error: Couldn't find a Program
    at Scope.getProgramParent (/Users/grikomsn/Workspace/.../node_modules/.pnpm/@[email protected]/node_modules/@babel/traverse/lib/scope/index.js:740:11)
    at Scope.crawl (/Users/grikomsn/Workspace/.../node_modules/.pnpm/@[email protected]/node_modules/@babel/traverse/lib/scope/index.js:663:32)
    at Scope.init (/Users/grikomsn/Workspace/.../node_modules/.pnpm/@[email protected]/node_modules/@babel/traverse/lib/scope/index.js:653:12)
    at NodePath.setScope (/Users/grikomsn/Workspace/.../node_modules/.pnpm/@[email protected]/node_modules/@babel/traverse/lib/path/context.js:116:30)
    at NodePath.setContext (/Users/grikomsn/Workspace/.../node_modules/.pnpm/@[email protected]/node_modules/@babel/traverse/lib/path/context.js:128:8)
    at NodePath.pushContext (/Users/grikomsn/Workspace/.../node_modules/.pnpm/@[email protected]/node_modules/@babel/traverse/lib/path/context.js:185:8)
    at TraversalContext.visitQueue (/Users/grikomsn/Workspace/.../node_modules/.pnpm/@[email protected]/node_modules/@babel/traverse/lib/context.js:78:14)
    at TraversalContext.visitSingle (/Users/grikomsn/Workspace/.../node_modules/.pnpm/@[email protected]/node_modules/@babel/traverse/lib/context.js:65:19)
    at TraversalContext.visit (/Users/grikomsn/Workspace/.../node_modules/.pnpm/@[email protected]/node_modules/@babel/traverse/lib/context.js:109:19)
    at traverseNode (/Users/grikomsn/Workspace/.../node_modules/.pnpm/@[email protected]/node_modules/@babel/traverse/lib/traverse-node.js:18:17)

Any ideas what I did wrong? What's the best way to traverse magicast's AST?

grikomsn avatar Apr 08 '23 19:04 grikomsn

https://github.com/babel/babel/discussions/13742#discussioncomment-1303246

At least it's clear now why the error comes. Gotta figure out how to access the top level of the tree with magicast

idfunctor avatar Sep 13 '23 21:09 idfunctor

https://github.com/unjs/magicast/blob/20da32ed1e0ba245981c7f67ed14aac879bf484c/src/code.ts#L34

You should be able to access the program node with mod.$ast.program, won't it?

antfu avatar Sep 15 '23 11:09 antfu