denoland / wanted_modules

Is there a missing deno module that is preventing you from building something? Let us know here.
https://deno.land/x
46 stars 2 forks source link

ast parser #10

Open lambtron opened 2 years ago

lambtron commented 2 years ago

deno parse to get the ast of html, js, xml any

origin: https://discord.com/channels/684898665143206084/769285344809451572/769323040324452352

for reference: https://www.npmjs.com/package/abstract-syntax-tree

littledivy commented 2 years ago
tjosepo commented 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);
jollytoad commented 1 year ago

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