node-typescript-parser icon indicating copy to clipboard operation
node-typescript-parser copied to clipboard

How to get parsed data ( key -> value) of Class Decorator

Open knlodha opened this issue 7 years ago • 2 comments

Hi @buehler , I can see that, I am not getting parsed data for class decorator. How can I get this data provided in decorator. I am using it for angular files

e.g.
import { Component } from '@angular/core';

@Component({ selector: 'app-root', template: <h1>{{title}}</h1> <h2>My favorite hero is: {{myHero}}</h2> }) export class AppComponent { title = 'Tour of Heroes'; myHero = 'Windstorm'; }

Here , I need selector and template values from decorator. Is there any way / configuration to get this data using parseSource or parseFile ?

knlodha avatar Sep 11 '18 11:09 knlodha

Hey @knlodha Right now, there is no possibility to get decorators (or preciser input values that are given into a decorator).

What you could do: search for the classes (and therefore the files) that have components in them and then use a string search to parse those elements.

buehler avatar Sep 18 '18 05:09 buehler

Hi @buehler , Thanks a lot for the inputs. Currently, I am doing exactly what you suggested ( string parsing ). However, in certain scenarios like below,

Angular Code: const routes: Routes = [ { path: '', redirectTo: '/dashboard', pathMatch: 'full' }, { path: 'dashboard', component: DashboardComponent }, { path: 'detail/:id', component: HeroDetailComponent }, { path: 'heroes', component: HeroesComponent } ];

where I need individual objects like the one highlighted above, string parsing becomes more complex. Also, I can not covert the parsed string into Javascript object using JSON.parse as it will not find the individual references like DashboardComponent in this case.

Why I can't get the values of variables like routes using typescript parser. I can see, I only get the datatype. Is there any other way to get such information.

Also, do we have something like esprima ( http://esprima.org/ )for typescript code.

knlodha avatar Sep 19 '18 08:09 knlodha