Skip to content

Commit 9ef6fed

Browse files
fix: handle an method parameter being an enum type with one part of enum being a dynamic struct (#153)
1 parent 98764b6 commit 9ef6fed

File tree

4 files changed

+88
-57
lines changed

4 files changed

+88
-57
lines changed

src/dynamic-param-interfaces.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@ const flushParamInterfaces = (
148148
param.properties = param.properties || [];
149149
param.properties.forEach(paramProperty => {
150150
if (paramProperty.description) {
151-
utils.extendArray(paramAPI, utils.wrapComment(paramProperty.description, paramProperty.additionalTags));
151+
utils.extendArray(
152+
paramAPI,
153+
utils.wrapComment(paramProperty.description, paramProperty.additionalTags),
154+
);
152155
}
153156

154157
if (!Array.isArray(paramProperty.type) && paramProperty.type.toLowerCase() === 'object') {

src/module-declaration.ts

+22-7
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ export const generateModuleDeclaration = (
6666
_.concat([], module.instanceEvents || [], module.events || [])
6767
.sort((a, b) => a.name.localeCompare(b.name))
6868
.forEach(moduleEvent => {
69-
utils.extendArray(moduleAPI, utils.wrapComment(moduleEvent.description, moduleEvent.additionalTags));
69+
utils.extendArray(
70+
moduleAPI,
71+
utils.wrapComment(moduleEvent.description, moduleEvent.additionalTags),
72+
);
7073
let listener = 'Function';
7174

7275
if (moduleEvent.parameters && moduleEvent.parameters.length) {
@@ -103,7 +106,7 @@ export const generateModuleDeclaration = (
103106
}
104107

105108
let newType = argType || utils.typify(eventListenerArg);
106-
const functionListenerArg = eventListenerArg as any as DetailedFunctionType &
109+
const functionListenerArg = (eventListenerArg as any) as DetailedFunctionType &
107110
DocumentationBlock &
108111
TypeInformation;
109112
if (newType === 'Function') {
@@ -134,7 +137,10 @@ export const generateModuleDeclaration = (
134137
if (module.type === 'Element') {
135138
if (module.events) {
136139
module.events.forEach(domEvent => {
137-
utils.extendArray(moduleAPI, utils.wrapComment(domEvent.description, domEvent.additionalTags));
140+
utils.extendArray(
141+
moduleAPI,
142+
utils.wrapComment(domEvent.description, domEvent.additionalTags),
143+
);
138144
let eventType = 'Event';
139145

140146
if (domEvent.parameters && domEvent.parameters.length) {
@@ -190,7 +196,10 @@ export const generateModuleDeclaration = (
190196
['on', 'once', 'removeAllListeners', 'removeListener'].includes(moduleMethod.name);
191197

192198
const addMethod = (moduleMethod: MethodDocumentationBlock, prefix = '') => {
193-
utils.extendArray(moduleAPI, utils.wrapComment(moduleMethod.description, moduleMethod.additionalTags));
199+
utils.extendArray(
200+
moduleAPI,
201+
utils.wrapComment(moduleMethod.description, moduleMethod.additionalTags),
202+
);
194203
let returnType: string | TypeInformation = returnsThis(moduleMethod) ? 'this' : 'void';
195204

196205
if (moduleMethod.returns) {
@@ -275,7 +284,9 @@ export const generateModuleDeclaration = (
275284
.sort((a, b) => a.name.localeCompare(b.name))
276285
.forEach(prop => {
277286
const isOptional = !prop.required ? '?' : '';
278-
const isReadonly = prop.additionalTags.includes(DocumentationTag.AVAILABILITY_READONLY) ? 'readonly ' : '';
287+
const isReadonly = prop.additionalTags.includes(DocumentationTag.AVAILABILITY_READONLY)
288+
? 'readonly '
289+
: '';
279290
moduleAPI.push(`${isReadonly}${prop.name}${isOptional}: ${utils.typify(prop)};`);
280291
});
281292
}
@@ -285,7 +296,9 @@ export const generateModuleDeclaration = (
285296
module.staticProperties
286297
.sort((a, b) => a.name.localeCompare(b.name))
287298
.forEach(prop => {
288-
const isReadonly = prop.additionalTags.includes(DocumentationTag.AVAILABILITY_READONLY) ? 'readonly ' : '';
299+
const isReadonly = prop.additionalTags.includes(DocumentationTag.AVAILABILITY_READONLY)
300+
? 'readonly '
301+
: '';
289302
moduleAPI.push(`static ${isReadonly}${prop.name}: ${utils.typify(prop)};`);
290303
});
291304
}
@@ -304,7 +317,9 @@ export const generateModuleDeclaration = (
304317

305318
const isStatic = isStaticVersion ? 'static ' : '';
306319
const isOptional = utils.isOptional(p) ? '?' : '';
307-
const isReadonly = p.additionalTags.includes(DocumentationTag.AVAILABILITY_READONLY) ? 'readonly ' : '';
320+
const isReadonly = p.additionalTags.includes(DocumentationTag.AVAILABILITY_READONLY)
321+
? 'readonly '
322+
: '';
308323
type = type || utils.typify(paramType);
309324

310325
utils.extendArray(moduleAPI, utils.wrapComment(p.description, p.additionalTags));

src/utils.ts

+59-46
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,31 @@ export const wrapComment = (comment: string, additionalTags: DocumentationTag[]
5050
if (additionalTags.length) {
5151
result.push(' *');
5252
const nodePlatforms: string[] = [];
53-
result.push(...(additionalTags.map(tag => {
54-
switch (tag) {
55-
case DocumentationTag.STABILITY_DEPRECATED:
56-
return ' * @deprecated';
57-
case DocumentationTag.STABILITY_EXPERIMENTAL:
58-
return ' * @experimental';
59-
case DocumentationTag.OS_LINUX:
60-
nodePlatforms.push('linux');
61-
break;
62-
case DocumentationTag.OS_MACOS:
63-
nodePlatforms.push('darwin');
64-
break;
65-
case DocumentationTag.OS_MAS:
66-
nodePlatforms.push('mas');
67-
break;
68-
case DocumentationTag.OS_WINDOWS:
69-
nodePlatforms.push('win32');
70-
break;
71-
}
72-
return '';
73-
}).filter(tag => tag)))
53+
result.push(
54+
...additionalTags
55+
.map(tag => {
56+
switch (tag) {
57+
case DocumentationTag.STABILITY_DEPRECATED:
58+
return ' * @deprecated';
59+
case DocumentationTag.STABILITY_EXPERIMENTAL:
60+
return ' * @experimental';
61+
case DocumentationTag.OS_LINUX:
62+
nodePlatforms.push('linux');
63+
break;
64+
case DocumentationTag.OS_MACOS:
65+
nodePlatforms.push('darwin');
66+
break;
67+
case DocumentationTag.OS_MAS:
68+
nodePlatforms.push('mas');
69+
break;
70+
case DocumentationTag.OS_WINDOWS:
71+
nodePlatforms.push('win32');
72+
break;
73+
}
74+
return '';
75+
})
76+
.filter(tag => tag),
77+
);
7478
if (nodePlatforms.length) {
7579
result.push(` * @platform ${nodePlatforms.join(',')}`);
7680
}
@@ -253,9 +257,9 @@ export const isEmitter = (module: Pick<ModuleDocumentationContainer, 'name'>) =>
253257
'touchbarbutton',
254258
'net',
255259
'netlog',
256-
'protocol'
260+
'protocol',
257261
];
258-
return !(nonEventEmitters.includes(module.name.toLowerCase()))
262+
return !nonEventEmitters.includes(module.name.toLowerCase());
259263
};
260264
export const isPrimitive = (type: string) => {
261265
const primitives = ['boolean', 'number', 'any', 'string', 'void', 'unknown'];
@@ -285,6 +289,29 @@ export const genMethodString = (
285289
includeType = true,
286290
paramTypePrefix = '',
287291
): string => {
292+
const createMethodObjectParamType = (
293+
objectParam: DetailedObjectType & TypeInformation & DocumentationBlock & { required: boolean },
294+
) => {
295+
if ('constructor' === moduleMethod.name.toLowerCase()) {
296+
objectParam.name = objectParam.name || 'options';
297+
}
298+
if (objectParam.name === 'options') {
299+
if (
300+
['show', 'hide', 'open', 'close', 'start', 'stop', 'constructor'].includes(
301+
moduleMethod.name.toLowerCase(),
302+
)
303+
) {
304+
return paramInterfaces.createParamInterface(
305+
objectParam,
306+
_.upperFirst(module.name) + _.upperFirst(moduleMethod.name),
307+
);
308+
}
309+
310+
return paramInterfaces.createParamInterface(objectParam, _.upperFirst(moduleMethod.name));
311+
}
312+
313+
return paramInterfaces.createParamInterface(objectParam, '', _.upperFirst(moduleMethod.name));
314+
};
288315
return `${includeType ? '(' : ''}${(moduleMethod.parameters || [])
289316
.map(param => {
290317
let paramType: string | null = param as any;
@@ -294,34 +321,15 @@ export const genMethodString = (
294321
DocumentationBlock & { required: boolean };
295322
if (param.type === 'Object' && objectParam.properties && objectParam.properties.length) {
296323
// Check if we have the same structure for a different name
297-
if (param.name === 'options') {
298-
if (
299-
['show', 'hide', 'open', 'close', 'start', 'stop', 'constructor'].includes(
300-
moduleMethod.name.toLowerCase(),
301-
)
302-
) {
303-
paramType = paramInterfaces.createParamInterface(
304-
objectParam,
305-
_.upperFirst(module.name) + _.upperFirst(moduleMethod.name),
306-
);
307-
} else {
308-
paramType = paramInterfaces.createParamInterface(
309-
objectParam,
310-
_.upperFirst(moduleMethod.name),
311-
);
312-
}
313-
} else {
314-
paramType = paramInterfaces.createParamInterface(
315-
objectParam,
316-
'',
317-
_.upperFirst(moduleMethod.name),
318-
);
319-
}
324+
paramType = createMethodObjectParamType(objectParam);
320325
}
321326
322327
if (Array.isArray(param.type)) {
323328
param.type = param.type.map(paramType => {
324329
const functionParam = paramType as DetailedFunctionType;
330+
const objectParam = paramType as DetailedObjectType &
331+
TypeInformation &
332+
DocumentationBlock & { required: boolean };
325333
if (paramType.type === 'Function' && functionParam.parameters) {
326334
return Object.assign({}, paramType, {
327335
type: genMethodString(
@@ -333,6 +341,11 @@ export const genMethodString = (
333341
} as any /* FIXME: */,
334342
),
335343
});
344+
} else if (paramType.type === 'Object' && objectParam.properties) {
345+
return {
346+
...objectParam,
347+
type: createMethodObjectParamType(objectParam),
348+
};
336349
}
337350
return paramType;
338351
});

yarn.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
registry-auth-token "^3.3.1"
2929

3030
"@electron/docs-parser@^0.4.1":
31-
version "0.4.1"
32-
resolved "https://registry.yarnpkg.com/@electron/docs-parser/-/docs-parser-0.4.1.tgz#e6557a6a3899ca1168b9f2d70ad24d9ba924e4a9"
33-
integrity sha512-mn4ofEvH8HNdzRMb++cUbiTkO88BAKwM0yjED3KrQ07yEl7CU1lK13qSMVSRhDzNkbg4kFTqBDjaJ57fdHlkxQ==
31+
version "0.4.2"
32+
resolved "https://registry.yarnpkg.com/@electron/docs-parser/-/docs-parser-0.4.2.tgz#1d5feb0376363435b2f6833b60271ee90c26f952"
33+
integrity sha512-BdEW2So0Qg7lxIET9q84DceavC1v/EyYF9PXU8vRRiwFebGPyEaJS4uqxhStAxmZslQAT4JjQc9jTd12IbG6BQ==
3434
dependencies:
3535
"@types/markdown-it" "^0.0.7"
3636
chai "^4.2.0"

0 commit comments

Comments
 (0)