The keyword 'private' is reserved when I parse typescript code into ast

809 views Asked by At

I want to parse this code to ast:

const code = `
 class Test {
  constructor(private foo: Foo) { }
 }
`;

But when I run I get error:

  The keyword 'private' is reserved

This is the full code:

import * as escodegen from "escodegen";
import { Parser } from "acorn";

const code = `
class Test {
  
  constructor(private foo: Foo) { }
}
`;

const acorn = Parser.extend(require("acorn-jsx")(), require("acorn-bigint"));

const ast = acorn.parse(code, { ranges: false, ecmaVersion: "latest" });

const output = escodegen.generate(ast);

console.log({ output });

In astexplorer.net it is works. How make it work in codesandbox.io

1

There are 1 answers

2
kigiri On BEST ANSWER

You want to use the typescript compiler if you want to work with typescript code. You can read more here: https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API