Skip to content

Commit

Permalink
Merge pull request #3396 from zelliott/handle-setters
Browse files Browse the repository at this point in the history
[api-extractor] Generate documentation for setters without getters
  • Loading branch information
octogonz committed May 14, 2022
2 parents e9a2b58 + ca2e07a commit 5f56cf6
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 5 deletions.
16 changes: 12 additions & 4 deletions apps/api-extractor/src/generators/ApiModelGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ export class ApiModelGenerator {
this._processApiProperty(astDeclaration, exportedName, parentApiItem);
break;

case ts.SyntaxKind.SetAccessor:
this._processApiProperty(astDeclaration, exportedName, parentApiItem);
break;

case ts.SyntaxKind.IndexSignature:
this._processApiIndexSignature(astDeclaration, exportedName, parentApiItem);
break;
Expand Down Expand Up @@ -817,13 +821,17 @@ export class ApiModelGenerator {
let apiProperty: ApiProperty | undefined = parentApiItem.tryGetMemberByKey(containerKey) as ApiProperty;

if (apiProperty === undefined) {
const propertyDeclaration: ts.PropertyDeclaration =
astDeclaration.declaration as ts.PropertyDeclaration;

const nodesToCapture: IExcerptBuilderNodeToCapture[] = [];

const propertyTypeTokenRange: IExcerptTokenRange = ExcerptBuilder.createEmptyTokenRange();
nodesToCapture.push({ node: propertyDeclaration.type, tokenRange: propertyTypeTokenRange });

// 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);
const apiItemMetadata: ApiItemMetadata = this._collector.fetchApiItemMetadata(astDeclaration);
Expand Down
11 changes: 10 additions & 1 deletion build-tests/api-documenter-test/config/api-extractor.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,14 @@
"enabled": false
},

"testMode": true
"testMode": true,

"messages": {
"extractorMessageReporting": {
// Purposefully disabled for the `writeonlyProperty` test case.
"ae-missing-getter": {
"logLevel": "none"
}
}
}
}
27 changes: 27 additions & 0 deletions build-tests/api-documenter-test/etc/api-documenter-test.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,33 @@
"endIndex": 2
},
"isStatic": false
},
{
"kind": "Property",
"canonicalReference": "api-documenter-test!DocClass1#writeonlyProperty:member",
"docComment": "/**\n * API Extractor will surface an `ae-missing-getter` finding for this property.\n */\n",
"excerptTokens": [
{
"kind": "Content",
"text": "set writeonlyProperty(value: "
},
{
"kind": "Content",
"text": "string"
},
{
"kind": "Content",
"text": ");"
}
],
"isOptional": false,
"releaseTag": "Public",
"name": "writeonlyProperty",
"propertyTypeTokenRange": {
"startIndex": 1,
"endIndex": 2
},
"isStatic": false
}
],
"extendsTokenRange": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class DocClass1 extends DocBaseClass implements IDocInterface1, IDocInter
// (undocumented)
get writeableProperty(): string;
set writeableProperty(value: string);
set writeonlyProperty(value: string);
}

// @public
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The constructor for this class is marked as internal. Third-party code should no
| [readonlyProperty](./api-documenter-test.docclass1.readonlyproperty.md) | | string | |
| [regularProperty](./api-documenter-test.docclass1.regularproperty.md) | | [SystemEvent](./api-documenter-test.systemevent.md) | This is a regular property that happens to use the SystemEvent type. |
| [writeableProperty](./api-documenter-test.docclass1.writeableproperty.md) | | string | |
| [writeonlyProperty](./api-documenter-test.docclass1.writeonlyproperty.md) | | string | API Extractor will surface an <code>ae-missing-getter</code> finding for this property. |
## Methods
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [api-documenter-test](./api-documenter-test.md) &gt; [DocClass1](./api-documenter-test.docclass1.md) &gt; [writeonlyProperty](./api-documenter-test.docclass1.writeonlyproperty.md)

## DocClass1.writeonlyProperty property

API Extractor will surface an `ae-missing-getter` finding for this property.

<b>Signature:</b>

```typescript
set writeonlyProperty(value: string);
```
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ properties:
set writeableProperty(value: string);
return:
type: string
- name: writeonlyProperty
uid: 'api-documenter-test!DocClass1#writeonlyProperty:member'
package: api-documenter-test!
fullName: writeonlyProperty
summary: API Extractor will surface an `ae-missing-getter` finding for this property.
remarks: ''
example: []
isPreview: false
isDeprecated: false
syntax:
content: 'set writeonlyProperty(value: string);'
return:
type: string
methods:
- name: deprecatedExample()
uid: 'api-documenter-test!DocClass1#deprecatedExample:member(1)'
Expand Down
5 changes: 5 additions & 0 deletions build-tests/api-documenter-test/src/DocClass1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ export class DocClass1 extends DocBaseClass implements IDocInterface1, IDocInter
}
public set writeableProperty(value: string) {}

/**
* API Extractor will surface an `ae-missing-getter` finding for this property.
*/
public set writeonlyProperty(value: string) {}

/**
* This event is fired whenever the object is modified.
* @eventProperty
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/api-extractor",
"comment": "Generate API doc model nodes for setters without getters",
"type": "minor"
}
],
"packageName": "@microsoft/api-extractor"
}

0 comments on commit 5f56cf6

Please sign in to comment.