Skip to content

Commit 6d0f184

Browse files
refactor: use type inference for inputs wiht default values
Closes ng-bootstrap#288
1 parent d79d9fb commit 6d0f184

File tree

7 files changed

+39
-32
lines changed

7 files changed

+39
-32
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {Directive, Input} from '@angular/core';
2+
3+
@Directive({
4+
selector: '[foo]'
5+
})
6+
export class Foo {
7+
8+
@Input() fooBoolean = false;
9+
@Input() fooNumber = 5;
10+
@Input() fooString = 'bar';
11+
}

misc/api-doc-test-cases/directives-with-tricky-inputs.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {Directive, Input} from '@angular/core';
66
export class Foo {
77
@Input('foo') _foo;
88
@Input() set bar(newVal) {
9-
};
9+
}
10+
1011
@Input('baz') set _baz(newVal) {
11-
};
12+
}
1213
}

misc/api-doc.js

+5-23
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ function getNamesCompareFn(name) {
99
}
1010

1111
class APIDocVisitor {
12-
constructor(fileNames) { this.program = ts.createProgram(fileNames, {}); }
12+
constructor(fileNames) {
13+
this.program = ts.createProgram(fileNames, {});
14+
this.typeChecker = this.program.getTypeChecker(true);
15+
}
1316

1417

1518
visitSourceFile(fileName) {
@@ -105,7 +108,7 @@ class APIDocVisitor {
105108
return {
106109
name: inArgs.length ? inArgs[0].text : property.name.text,
107110
defaultValue: property.initializer ? this.stringifyDefaultValue(property.initializer) : undefined,
108-
type: this.stringifyTypeInfo(property.type),
111+
type: this.typeChecker.typeToString(this.typeChecker.getTypeAtLocation(property)),
109112
description: ts.displayPartsToString(property.symbol.getDocumentationComment())
110113
};
111114
}
@@ -144,27 +147,6 @@ class APIDocVisitor {
144147

145148
return null;
146149
}
147-
148-
stringifyTypeInfo(type) {
149-
// TODO: this is probably covered by some helper method in TS, investigate
150-
151-
if (!type) {
152-
return undefined;
153-
}
154-
155-
switch (type.kind) {
156-
case ts.SyntaxKind.BooleanKeyword:
157-
return 'boolean';
158-
case ts.SyntaxKind.NumberKeyword:
159-
return 'number';
160-
case ts.SyntaxKind.StringKeyword:
161-
return 'string';
162-
case ts.SyntaxKind.UnionType:
163-
return type.types.map((typeNode) => { return this.stringifyTypeInfo(typeNode); }).join(' | ');
164-
default:
165-
return 'unknown';
166-
}
167-
}
168150
}
169151

170152
function parseOutApiDocs(programFiles) {

misc/api-doc.spec.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('APIDocVisitor', function() {
4040

4141
expect(inputDocs[2].name).toBe('foo');
4242
expect(inputDocs[2].defaultValue).toBe('5');
43-
expect(inputDocs[2].type).toBeUndefined();
43+
expect(inputDocs[2].type).toBe('number');
4444
expect(inputDocs[2].description).toBe('Has default value');
4545
});
4646

@@ -54,6 +54,19 @@ describe('APIDocVisitor', function() {
5454
expect(inputDocs[2].defaultValue).toBe('bar');
5555
});
5656

57+
it('should extract inferred types', function() {
58+
var inputDocs = apiDoc(['./misc/api-doc-test-cases/directives-with-inputs-types-to-infer.ts']).Foo.inputs;
59+
60+
expect(inputDocs.length).toBe(3);
61+
62+
expect(inputDocs[0].defaultValue).toBe('false');
63+
expect(inputDocs[0].type).toBe('boolean');
64+
expect(inputDocs[1].defaultValue).toBe('5');
65+
expect(inputDocs[1].type).toBe('number');
66+
expect(inputDocs[2].defaultValue).toBe('bar');
67+
expect(inputDocs[2].type).toBe('string');
68+
});
69+
5770
it('should extract inputs info from setters', function() {
5871
var inputDocs = apiDoc(['./misc/api-doc-test-cases/directives-with-tricky-inputs.ts']).Foo.inputs;
5972

src/accordion/accordion.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ let nextId = 0;
3333
directives: [NgbCollapse]
3434
})
3535
export class NgbPanel {
36-
@Input() disabled: boolean = false;
37-
@Input() id: string = `ngb-panel-${nextId++}`;
38-
@Input() open: boolean = false;
36+
@Input() disabled = false;
37+
@Input() id = `ngb-panel-${nextId++}`;
38+
@Input() open = false;
3939
@Input() title: string;
4040

4141
constructor(@Optional() @Inject(forwardRef(() => NgbAccordion)) private accordion: NgbAccordion) {}

src/alert/alert.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ export class NgbAlert {
2020
* A flag indicating if a given alert can be dismissed (closed) by a user. If this flag is set, a close button (in a
2121
* form of a cross) will be displayed.
2222
*/
23-
@Input() dismissible: boolean = true;
23+
@Input() dismissible = true;
2424
/**
2525
* Alert type (CSS class). Bootstrap 4 recognizes the following types: "success", "info", "warning" and "danger".
2626
*/
27-
@Input() type: string = 'warning';
27+
@Input() type = 'warning';
2828
/**
2929
* An event emitted when the close button is clicked. This event has no payload. Only relevant for dismissible alerts.
3030
*/

src/collapse/collapse.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class NgbCollapse {
1212
/**
1313
* A flag indicating collapsed (true) or open (false) state.
1414
*/
15-
@Input('ngbCollapse') collapsed: boolean = false;
15+
@Input('ngbCollapse') collapsed = false;
1616
}
1717

1818
export const NGB_COLLAPSE_DIRECTIVES = [NgbCollapse];

0 commit comments

Comments
 (0)