@jamesbenrobb/ngx-ts-ast-parser
v0.0.5
Published
Angular specific declarations for @jamesbenrobb/ts-ast-parser
Downloads
13
Readme
Angular declarations for @jamesbenrobb/ts-ast-parser
Adds Angular specific properties to the output.
1.
Example file to parse.
Given the following file path/to/my-component.ts
import {
Component, EventEmitter, inject, Inject, input, Input, Output, output,
OnInit, OnChanges, AfterViewInit, OnDestroy, SimpleChanges
} from '@angular/core';
import {outputFromObservable} from "@angular/core/rxjs-interop";
import {Subject} from "rxjs";
class SomeService {}
class SomeOtherService {}
@Component({
selector: 'my-component',
standalone: true,
imports: [],
templateUrl: './my-component.component.html',
styleUrl: './my-component.component.css'
})
export class MyComponent implements OnInit, OnChanges, AfterViewInit, OnDestroy {
@Input() firstName?: string;
@Input({alias: 'lastName'}) surname?: string;
@Input({required: true}) age?: number;
houseNumber = input<number>();
streetName = input<string>();
city = input.required<string>();
country = input<string>('UK', {alias: 'countryCode'});
@Output()
firstNameUpdated = new EventEmitter<string>();
@Output('lastNameUpdated')
surnameUpdated = new EventEmitter<string>();
@Output()
ageUpdated = new EventEmitter<number>();
houseNumberUpdated = output<number>();
streetNameUpdated = output<string>();
cityUpdated = output<string>();
countryUpdated = output<string>({alias: 'countryCodeUpdated'});
readonly #initialised = new Subject<boolean>();
isInitialised = outputFromObservable(this.#initialised.asObservable());
readonly #someService = inject(SomeService);
constructor(@Inject('someToken') someToken: string, someOtherService: SomeOtherService) {
}
ngOnInit() {
this.#initialised.next(true);
}
ngOnChanges(changes: SimpleChanges) {
}
ngAfterViewInit() {
}
ngOnDestroy() {
}
someNonLifecycleMethod(arg: string): void {
}
}
2.
Calling the parse
function
import {
CommonPathHandler,
NgPathHandler,
NodeModulesPathHandler,
RxjsPathHandler,
Parser,
parse
} from "@jamesbenrobb/ts-ast-parser";
import {ngDeclarationDefinitionMap} from "@jamesbenrobb/ngx-ts-ast-parser";
const pathHandlers = [
new CommonPathHandler(),
new NodeModulesPathHandler(),
new NgPathHandler(),
new RxjsPathHandler()
]
const sourcePath = 'path/to/my-component.ts';
const parser = new Parser(ngDeclarationDefinitionMap);
const source = parse(
sourcePath,
parser,
pathHandlers, {
walk: false,
debug: false
}
);
console.log(source);
3.
Example output from parse
The following Angular specific properties are added:
isUI
injectedDependencies
inputs
outputs
lifeCycleMethods
{
"fileName": "my-component.component.ts",
"path": "/path/to",
"imports": [
{
"name": "Component",
"module": "@angular/core",
"resolvedModulePath": "@angular/core",
"convertedModulePath": "https://angular.dev/api/core/Component"
},
{
"name": "EventEmitter",
"module": "@angular/core",
"resolvedModulePath": "@angular/core",
"convertedModulePath": "https://angular.dev/api/core/EventEmitter"
},
{
"name": "inject",
"module": "@angular/core",
"resolvedModulePath": "@angular/core",
"convertedModulePath": "https://angular.dev/api/core/inject"
},
{
"name": "Inject",
"module": "@angular/core",
"resolvedModulePath": "@angular/core",
"convertedModulePath": "https://angular.dev/api/core/Inject"
},
{
"name": "input",
"module": "@angular/core",
"resolvedModulePath": "@angular/core",
"convertedModulePath": "https://angular.dev/api/core/input"
},
{
"name": "Input",
"module": "@angular/core",
"resolvedModulePath": "@angular/core",
"convertedModulePath": "https://angular.dev/api/core/Input"
},
{
"name": "Output",
"module": "@angular/core",
"resolvedModulePath": "@angular/core",
"convertedModulePath": "https://angular.dev/api/core/Output"
},
{
"name": "output",
"module": "@angular/core",
"resolvedModulePath": "@angular/core",
"convertedModulePath": "https://angular.dev/api/core/output"
},
{
"name": "OnInit",
"module": "@angular/core",
"resolvedModulePath": "@angular/core",
"convertedModulePath": "https://angular.dev/api/core/OnInit"
},
{
"name": "OnChanges",
"module": "@angular/core",
"resolvedModulePath": "@angular/core",
"convertedModulePath": "https://angular.dev/api/core/OnChanges"
},
{
"name": "AfterViewInit",
"module": "@angular/core",
"resolvedModulePath": "@angular/core",
"convertedModulePath": "https://angular.dev/api/core/AfterViewInit"
},
{
"name": "OnDestroy",
"module": "@angular/core",
"resolvedModulePath": "@angular/core",
"convertedModulePath": "https://angular.dev/api/core/OnDestroy"
},
{
"name": "SimpleChanges",
"module": "@angular/core",
"resolvedModulePath": "@angular/core",
"convertedModulePath": "https://angular.dev/api/core/SimpleChanges"
},
{
"name": "outputFromObservable",
"module": "@angular/core/rxjs-interop",
"resolvedModulePath": "@angular/core/rxjs-interop",
"convertedModulePath": "https://angular.dev/api/core/rxjs-interop/outputFromObservable"
},
{
"name": "Subject",
"module": "rxjs",
"resolvedModulePath": "rxjs",
"convertedModulePath": "https://rxjs.dev/api/index/class/Subject"
}
],
"exports": [
{
"kind": "ClassDeclaration",
"name": "MyComponent",
"members": [
{
"kind": "PropertyDeclaration",
"name": "#initialised",
"initializedValue": {
"expression": "Subject",
"typeArguments": [
"boolean"
],
"arguments": [],
"signature": "new Subject()"
},
"modifiers": {
"keywords": [
"readonly"
]
},
"access": "private",
"signature": "readonly #initialised = new Subject()"
},
{
"kind": "PropertyDeclaration",
"name": "#someService",
"initializedValue": {
"expression": "inject",
"arguments": [
"SomeService"
],
"signature": "inject(SomeService)"
},
"modifiers": {
"keywords": [
"readonly"
]
},
"access": "private",
"injectedDependency": {
"type": "SomeService",
"text": "SomeService",
"args": []
},
"signature": "readonly #someService = inject(SomeService)"
},
{
"kind": "Constructor",
"parameters": [
{
"kind": "Parameter",
"name": "someToken",
"type": "string",
"modifiers": {
"decorators": [
{
"type": "Inject",
"metadata": "someToken",
"signature": "@Inject('someToken')"
}
]
},
"signature": "someToken: string"
},
{
"kind": "Parameter",
"name": "someOtherService",
"type": {
"kind": "TypeReference",
"name": "SomeOtherService",
"signature": "SomeOtherService"
},
"signature": "someOtherService: SomeOtherService"
}
],
"injectedDependencies": [
{
"type": "string",
"text": "someToken",
"args": []
},
{
"type": "SomeOtherService",
"text": "SomeOtherService",
"args": []
}
]
}
],
"heritageClauses": [
{
"kind": "HeritageClause",
"token": "implements",
"types": [
{
"kind": "ExpressionWithTypeArguments",
"expression": "OnInit",
"resolvedPath": "https://angular.dev/api/core/OnInit",
"signature": "OnInit"
},
{
"kind": "ExpressionWithTypeArguments",
"expression": "OnChanges",
"resolvedPath": "https://angular.dev/api/core/OnChanges",
"signature": "OnChanges"
},
{
"kind": "ExpressionWithTypeArguments",
"expression": "AfterViewInit",
"resolvedPath": "https://angular.dev/api/core/AfterViewInit",
"signature": "AfterViewInit"
},
{
"kind": "ExpressionWithTypeArguments",
"expression": "OnDestroy",
"resolvedPath": "https://angular.dev/api/core/OnDestroy",
"signature": "OnDestroy"
}
]
}
],
"modifiers": {
"decorators": [
{
"type": "Component",
"metadata": {
"selector": "my-component",
"standalone": true,
"imports": [],
"templateUrl": "./my-component.component.html",
"styleUrl": "./my-component.component.css"
},
"signature": "@Component({selector: my-component, standalone: true, imports: , templateUrl: ./my-component.component.html, styleUrl: ./my-component.component.css})"
}
],
"keywords": [
"export"
]
},
"methods": [
{
"kind": "MethodDeclaration",
"name": "someNonLifecycleMethod",
"returnType": "void",
"parameters": [
{
"kind": "Parameter",
"name": "arg",
"type": "string",
"signature": "arg: string"
}
],
"access": "public",
"signature": "someNonLifecycleMethod(arg: string): void"
}
],
"isUI": true,
"injectedDependencies": [
{
"type": "SomeService",
"text": "SomeService",
"args": []
},
{
"type": "string",
"text": "someToken",
"args": []
},
{
"type": "SomeOtherService",
"text": "SomeOtherService",
"args": []
}
],
"inputs": [
{
"kind": "PropertyDeclaration",
"name": "firstName",
"type": "string",
"optional": true,
"modifiers": {
"decorators": [
{
"type": "Input",
"signature": "@Input()"
}
]
},
"access": "public",
"isInput": true,
"signature": "@Input()\nfirstName?: string"
},
{
"kind": "PropertyDeclaration",
"name": "surname",
"type": "string",
"optional": true,
"modifiers": {
"decorators": [
{
"type": "Input",
"metadata": {
"alias": "lastName"
},
"signature": "@Input({alias: lastName})"
}
]
},
"access": "public",
"isInput": true,
"signature": "@Input({alias: lastName})\nsurname?: string"
},
{
"kind": "PropertyDeclaration",
"name": "age",
"type": "number",
"optional": true,
"modifiers": {
"decorators": [
{
"type": "Input",
"metadata": {
"required": true
},
"signature": "@Input({required: true})"
}
]
},
"access": "public",
"isInput": true,
"signature": "@Input({required: true})\nage?: number"
},
{
"kind": "PropertyDeclaration",
"name": "houseNumber",
"initializedValue": {
"expression": "input",
"typeArguments": [
"number"
],
"arguments": [],
"signature": "input<number>()"
},
"access": "public",
"isInput": true,
"signature": "houseNumber = input<number>()"
},
{
"kind": "PropertyDeclaration",
"name": "streetName",
"initializedValue": {
"expression": "input",
"typeArguments": [
"string"
],
"arguments": [],
"signature": "input<string>()"
},
"access": "public",
"isInput": true,
"signature": "streetName = input<string>()"
},
{
"kind": "PropertyDeclaration",
"name": "city",
"initializedValue": {
"expression": {
"name": "required",
"expression": "input",
"signature": "input.required"
},
"typeArguments": [
"string"
],
"arguments": [],
"signature": "input.required<string>()"
},
"access": "public",
"isInput": true,
"isRequired": true,
"signature": "city = input.required<string>()"
},
{
"kind": "PropertyDeclaration",
"name": "country",
"initializedValue": {
"expression": "input",
"typeArguments": [
"string"
],
"arguments": [
"UK",
{
"alias": "countryCode"
}
],
"signature": "input<string>(UK, { alias: countryCode })"
},
"access": "public",
"isInput": true,
"signature": "country = input<string>(UK, { alias: countryCode })"
}
],
"outputs": [
{
"kind": "PropertyDeclaration",
"name": "firstNameUpdated",
"initializedValue": {
"expression": "EventEmitter",
"typeArguments": [
"string"
],
"arguments": [],
"signature": "new EventEmitter()"
},
"modifiers": {
"decorators": [
{
"type": "Output",
"signature": "@Output()"
}
]
},
"access": "public",
"isOutput": true,
"signature": "@Output()\nfirstNameUpdated = new EventEmitter()"
},
{
"kind": "PropertyDeclaration",
"name": "surnameUpdated",
"initializedValue": {
"expression": "EventEmitter",
"typeArguments": [
"string"
],
"arguments": [],
"signature": "new EventEmitter()"
},
"modifiers": {
"decorators": [
{
"type": "Output",
"metadata": "lastNameUpdated",
"signature": "@Output('lastNameUpdated')"
}
]
},
"access": "public",
"isOutput": true,
"signature": "@Output('lastNameUpdated')\nsurnameUpdated = new EventEmitter()"
},
{
"kind": "PropertyDeclaration",
"name": "ageUpdated",
"initializedValue": {
"expression": "EventEmitter",
"typeArguments": [
"number"
],
"arguments": [],
"signature": "new EventEmitter()"
},
"modifiers": {
"decorators": [
{
"type": "Output",
"signature": "@Output()"
}
]
},
"access": "public",
"isOutput": true,
"signature": "@Output()\nageUpdated = new EventEmitter()"
},
{
"kind": "PropertyDeclaration",
"name": "houseNumberUpdated",
"initializedValue": {
"expression": "output",
"typeArguments": [
"number"
],
"arguments": [],
"signature": "output<number>()"
},
"access": "public",
"isOutput": true,
"signature": "houseNumberUpdated = output<number>()"
},
{
"kind": "PropertyDeclaration",
"name": "streetNameUpdated",
"initializedValue": {
"expression": "output",
"typeArguments": [
"string"
],
"arguments": [],
"signature": "output<string>()"
},
"access": "public",
"isOutput": true,
"signature": "streetNameUpdated = output<string>()"
},
{
"kind": "PropertyDeclaration",
"name": "cityUpdated",
"initializedValue": {
"expression": "output",
"typeArguments": [
"string"
],
"arguments": [],
"signature": "output<string>()"
},
"access": "public",
"isOutput": true,
"signature": "cityUpdated = output<string>()"
},
{
"kind": "PropertyDeclaration",
"name": "countryUpdated",
"initializedValue": {
"expression": "output",
"typeArguments": [
"string"
],
"arguments": [
{
"alias": "countryCodeUpdated"
}
],
"signature": "output<string>({ alias: countryCodeUpdated })"
},
"access": "public",
"isOutput": true,
"signature": "countryUpdated = output<string>({ alias: countryCodeUpdated })"
},
{
"kind": "PropertyDeclaration",
"name": "isInitialised",
"initializedValue": {
"expression": "outputFromObservable",
"arguments": [
{
"expression": {
"name": "asObservable",
"expression": {
"name": "#initialised",
"expression": "this",
"signature": "this.#initialised"
},
"signature": "this.#initialised.asObservable"
},
"arguments": [],
"signature": "this.#initialised.asObservable()"
}
],
"signature": "outputFromObservable(this.#initialised.asObservable())"
},
"access": "public",
"isOutput": true,
"signature": "isInitialised = outputFromObservable(this.#initialised.asObservable())"
}
],
"lifeCycleMethods": [
{
"kind": "MethodDeclaration",
"name": "ngOnInit",
"parameters": [],
"access": "public",
"returnType": "void",
"isLifeCycleMethod": true,
"signature": "ngOnInit(): void"
},
{
"kind": "MethodDeclaration",
"name": "ngOnChanges",
"parameters": [
{
"kind": "Parameter",
"name": "changes",
"type": {
"kind": "TypeReference",
"name": "SimpleChanges",
"resolvedPath": "https://angular.dev/api/core/SimpleChanges",
"signature": "SimpleChanges"
},
"signature": "changes: SimpleChanges"
}
],
"access": "public",
"returnType": "void",
"isLifeCycleMethod": true,
"signature": "ngOnChanges(changes: SimpleChanges): void"
},
{
"kind": "MethodDeclaration",
"name": "ngAfterViewInit",
"parameters": [],
"access": "public",
"returnType": "void",
"isLifeCycleMethod": true,
"signature": "ngAfterViewInit(): void"
},
{
"kind": "MethodDeclaration",
"name": "ngOnDestroy",
"parameters": [],
"access": "public",
"returnType": "void",
"isLifeCycleMethod": true,
"signature": "ngOnDestroy(): void"
}
]
}
]
}