buehler/node-typescript-parser

docs: user guide

Opened this issue · 6 comments

I'm using this parser within a yeoman generator to parse angular 4 services, components, along with their usual decorators, but I'm finding it time consuming learning how to best drive your parser interface relying solely on deep diving into your code base, unit tests and the various reference style html docs.

I'd much appreciate any links you might have to additional material, user guide style docs, tutorials, extensive code samples and anything else to help. Cheers guys, a nice project and very useful, thanks for your efforts.

Hey @craigryan
Thanks for reaching out! By now, I don't have any more documentation, but I'd be happy to provide some! Could you describe - in a little more specific way - what resources would be helpful?

Then I can take the time and documentate the stuff you need.

Cheers

Something I'd find useful is a quick FAQ, specifically to the question "Why would I use this and not the TypeScript Compiler API?" (see https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API)

Thanks for responding Christoph,

I specifically need a guide on how best to examine a typescript source file to determine what imports it contains, what (angular) decorators it has (@Injectable for services, @component for components, @input fields etc), what private constructor params are passed in, all public methods and their params, basic logic to scan blocks of code within a method to try locate some commonly used statements I'm looking for (eg calls made to this.http.get or maybe this.ngRedux.dispatch).

Example of what isn't clear to me, say when looking for @Injectable where to look in the parsed data ('usages' contains it, but along with a lot of other identifiers?), also code samples of how to iterate through parsed data for the various items of interest above. Hope that's clear, if not let me know.

What follows is some sample code of some methods I am implementing to examine the parsed source, consider it pseudo code.

const NamedImport = require('typescript-parser/imports/NamedImport');
... etc

class Example {

lookForServicesAndImports() {
  let parsed = tparser.parseFile(tsSourceFile, tsRoot).then((p) => {
    this.parsed = p;
    return this.parsed;
  });

  for (var u of parsed.usages) {
	if (u === 'Injectable') {
	  console.log('hey I have an Injectable ng service!!');
	}
  }

  for (let imp of parsed.imports) {
    if (imp instanceof NamedImport.NamedImport) {
      this.namedImport(imp, t, mods);
    } else if (imp instanceof StringImport.StringImport) {
      ... etc
    }
  }
}

namedImport(imp, t, mods) {
  const mod = imp.libraryName;
  const specs = this.specifiers(imp.specifiers);
 // do something with a named import
}
... etc

Have you had a chance to make any progress on guide material? I've managed to progress further by console dumps and have a better picture of navigating declarations but now need to parse block statements for specific calls like this.http.get within class methods and from what I can see the parser doesn't expose this information so hoping to get some guide on how I might be able to do this.

HI. I have a question. I'm doing poor man swagger with code generator and noticed that there is properties on InterfaceDeclaration buy nothing in TypeAliasDeclaration except (start,end). How can i get type declaration on node itself ? Or i should use ts api for that purpose ?

Hey @enomado
Actually the real type declaration (aka properties and derived types) are not part of this parser. For this information, you should use the typescript compiler and use it to get the actual types of your file.

I guess. The type alias declaration is only for recognizing types, not the types that they alias.