Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best way to traverse nodes? #40

Open
grikomsn opened this issue Apr 8, 2023 · 2 comments
Open

Best way to traverse nodes? #40

grikomsn opened this issue Apr 8, 2023 · 2 comments

Comments

@grikomsn
Copy link

grikomsn commented Apr 8, 2023

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/@babel+traverse@7.21.3/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/@babel+traverse@7.21.3/node_modules/@babel/traverse/lib/scope/index.js:740:11)
    at Scope.crawl (/Users/grikomsn/Workspace/.../node_modules/.pnpm/@babel+traverse@7.21.3/node_modules/@babel/traverse/lib/scope/index.js:663:32)
    at Scope.init (/Users/grikomsn/Workspace/.../node_modules/.pnpm/@babel+traverse@7.21.3/node_modules/@babel/traverse/lib/scope/index.js:653:12)
    at NodePath.setScope (/Users/grikomsn/Workspace/.../node_modules/.pnpm/@babel+traverse@7.21.3/node_modules/@babel/traverse/lib/path/context.js:116:30)
    at NodePath.setContext (/Users/grikomsn/Workspace/.../node_modules/.pnpm/@babel+traverse@7.21.3/node_modules/@babel/traverse/lib/path/context.js:128:8)
    at NodePath.pushContext (/Users/grikomsn/Workspace/.../node_modules/.pnpm/@babel+traverse@7.21.3/node_modules/@babel/traverse/lib/path/context.js:185:8)
    at TraversalContext.visitQueue (/Users/grikomsn/Workspace/.../node_modules/.pnpm/@babel+traverse@7.21.3/node_modules/@babel/traverse/lib/context.js:78:14)
    at TraversalContext.visitSingle (/Users/grikomsn/Workspace/.../node_modules/.pnpm/@babel+traverse@7.21.3/node_modules/@babel/traverse/lib/context.js:65:19)
    at TraversalContext.visit (/Users/grikomsn/Workspace/.../node_modules/.pnpm/@babel+traverse@7.21.3/node_modules/@babel/traverse/lib/context.js:109:19)
    at traverseNode (/Users/grikomsn/Workspace/.../node_modules/.pnpm/@babel+traverse@7.21.3/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?

@idfunctor
Copy link

babel/babel#13742 (comment)

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

@antfu
Copy link
Member

antfu commented Sep 15, 2023

let body: ASTNode = root.program.body[0];

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants