From bace09b6a5f4cf5cbf66b37be8916c5cd1ca9856 Mon Sep 17 00:00:00 2001 From: kreuzerk Date: Tue, 16 Jul 2024 13:29:02 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20parse=20public=20methods?= =?UTF-8?q?=20for=20directives?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bin/ng-parsel.bin.ts | 2 ++ src/parser/directive/directive.model.ts | 2 ++ src/parser/directive/directive.parser.spec.ts | 2 ++ src/parser/directive/directive.parser.ts | 2 ++ test-spa/src/app/tooltip.directive.ts | 32 ++++++++++++------- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/bin/ng-parsel.bin.ts b/src/bin/ng-parsel.bin.ts index 6278939..02d01d9 100644 --- a/src/bin/ng-parsel.bin.ts +++ b/src/bin/ng-parsel.bin.ts @@ -51,6 +51,8 @@ program parse(mergeOptionalConfigWithDefaults(configObject.config)); } else { console.log(chalk.cyan(`ng-parsel: no configuration found. CLI arguments will be used.`)); + options.src = './test-spa'; + parse(mergeOptionalConfigWithDefaults(options)); } diff --git a/src/parser/directive/directive.model.ts b/src/parser/directive/directive.model.ts index 4fb6fea..69585c1 100644 --- a/src/parser/directive/directive.model.ts +++ b/src/parser/directive/directive.model.ts @@ -1,5 +1,6 @@ import { NgParselOutput } from '../shared/model/types.model.js'; import { NgParselFieldDecorator } from '../shared/model/decorator.model.js'; +import { NgParselMethod } from '../shared/model/method.model.js'; export interface NgParselDirective extends NgParselOutput { className: string; @@ -8,4 +9,5 @@ export interface NgParselDirective extends NgParselOutput { implementation: string; inputs: NgParselFieldDecorator[]; outputs: NgParselFieldDecorator[]; + methodsPublicExplicit: NgParselMethod[]; } diff --git a/src/parser/directive/directive.parser.spec.ts b/src/parser/directive/directive.parser.spec.ts index a45bc71..49dfd04 100644 --- a/src/parser/directive/directive.parser.spec.ts +++ b/src/parser/directive/directive.parser.spec.ts @@ -48,6 +48,7 @@ describe('DirectiveParser', () => { }, ], implementation, + methodsPublicExplicit: [], }; jest.spyOn(fs, 'readFileSync').mockReturnValue(implementation); @@ -93,6 +94,7 @@ describe('DirectiveParser', () => { }, ], implementation, + methodsPublicExplicit: [], }; jest.spyOn(fs, 'readFileSync').mockReturnValue(implementation); diff --git a/src/parser/directive/directive.parser.ts b/src/parser/directive/directive.parser.ts index d535d50..97d3d05 100644 --- a/src/parser/directive/directive.parser.ts +++ b/src/parser/directive/directive.parser.ts @@ -7,6 +7,7 @@ import { getDecoratorProperties } from '../shared/parser/decorator.parser.js'; import { parseInputsAndOutputs } from '../shared/parser/field-decorator.parser.js'; import { NgParselDirective } from './directive.model.js'; +import { parseExplicitPublicMethods } from '../shared/parser/method.parser.js'; export function parseDirective(ast: ts.SourceFile, directiveFilePath: string): NgParselDirective { const directiveDecorators = getDecoratorProperties(ast); @@ -24,5 +25,6 @@ export function parseDirective(ast: ts.SourceFile, directiveFilePath: string): N implementation: directiveImplementation, inputs: inputsAndOutputs.inputs, outputs: inputsAndOutputs.outputs, + methodsPublicExplicit: parseExplicitPublicMethods(ast), }; } diff --git a/test-spa/src/app/tooltip.directive.ts b/test-spa/src/app/tooltip.directive.ts index 9d9e645..6475382 100644 --- a/test-spa/src/app/tooltip.directive.ts +++ b/test-spa/src/app/tooltip.directive.ts @@ -1,20 +1,21 @@ -import {Directive, ElementRef, HostListener, Input, OnDestroy} from "@angular/core"; +import { Directive, ElementRef, HostListener, Input, OnDestroy } from '@angular/core'; @Directive({ - selector: '[tooltip]' + selector: '[tooltip]', }) export class TooltipDirective implements OnDestroy { - @Input() tooltip = ''; // The text for the tooltip to display @Input() delay? = 190; // Optional delay input, in ms private myPopup: any; private timer: any; - constructor(private el: ElementRef) { } + constructor(private el: ElementRef) {} ngOnDestroy(): void { - if (this.myPopup) { this.myPopup.remove() } + if (this.myPopup) { + this.myPopup.remove(); + } } @HostListener('mouseenter') onMouseEnter() { @@ -22,28 +23,35 @@ export class TooltipDirective implements OnDestroy { let x = this.el.nativeElement.getBoundingClientRect().left + this.el.nativeElement.offsetWidth / 2; // Get the middle of the element let y = this.el.nativeElement.getBoundingClientRect().top + this.el.nativeElement.offsetHeight + 6; // Get the bottom of the element, plus a little extra this.createTooltipPopup(x, y); - }, this.delay) + }, this.delay); } @HostListener('mousedown') dragstart() { console.log('Drag start'); if (this.timer) clearTimeout(this.timer); - if (this.myPopup) { this.myPopup.remove() } + if (this.myPopup) { + this.myPopup.remove(); + } } @HostListener('mouseleave') onMouseLeave() { if (this.timer) clearTimeout(this.timer); - if (this.myPopup) { this.myPopup.remove() } + if (this.myPopup) { + this.myPopup.remove(); + } + } + + public mySuperMethod(): string { + return 'This method is awesome'; } private createTooltipPopup(x: number, y: number) { let popup = document.createElement('div'); popup.innerHTML = this.tooltip; - popup.setAttribute("class", "tooltip-container"); - popup.style.top = y.toString() + "px"; - popup.style.left = x.toString() + "px"; + popup.setAttribute('class', 'tooltip-container'); + popup.style.top = y.toString() + 'px'; + popup.style.left = x.toString() + 'px'; document.body.appendChild(popup); this.myPopup = popup; } - }