Skip to content

Commit

Permalink
feat(app): add support for inferred input types
Browse files Browse the repository at this point in the history
This commit adds support to extract inferred types on Angular
input attributes. E.g. `@Input() name = ''`

Closes compodoc#896
  • Loading branch information
fh1ch committed Oct 18, 2020
1 parent fe9cd04 commit 11c4a68
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/app/compiler/angular/deps/helpers/class-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,19 @@ export class ClassHelper {
}
}
}
// Try to get inferred type
if (property.symbol) {
let symbol: ts.Symbol = property.symbol;
if (symbol.valueDeclaration) {
let symbolType = this.typeChecker.getTypeOfSymbolAtLocation(
symbol,
symbol.valueDeclaration
);
if (symbolType) {
_return.type = this.typeChecker.typeToString(symbolType);
}
}
}
}
if (property.kind === SyntaxKind.SetAccessor) {
// For setter accessor, find type in first parameter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<p>
This application is a <a [routerLink]="['todomvc']">TodoMVC</a> example written using Angular 2, with code documented, and ready for <a [routerLink]="['compodoc']">compodoc</a>.
This application is a <a [routerLink]="['todomvc']">TodoMVC</a> example written using {{angularVersion}}, with code documented, and ready for <a [routerLink]="['compodoc']">compodoc</a>.
</p>
<router-outlet></router-outlet>
7 changes: 6 additions & 1 deletion test/fixtures/todomvc-ng2/src/app/about/about.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, HostListener } from '@angular/core';
import { Component, HostListener, Input } from '@angular/core';

import { Subscription } from 'rxjs/Subscription';

Expand Down Expand Up @@ -31,6 +31,11 @@ export class AboutComponent {
@HostListener('mouseup', ['$event.clientX', '$event.clientY'])
onMouseup(mouseX: number, mouseY: number): void {}

/**
* Inherited type of Angular Version
*/
@Input() public angularVersion = 'Angular 2';

chartOptions: Highcharts.Options = {
colors: [
'#7cb5ec',
Expand Down
8 changes: 7 additions & 1 deletion test/src/cli/cli-generation-big-app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,18 @@ describe('CLI simple generation - big app', () => {
expect(todoStoreFile).to.contain('code><a href="../classes/Todo.html" target="_self" >To');
});

it('should have inherreturn type', () => {
it('should have inherit return type', () => {
expect(todoClassFile).to.contain(
'code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/number"'
);
});

it('should have inherit input type', () => {
expect(aboutComponentFile).to.contain(
'code><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/string"'
);
});

it('should support simple class with custom decorator', () => {
expect(tidiClassFile).to.contain('completed</b>');
});
Expand Down

0 comments on commit 11c4a68

Please sign in to comment.