Skip to content

Instantly share code, notes, and snippets.

@allenhwkim
Last active December 10, 2019 17:59
Show Gist options
  • Select an option

  • Save allenhwkim/8c52ac84b00a2a697f2bbef3b5a83350 to your computer and use it in GitHub Desktop.

Select an option

Save allenhwkim/8c52ac84b00a2a697f2bbef3b5a83350 to your computer and use it in GitHub Desktop.
Typescript Parser from string
const ts = require('typescript');
class TypescriptParser {
constructor(code) {
const sourceFile = ts.createSourceFile('temp.ts', code, ts.ScriptTarget.Latest);
this.rootNode = this.getRecursiveFrom(sourceFile, sourceFile);
}
getRecursiveFrom(node, sourceFile) {
const syntaxKind = ts.SyntaxKind[node.kind];
const nodeText = node.getText(sourceFile);
const children = [];
node.forEachChild(child => {
children.push(this.getRecursiveFrom(child, sourceFile));
});
const getFunc = function() { return {get: getFunc} }
const get = function(kind) {
const all = this.children.filter(el => el.syntaxKind === kind);
return all.length === 0 ? {get: getFunc} :
all.length === 1 ? all[0] :
all;
}
return {node, syntaxKind, nodeText, children, get};
}
}
module.exports = TypescriptParser;
// var typescript = `
// @Component({
// selector: 'app-root',
// template: '<div>Example Component</div>',
// styles: [''],
// providers: [FooKlass],
// x: {foo:1, bar:2}
// })
// class X {}
// `;
//
// var parsed = new TypescriptParser(typescript);
// const klassDecorator = {}; // return decorator info as an object {key: value...}
//
// parsed.rootNode.get('ClassDeclaration')
// .get('Decorator')
// .get('CallExpression')
// .get('ObjectLiteralExpression')
// .get('PropertyAssignment')
// .forEach(el => {
// const key = el.children[0].node.escapedText;
// const value = el.children[1].node;
// klassDecorator[key] = value;
// })
//
// console.log(klassDecorator)
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment