Skip to content

Commit

Permalink
fix(app): Directive class inheritance
Browse files Browse the repository at this point in the history
fix #1140
  • Loading branch information
vogloblinsky committed Feb 7, 2022
1 parent 0a15cf6 commit 765f496
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/app/compiler/angular/deps/directive-dep.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export class DirectiveDepFactory {
constructor(private helper: ComponentHelper) {}

public create(file: any, srcFile: any, name: any, props: any, IO: any): IDirectiveDep {
let sourceCode = srcFile.getText();
let hash = crypto.createHash('sha512').update(sourceCode).digest('hex');
let directiveDeps: IDirectiveDep = {
const sourceCode = srcFile.getText();
const hash = crypto.createHash('sha512').update(sourceCode).digest('hex');
const directiveDeps: IDirectiveDep = {
name,
id: 'directive-' + name + '-' + hash,
file: file,
Expand Down Expand Up @@ -41,6 +41,9 @@ export class DirectiveDepFactory {
if (IO.jsdoctags && IO.jsdoctags.length > 0) {
directiveDeps.jsdoctags = IO.jsdoctags[0].tags;
}
if (IO.extends) {
directiveDeps.extends = IO.extends;
}
if (IO.implements && IO.implements.length > 0) {
directiveDeps.implements = IO.implements;
}
Expand Down Expand Up @@ -80,4 +83,5 @@ export interface IDirectiveDep extends IDep {
jsdoctags?: Array<string>;
implements?: any;
accessors?: Object;
extends?: any;
}
9 changes: 9 additions & 0 deletions src/templates/partials/directive.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@
</p>
{{/if}}

{{#if directive.extends}}
<p class="comment">
<h3>{{t "extends" }}</h3>
</p>
<p class="comment">
{{> link-type type=directive.extends }}
</p>
{{/if}}

{{#if directive.implements}}
<p class="comment">
<h3>{{t "implements" }}</h3>
Expand Down
8 changes: 6 additions & 2 deletions src/utils/extends-merger.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class ExtendsMerger {
this.injectables = deps.injectables;
this.directives = deps.directives;

this.components.forEach(component => {
const mergeExtendedProperties = component => {
let ext;
if (typeof component.extends !== 'undefined') {
ext = this.findInDependencies(component.extends);
Expand Down Expand Up @@ -117,7 +117,10 @@ export class ExtendsMerger {
recursiveScanWithInheritance(ext);
}
}
});
};

this.components.forEach(mergeExtendedProperties);
this.directives.forEach(mergeExtendedProperties);

const mergeExtendedClasses = el => {
let ext;
Expand Down Expand Up @@ -151,6 +154,7 @@ export class ExtendsMerger {

this.classes.forEach(mergeExtendedClasses);
this.injectables.forEach(mergeExtendedClasses);
this.directives.forEach(mergeExtendedClasses);

return deps;
}
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/sample-files-extends/src/app/a.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Directive, HostBinding, HostListener, Input } from '@angular/core';

/**
* The a directive
*/
@Directive({
selector: '[a]',
})
export class ADirective {
title: string;

/**
* constructor description
*/
constructor() {}
}
41 changes: 41 additions & 0 deletions test/fixtures/sample-files-extends/src/app/do-nothing.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Directive, HostBinding, HostListener, Input } from '@angular/core';
import { ADirective } from './a.directive';

/**
* This directive does nothing !
*/
@Directive({
selector: '[donothing]',
})
export class DoNothingDirective extends ADirective {
protected popover: string;

/**
* constructor description
*/
constructor() {
console.log('Do nothing directive');
}

/**
* HostBinding description
*/
@HostBinding('style.color') color: string;

/**
* HostListener description 1
*/
@HostListener('mouseup', ['$event.clientX', '$event.clientY'])
onMouseup(mouseX: number, mouseY: number): void {}
/**
* HostListener description 2
*/
@HostListener('mousedown', ['$event.clientX', '$event.clientY'])
onMousedown(mouseX: number, mouseY: number): void {}
/**
* HostListener description 3
*/
@HostListener('focus', ['$event'])
@HostListener('click', ['$event'])
onClick(e: Event): void {}
}
10 changes: 9 additions & 1 deletion test/src/cli/cli-extends.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ describe('CLI simple generation - extends app', () => {
expect(appComponentFile).to.contain('itisme');
});

it('DoNothingDirective extends ADirective', () => {
const file = read(distFolder + '/directives/DoNothingDirective.html');
expect(file).to.contain('Extends');
expect(file).to.contain(
'code><a href="../directives/ADirective.html" target="_self" >ADirective'
);
});

it('MyInitialClass extends SubClassA', () => {
expect(myInitialClassFile).to.contain('meh');
expect(myInitialClassFile).to.contain('myproperty');
Expand All @@ -51,7 +59,7 @@ describe('CLI simple generation - extends app', () => {
});

it('CharactersService extends AbstractService', () => {
let file = read(distFolder + '/injectables/CharactersService.html');
const file = read(distFolder + '/injectables/CharactersService.html');
expect(file).to.contain(
'code><a href="../injectables/AbstractService.html" target="_self" >AbstractService'
);
Expand Down

0 comments on commit 765f496

Please sign in to comment.