You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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?
The text was updated successfully, but these errors were encountered:
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:But when I swap the traverse method using
@babel/traverse
, program errors out and shows this error:Any ideas what I did wrong? What's the best way to traverse magicast's AST?
The text was updated successfully, but these errors were encountered: