Open lambtron opened 2 years ago
Recast works if you import it from jspm and use the TypeScript parser:
import { parse } from "https://jspm.dev/npm:recast";
import TSParser from "https://jspm.dev/npm:recast/parsers/typescript";
// Let's turn this function declaration into a variable declaration.
const code = [
"function add(a: number, b: number): number {",
" return a +",
" // Weird formatting, huh?",
" b;",
"}"
].join("\n");
// Parse the code using an interface similar to require("esprima").parse.
const ast = parse(code, { parser: TSParser });
console.log(ast);
https://github.com/syntax-tree/unist ... an excellent ecosystem of syntax trees, and utilities (in the wider Unified ecosystem).
Mostly focused around markup languages (html, xml, markdown) rather than code, but there isn't anything to prevent unist based ASTs being created for programming languages.
I've found many of these libraries already work really well in Deno imported via esm.sh
origin: https://discord.com/channels/684898665143206084/769285344809451572/769323040324452352
for reference: https://www.npmjs.com/package/abstract-syntax-tree