7

As it is known that Typescript is completely opensource now. which is available at Tyescript. I am building an application that will get Typescript code as input and give output the AST of the given code. Provide me a proper way to extract this AST(Abstract Syntax Tree) of input Typescript code rather than comppliling it and converting it into Javascript.

1
  • You could also inspect the AST in a more visual way in astexplorer.net Commented Nov 7, 2018 at 10:14

2 Answers 2

6

Basic code:

const fileNames = ["C:\\MyFile.ts"];
const compilerOptions: ts.CompilerOptions = {
    // compiler options go here if any...
    // look at ts.CompilerOptions to see what's available
};
const program = ts.createProgram(fileNames, compilerOptions);
const typeChecker = program.getTypeChecker();
const sourceFiles = program.getSourceFiles();

sourceFiles.filter(f => /MyFile\.ts$/.test(f.fileName)).forEach(sourceFile => {
    ts.forEachChild(sourceFile, node => {
        const declaration = node as ts.Declaration;
        if (declaration.name) {
            console.log(declaration.name.getText());
        }
    });
});

So if you provided that with a C:\MyFile.ts like:

class MyClass {}
interface MyInterface {}

It would output MyClass and MyInterface.

Figuring out everything beyond what I've just shown is a lot of work. It might be more beneficial for you to look at and/or help contribute to this work in progress.

Sign up to request clarification or add additional context in comments.

6 Comments

I did not understand the area {/* compiler options go here*/}. Please explain what is its mean?
@AhmedRaza compiler options like what is found in tsconfig.json. The type will be ts.CompilerOptions. I'll update my answer.
Please share any example related to it.
@AhmedRaza I updated it with a working example, but you will have to figure out the rest. It's a lot of work.
yes its good but it is also give the same output if i declare any variable or function in class and interface. How can i extract the variables and functions in class.
|
0

TypeScript compiler doesn't support it but you can do it using recast and babylon@next. Although you will have to trust in the syntax defined by these technologies for representing TypeScript code AST and that they will keep up to date - since TypeScript has new language features release by release (short period of time) - is not like other languages (JavaScript) where you have well defined versions and released in a standard - so if your users start using new language features these technologies (mostly babylon) should keep up to date:

// npm install recast babylon@next
const source = `
interface I {
  color: string
}
class C implements I{
  color: string='blue'
}
`
const recast = require('recast')
const tsParser = require("recast/parsers/typescript")
const ast = recast.parse(source, {
  parser: tsParser
});
console.log(`
CODE: 

${source}

AST: 

${JSON.stringify(ast)}
`);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.