Skip to content

Commit 5f56cf6

Browse files
authored
Merge pull request #3396 from zelliott/handle-setters
[api-extractor] Generate documentation for setters without getters
2 parents e9a2b58 + ca2e07a commit 5f56cf6

File tree

9 files changed

+92
-5
lines changed

9 files changed

+92
-5
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ export class ApiModelGenerator {
202202
this._processApiProperty(astDeclaration, exportedName, parentApiItem);
203203
break;
204204

205+
case ts.SyntaxKind.SetAccessor:
206+
this._processApiProperty(astDeclaration, exportedName, parentApiItem);
207+
break;
208+
205209
case ts.SyntaxKind.IndexSignature:
206210
this._processApiIndexSignature(astDeclaration, exportedName, parentApiItem);
207211
break;
@@ -817,13 +821,17 @@ export class ApiModelGenerator {
817821
let apiProperty: ApiProperty | undefined = parentApiItem.tryGetMemberByKey(containerKey) as ApiProperty;
818822

819823
if (apiProperty === undefined) {
820-
const propertyDeclaration: ts.PropertyDeclaration =
821-
astDeclaration.declaration as ts.PropertyDeclaration;
822-
823824
const nodesToCapture: IExcerptBuilderNodeToCapture[] = [];
824825

825826
const propertyTypeTokenRange: IExcerptTokenRange = ExcerptBuilder.createEmptyTokenRange();
826-
nodesToCapture.push({ node: propertyDeclaration.type, tokenRange: propertyTypeTokenRange });
827+
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;
834+
nodesToCapture.push({ node: propertyTypeNode, tokenRange: propertyTypeTokenRange });
827835

828836
const excerptTokens: IExcerptToken[] = this._buildExcerptTokens(astDeclaration, nodesToCapture);
829837
const apiItemMetadata: ApiItemMetadata = this._collector.fetchApiItemMetadata(astDeclaration);

build-tests/api-documenter-test/config/api-extractor.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,14 @@
1818
"enabled": false
1919
},
2020

21-
"testMode": true
21+
"testMode": true,
22+
23+
"messages": {
24+
"extractorMessageReporting": {
25+
// Purposefully disabled for the `writeonlyProperty` test case.
26+
"ae-missing-getter": {
27+
"logLevel": "none"
28+
}
29+
}
30+
}
2231
}

build-tests/api-documenter-test/etc/api-documenter-test.api.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,33 @@
905905
"endIndex": 2
906906
},
907907
"isStatic": false
908+
},
909+
{
910+
"kind": "Property",
911+
"canonicalReference": "api-documenter-test!DocClass1#writeonlyProperty:member",
912+
"docComment": "/**\n * API Extractor will surface an `ae-missing-getter` finding for this property.\n */\n",
913+
"excerptTokens": [
914+
{
915+
"kind": "Content",
916+
"text": "set writeonlyProperty(value: "
917+
},
918+
{
919+
"kind": "Content",
920+
"text": "string"
921+
},
922+
{
923+
"kind": "Content",
924+
"text": ");"
925+
}
926+
],
927+
"isOptional": false,
928+
"releaseTag": "Public",
929+
"name": "writeonlyProperty",
930+
"propertyTypeTokenRange": {
931+
"startIndex": 1,
932+
"endIndex": 2
933+
},
934+
"isStatic": false
908935
}
909936
],
910937
"extendsTokenRange": {

build-tests/api-documenter-test/etc/api-documenter-test.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export class DocClass1 extends DocBaseClass implements IDocInterface1, IDocInter
4949
// (undocumented)
5050
get writeableProperty(): string;
5151
set writeableProperty(value: string);
52+
set writeonlyProperty(value: string);
5253
}
5354

5455
// @public

build-tests/api-documenter-test/etc/markdown/api-documenter-test.docclass1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ The constructor for this class is marked as internal. Third-party code should no
3838
| [readonlyProperty](./api-documenter-test.docclass1.readonlyproperty.md) | | string | |
3939
| [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. |
4040
| [writeableProperty](./api-documenter-test.docclass1.writeableproperty.md) | | string | |
41+
| [writeonlyProperty](./api-documenter-test.docclass1.writeonlyproperty.md) | | string | API Extractor will surface an <code>ae-missing-getter</code> finding for this property. |
4142
4243
## Methods
4344
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[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)
4+
5+
## DocClass1.writeonlyProperty property
6+
7+
API Extractor will surface an `ae-missing-getter` finding for this property.
8+
9+
<b>Signature:</b>
10+
11+
```typescript
12+
set writeonlyProperty(value: string);
13+
```

build-tests/api-documenter-test/etc/yaml/api-documenter-test/docclass1.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ properties:
6161
set writeableProperty(value: string);
6262
return:
6363
type: string
64+
- name: writeonlyProperty
65+
uid: 'api-documenter-test!DocClass1#writeonlyProperty:member'
66+
package: api-documenter-test!
67+
fullName: writeonlyProperty
68+
summary: API Extractor will surface an `ae-missing-getter` finding for this property.
69+
remarks: ''
70+
example: []
71+
isPreview: false
72+
isDeprecated: false
73+
syntax:
74+
content: 'set writeonlyProperty(value: string);'
75+
return:
76+
type: string
6477
methods:
6578
- name: deprecatedExample()
6679
uid: 'api-documenter-test!DocClass1#deprecatedExample:member(1)'

build-tests/api-documenter-test/src/DocClass1.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ export class DocClass1 extends DocBaseClass implements IDocInterface1, IDocInter
206206
}
207207
public set writeableProperty(value: string) {}
208208

209+
/**
210+
* API Extractor will surface an `ae-missing-getter` finding for this property.
211+
*/
212+
public set writeonlyProperty(value: string) {}
213+
209214
/**
210215
* This event is fired whenever the object is modified.
211216
* @eventProperty
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/api-extractor",
5+
"comment": "Generate API doc model nodes for setters without getters",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@microsoft/api-extractor"
10+
}

0 commit comments

Comments
 (0)