Skip to content

Commit

Permalink
Fix bug in code that processes API properties
Browse files Browse the repository at this point in the history
  • Loading branch information
zelliott committed May 17, 2022
1 parent 0e2502e commit fc4ec11
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
19 changes: 13 additions & 6 deletions apps/api-extractor/src/generators/ApiModelGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -824,13 +824,20 @@ export class ApiModelGenerator {
const nodesToCapture: IExcerptBuilderNodeToCapture[] = [];

const propertyTypeTokenRange: IExcerptTokenRange = ExcerptBuilder.createEmptyTokenRange();
let propertyTypeNode: ts.TypeNode | undefined;

if (
ts.isPropertyDeclaration(astDeclaration.declaration) ||
ts.isGetAccessorDeclaration(astDeclaration.declaration)
) {
propertyTypeNode = astDeclaration.declaration.type;
}

if (ts.isSetAccessorDeclaration(astDeclaration.declaration)) {
// Note that TypeScript always reports an error if a setter does not have exactly one parameter.
propertyTypeNode = astDeclaration.declaration.parameters[0].type;
}

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

const excerptTokens: IExcerptToken[] = this._buildExcerptTokens(astDeclaration, nodesToCapture);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,52 @@
},
"isStatic": false
},
{
"kind": "Property",
"canonicalReference": "api-extractor-scenarios!SimpleClass#someReadonlyProp:member",
"docComment": "",
"excerptTokens": [
{
"kind": "Content",
"text": "readonly someReadonlyProp = 5;"
}
],
"isOptional": false,
"releaseTag": "Public",
"name": "someReadonlyProp",
"propertyTypeTokenRange": {
"startIndex": 0,
"endIndex": 0
},
"isStatic": false
},
{
"kind": "Property",
"canonicalReference": "api-extractor-scenarios!SimpleClass#someReadonlyPropWithType:member",
"docComment": "",
"excerptTokens": [
{
"kind": "Content",
"text": "readonly someReadonlyPropWithType: "
},
{
"kind": "Content",
"text": "number"
},
{
"kind": "Content",
"text": ";"
}
],
"isOptional": false,
"releaseTag": "Public",
"name": "someReadonlyPropWithType",
"propertyTypeTokenRange": {
"startIndex": 1,
"endIndex": 2
},
"isStatic": false
},
{
"kind": "Property",
"canonicalReference": "api-extractor-scenarios!SimpleClass#writeableProperty:member",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ export class SimpleClass {
// (undocumented)
get readonlyProperty(): string;
// (undocumented)
readonly someReadonlyProp = 5;
// (undocumented)
readonly someReadonlyPropWithType: number;
// (undocumented)
get writeableProperty(): string;
set writeableProperty(value: string);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export declare class SimpleClass {
get readonlyProperty(): string;
get writeableProperty(): string;
set writeableProperty(value: string);
readonly someReadonlyProp = 5;
readonly someReadonlyPropWithType: number;
}

/** @public */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ export class SimpleClass {
return 'hello';
}
public set writeableProperty(value: string) {}

readonly someReadonlyProp = 5;
readonly someReadonlyPropWithType: number = 5;
}

0 comments on commit fc4ec11

Please sign in to comment.