Skip to content

Commit fc4ec11

Browse files
committed
Fix bug in code that processes API properties
1 parent 0e2502e commit fc4ec11

File tree

5 files changed

+68
-6
lines changed

5 files changed

+68
-6
lines changed

apps/api-extractor/src/generators/ApiModelGenerator.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -824,13 +824,20 @@ export class ApiModelGenerator {
824824
const nodesToCapture: IExcerptBuilderNodeToCapture[] = [];
825825

826826
const propertyTypeTokenRange: IExcerptTokenRange = ExcerptBuilder.createEmptyTokenRange();
827+
let propertyTypeNode: ts.TypeNode | undefined;
828+
829+
if (
830+
ts.isPropertyDeclaration(astDeclaration.declaration) ||
831+
ts.isGetAccessorDeclaration(astDeclaration.declaration)
832+
) {
833+
propertyTypeNode = astDeclaration.declaration.type;
834+
}
835+
836+
if (ts.isSetAccessorDeclaration(astDeclaration.declaration)) {
837+
// Note that TypeScript always reports an error if a setter does not have exactly one parameter.
838+
propertyTypeNode = astDeclaration.declaration.parameters[0].type;
839+
}
827840

828-
// If the property declaration's type is `undefined`, then we're processing a setter with no corresponding
829-
// getter. Use the parameter type instead (note that TypeScript always reports an error if a setter
830-
// does not have exactly one parameter).
831-
const propertyTypeNode: ts.TypeNode | undefined =
832-
(astDeclaration.declaration as ts.PropertyDeclaration | ts.GetAccessorDeclaration).type ||
833-
(astDeclaration.declaration as ts.SetAccessorDeclaration).parameters[0].type;
834841
nodesToCapture.push({ node: propertyTypeNode, tokenRange: propertyTypeTokenRange });
835842

836843
const excerptTokens: IExcerptToken[] = this._buildExcerptTokens(astDeclaration, nodesToCapture);

build-tests/api-extractor-scenarios/etc/test-outputs/apiItemKinds/api-extractor-scenarios.api.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,52 @@
685685
},
686686
"isStatic": false
687687
},
688+
{
689+
"kind": "Property",
690+
"canonicalReference": "api-extractor-scenarios!SimpleClass#someReadonlyProp:member",
691+
"docComment": "",
692+
"excerptTokens": [
693+
{
694+
"kind": "Content",
695+
"text": "readonly someReadonlyProp = 5;"
696+
}
697+
],
698+
"isOptional": false,
699+
"releaseTag": "Public",
700+
"name": "someReadonlyProp",
701+
"propertyTypeTokenRange": {
702+
"startIndex": 0,
703+
"endIndex": 0
704+
},
705+
"isStatic": false
706+
},
707+
{
708+
"kind": "Property",
709+
"canonicalReference": "api-extractor-scenarios!SimpleClass#someReadonlyPropWithType:member",
710+
"docComment": "",
711+
"excerptTokens": [
712+
{
713+
"kind": "Content",
714+
"text": "readonly someReadonlyPropWithType: "
715+
},
716+
{
717+
"kind": "Content",
718+
"text": "number"
719+
},
720+
{
721+
"kind": "Content",
722+
"text": ";"
723+
}
724+
],
725+
"isOptional": false,
726+
"releaseTag": "Public",
727+
"name": "someReadonlyPropWithType",
728+
"propertyTypeTokenRange": {
729+
"startIndex": 1,
730+
"endIndex": 2
731+
},
732+
"isStatic": false
733+
},
688734
{
689735
"kind": "Property",
690736
"canonicalReference": "api-extractor-scenarios!SimpleClass#writeableProperty:member",

build-tests/api-extractor-scenarios/etc/test-outputs/apiItemKinds/api-extractor-scenarios.api.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ export class SimpleClass {
6262
// (undocumented)
6363
get readonlyProperty(): string;
6464
// (undocumented)
65+
readonly someReadonlyProp = 5;
66+
// (undocumented)
67+
readonly someReadonlyPropWithType: number;
68+
// (undocumented)
6569
get writeableProperty(): string;
6670
set writeableProperty(value: string);
6771
}

build-tests/api-extractor-scenarios/etc/test-outputs/apiItemKinds/rollup.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ export declare class SimpleClass {
5858
get readonlyProperty(): string;
5959
get writeableProperty(): string;
6060
set writeableProperty(value: string);
61+
readonly someReadonlyProp = 5;
62+
readonly someReadonlyPropWithType: number;
6163
}
6264

6365
/** @public */

build-tests/api-extractor-scenarios/src/apiItemKinds/classes.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,7 @@ export class SimpleClass {
2020
return 'hello';
2121
}
2222
public set writeableProperty(value: string) {}
23+
24+
readonly someReadonlyProp = 5;
25+
readonly someReadonlyPropWithType: number = 5;
2326
}

0 commit comments

Comments
 (0)