Skip to content

Commit 7f9d849

Browse files
authored
Merge pull request microsoft#367 from Microsoft/pgonzal/yaml-documenter6
Fix an issue where the *.api.json files were sometimes missing function parameters
2 parents be3b9de + 7de7daf commit 7f9d849

9 files changed

Lines changed: 116 additions & 90 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/api-extractor",
5+
"comment": "Fix an issue where the *.api.json file was sometimes missing function parameters",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@microsoft/api-extractor",
10+
"email": "pgonzal@users.noreply.github.com"
11+
}

‎common/reviews/api/api-extractor.api.ts‎

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ class ApiFileGenerator extends AstItemVisitor {
66
public static areEquivalentApiFileContents(actualFileContent: string, expectedFileContent: string): boolean;
77
// (undocumented)
88
public generateApiFileContent(extractor: Extractor): string;
9-
// WARNING: The type "AstParameter" needs to be exported by the package (e.g. added to index.ts)
10-
// (undocumented)
11-
protected visitApiParam(astParam: AstParameter): void;
129
// WARNING: The type "AstEnum" needs to be exported by the package (e.g. added to index.ts)
1310
// (undocumented)
1411
protected visitAstEnum(astEnum: AstEnum): void;
@@ -44,9 +41,6 @@ class ApiJsonGenerator extends AstItemVisitor {
4441
// WARNING: The type "AstItem" needs to be exported by the package (e.g. added to index.ts)
4542
// (undocumented)
4643
protected visit(astItem: AstItem, refObject?: Object): void;
47-
// WARNING: The type "AstParameter" needs to be exported by the package (e.g. added to index.ts)
48-
// (undocumented)
49-
protected visitApiParam(astParam: AstParameter, refObject?: Object): void;
5044
// WARNING: The type "AstEnum" needs to be exported by the package (e.g. added to index.ts)
5145
// (undocumented)
5246
protected visitAstEnum(astEnum: AstEnum, refObject?: Object): void;
@@ -126,18 +120,14 @@ interface IApiClass extends IApiBaseDefinition {
126120
extends?: string;
127121
implements?: string;
128122
kind: 'class';
129-
members: {
130-
[ name: string ]: ApiMember
131-
}
123+
members: IApiNameMap<ApiMember>;
132124
typeParameters?: string[];
133125
}
134126

135127
// @alpha
136128
interface IApiConstructor extends IApiBaseDefinition {
137129
kind: 'constructor';
138-
parameters: {
139-
[ name: string ]: IApiParameter
140-
}
130+
parameters: IApiNameMap<IApiParameter>;
141131
}
142132

143133
// @alpha
@@ -157,9 +147,7 @@ interface IApiEnumMember extends IApiBaseDefinition {
157147
// @alpha
158148
interface IApiFunction extends IApiBaseDefinition {
159149
kind: 'function';
160-
parameters: {
161-
[ name: string ]: IApiParameter
162-
}
150+
parameters: IApiNameMap<IApiParameter>;
163151
returnValue: IApiReturnValue;
164152
}
165153

@@ -168,9 +156,7 @@ interface IApiInterface extends IApiBaseDefinition {
168156
extends?: string;
169157
implements?: string;
170158
kind: 'interface';
171-
members: {
172-
[ name: string ]: ApiMember
173-
}
159+
members: IApiNameMap<ApiMember>;
174160
typeParameters?: string[];
175161
}
176162

@@ -188,20 +174,22 @@ interface IApiMethod extends IApiBaseDefinition {
188174
isOptional: boolean;
189175
isStatic: boolean;
190176
kind: 'method';
191-
parameters: {
192-
[ name: string ]: IApiParameter
193-
}
177+
parameters: IApiNameMap<IApiParameter>;
194178
returnValue: IApiReturnValue;
195179
signature: string;
196180
}
197181

182+
// @alpha
183+
interface IApiNameMap<T> {
184+
// (undocumented)
185+
[ name: string ]: T;
186+
}
187+
198188
// @alpha
199189
interface IApiPackage {
200190
// (undocumented)
201191
deprecatedMessage?: IDocElement[];
202-
exports: {
203-
[ name: string ]: ApiItem
204-
}
192+
exports: IApiNameMap<ApiItem>;
205193
isBeta?: boolean;
206194
kind: 'package';
207195
name: string;

‎libraries/api-extractor/src/api/ApiItem.ts‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ export interface IApiParameter {
7676
type: string;
7777
}
7878

79+
/**
80+
* An ordered map of items, indexed by the symbol name.
81+
* @alpha
82+
*/
83+
export interface IApiNameMap<T> {
84+
/**
85+
* For a given name, returns the object with that name.
86+
*/
87+
[name: string]: T;
88+
}
89+
7990
/**
8091
* Return value of a method or function.
8192
* @alpha
@@ -175,7 +186,7 @@ export interface IApiMethod extends IApiBaseDefinition {
175186
* a mapping of parameter name to IApiParameter
176187
*/
177188

178-
parameters: { [name: string]: IApiParameter};
189+
parameters: IApiNameMap<IApiParameter>;
179190

180191
/**
181192
* describes the return value of the method
@@ -195,7 +206,7 @@ export interface IApiFunction extends IApiBaseDefinition {
195206
/**
196207
* parameters of the function
197208
*/
198-
parameters: { [name: string]: IApiParameter};
209+
parameters: IApiNameMap<IApiParameter>;
199210

200211
/**
201212
* a description of the return value
@@ -215,7 +226,7 @@ export interface IApiConstructor extends IApiBaseDefinition {
215226
/**
216227
* parameters of the function
217228
*/
218-
parameters: { [name: string]: IApiParameter};
229+
parameters: IApiNameMap<IApiParameter>;
219230
}
220231

221232
/**
@@ -230,7 +241,7 @@ export interface IApiClass extends IApiBaseDefinition {
230241
/**
231242
* Can be a combination of methods and/or properties
232243
*/
233-
members: { [name: string]: ApiMember};
244+
members: IApiNameMap<ApiMember>;
234245

235246
/**
236247
* Interfaces implemented by this class
@@ -287,7 +298,7 @@ export interface IApiInterface extends IApiBaseDefinition {
287298
/**
288299
* A mapping from the name of a member API to its ApiMember
289300
*/
290-
members: { [name: string]: ApiMember};
301+
members: IApiNameMap<ApiMember>;
291302

292303
/**
293304
* Interfaces implemented by this interface
@@ -327,7 +338,7 @@ export interface IApiPackage {
327338
/**
328339
* IDocItems of exported API items
329340
*/
330-
exports: { [name: string]: ApiItem};
341+
exports: IApiNameMap<ApiItem>;
331342

332343
/**
333344
* The following are needed so that this interface and can share

‎libraries/api-extractor/src/generators/ApiFileGenerator.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import AstFunction from '../ast/AstFunction';
1010
import AstItem, { AstItemKind } from '../ast/AstItem';
1111
import AstItemVisitor from './AstItemVisitor';
1212
import AstPackage from '../ast/AstPackage';
13-
import AstParameter from '../ast/AstParameter';
1413
import AstMember from '../ast/AstMember';
1514
import AstNamespace from '../ast/AstNamespace';
1615
import AstModuleVariable from '../ast/AstModuleVariable';
@@ -178,10 +177,6 @@ export default class ApiFileGenerator extends AstItemVisitor {
178177
this._indentedWriter.write(astFunction.getDeclarationLine());
179178
}
180179

181-
protected visitApiParam(astParam: AstParameter): void {
182-
throw Error('Not Implemented');
183-
}
184-
185180
/**
186181
* Writes a synopsis of the AEDoc comments, which indicates the release tag,
187182
* whether the item has been documented, and any warnings that were detected

‎libraries/api-extractor/src/generators/ApiJsonGenerator.ts‎

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import AstNamespace from '../ast/AstNamespace';
2121
import AstModuleVariable from '../ast/AstModuleVariable';
2222
import AstMethod from '../ast/AstMethod';
2323
import { ReleaseTag } from '../aedoc/ReleaseTag';
24-
import { IApiReturnValue, IApiParameter } from '../api/ApiItem';
24+
import { IAedocParameter } from '../aedoc/ApiDocumentation';
25+
import { IApiReturnValue, IApiParameter, IApiNameMap } from '../api/ApiItem';
2526
import ApiJsonFile from '../api/ApiJsonFile';
2627

2728
/**
@@ -168,9 +169,6 @@ export default class ApiJsonGenerator extends AstItemVisitor {
168169
return;
169170
}
170171

171-
for (const param of astFunction.params) {
172-
this.visitApiParam(param, astFunction.documentation.parameters[param.name]);
173-
}
174172
const returnValueNode: IApiReturnValue = {
175173
type: astFunction.returnType,
176174
description: astFunction.documentation.returnsMessage
@@ -179,7 +177,7 @@ export default class ApiJsonGenerator extends AstItemVisitor {
179177
const newNode: Object = {
180178
kind: ApiJsonFile.convertKindToJson(astFunction.kind),
181179
returnValue: returnValueNode,
182-
parameters: astFunction.documentation.parameters,
180+
parameters: this._createParameters(astFunction),
183181
deprecatedMessage: astFunction.inheritedDeprecatedMessage || [],
184182
summary: astFunction.documentation.summary || [],
185183
remarks: astFunction.documentation.remarks || [],
@@ -278,16 +276,12 @@ export default class ApiJsonGenerator extends AstItemVisitor {
278276
return;
279277
}
280278

281-
for (const param of astMethod.params) {
282-
this.visitApiParam(param, astMethod.documentation.parameters[param.name]);
283-
}
284-
285279
let newNode: Object;
286280
if (astMethod.name === '__constructor') {
287281
newNode = {
288282
kind: ApiJsonFile.convertKindToJson(AstItemKind.Constructor),
289283
signature: astMethod.getDeclarationLine(),
290-
parameters: astMethod.documentation.parameters,
284+
parameters: this._createParameters(astMethod),
291285
deprecatedMessage: astMethod.inheritedDeprecatedMessage || [],
292286
summary: astMethod.documentation.summary || [],
293287
remarks: astMethod.documentation.remarks || []
@@ -305,7 +299,7 @@ export default class ApiJsonGenerator extends AstItemVisitor {
305299
isOptional: !!astMethod.isOptional,
306300
isStatic: !!astMethod.isStatic,
307301
returnValue: returnValueNode,
308-
parameters: astMethod.documentation.parameters,
302+
parameters: this._createParameters(astMethod),
309303
deprecatedMessage: astMethod.inheritedDeprecatedMessage || [],
310304
summary: astMethod.documentation.summary || [],
311305
remarks: astMethod.documentation.remarks || [],
@@ -316,15 +310,31 @@ export default class ApiJsonGenerator extends AstItemVisitor {
316310
refObject[astMethod.name] = newNode;
317311
}
318312

319-
protected visitApiParam(astParam: AstParameter, refObject?: Object): void {
320-
if (!astParam.supportedName) {
321-
return;
322-
}
313+
private _createParameters(astFunction: AstMethod | AstFunction): IApiNameMap<IApiParameter> {
314+
const result: IApiNameMap<IApiParameter> = { };
315+
316+
for (const astParameter of astFunction.params) {
317+
if (!astParameter.supportedName) {
318+
continue; // skip parameter names with unusual characters
319+
}
323320

324-
if (refObject) {
325-
(refObject as IApiParameter).isOptional = astParam.isOptional;
326-
(refObject as IApiParameter).isSpread = astParam.isSpread;
327-
(refObject as IApiParameter).type = astParam.type;
321+
const apiParameter: IApiParameter = {
322+
name: astParameter.name,
323+
description: [],
324+
isOptional: astParameter.isOptional,
325+
isSpread: astParameter.isSpread,
326+
type: astParameter.type || ''
327+
};
328+
329+
const aedocParameter: IAedocParameter = astFunction.documentation.parameters[astParameter.name];
330+
if (aedocParameter) {
331+
apiParameter.description = aedocParameter.description;
332+
}
333+
334+
result[astParameter.name] = apiParameter;
328335
}
336+
337+
return result;
329338
}
339+
330340
}

‎libraries/api-extractor/src/generators/AstItemVisitor.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import AstStructuredType from '../ast/AstStructuredType';
1010
import AstMember from '../ast/AstMember';
1111
import AstMethod from '../ast/AstMethod';
1212
import AstNamespace from '../ast/AstNamespace';
13-
import AstParameter from '../ast/AstParameter';
1413
import AstProperty from '../ast/AstProperty';
1514
import AstModuleVariable from '../ast/AstModuleVariable';
1615

@@ -66,8 +65,6 @@ abstract class AstItemVisitor {
6665
protected visitAstProperty(astProperty: AstProperty, refObject?: Object): void {
6766
this.visitAstMember(astProperty, refObject);
6867
}
69-
70-
protected abstract visitApiParam(astParam: AstParameter, refObject?: Object): void;
7168
}
7269

7370
export default AstItemVisitor;

‎libraries/api-extractor/testInputs/example1/example1-output.json‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,15 @@
6363
"type": "__proto__",
6464
"description": []
6565
},
66-
"parameters": {},
66+
"parameters": {
67+
"___proto__": {
68+
"name": "___proto__",
69+
"description": [],
70+
"isOptional": false,
71+
"isSpread": false,
72+
"type": "string"
73+
}
74+
},
6775
"deprecatedMessage": [],
6876
"summary": [],
6977
"remarks": [],
@@ -227,7 +235,15 @@
227235
"type": "string",
228236
"description": []
229237
},
230-
"parameters": {},
238+
"parameters": {
239+
"param": {
240+
"name": "param",
241+
"description": [],
242+
"isOptional": false,
243+
"isSpread": false,
244+
"type": "number"
245+
}
246+
},
231247
"deprecatedMessage": [],
232248
"summary": [],
233249
"remarks": [],

‎libraries/api-extractor/testInputs/example3/example3-output.json‎

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -191,20 +191,7 @@
191191
}
192192
]
193193
},
194-
"parameters": {
195-
"param1": {
196-
"name": "param1",
197-
"description": [
198-
{
199-
"kind": "textDocElement",
200-
"value": "A description for param1."
201-
}
202-
],
203-
"isOptional": false,
204-
"isSpread": false,
205-
"type": "string"
206-
}
207-
},
194+
"parameters": {},
208195
"deprecatedMessage": [],
209196
"summary": [
210197
{
@@ -233,20 +220,7 @@
233220
}
234221
]
235222
},
236-
"parameters": {
237-
"param1": {
238-
"name": "param1",
239-
"description": [
240-
{
241-
"kind": "textDocElement",
242-
"value": "A description for param1."
243-
}
244-
],
245-
"isOptional": false,
246-
"isSpread": false,
247-
"type": "string"
248-
}
249-
},
223+
"parameters": {},
250224
"deprecatedMessage": [],
251225
"summary": [
252226
{

0 commit comments

Comments
 (0)