Skip to content

Commit b81bb04

Browse files
feat: expose deprecated,readonly and platform annotations in the ts defs (#150)
1 parent 33a3603 commit b81bb04

6 files changed

+57
-41
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"semantic-release": "^15.13.3"
3535
},
3636
"dependencies": {
37-
"@electron/docs-parser": "^0.2.1",
37+
"@electron/docs-parser": "^0.4.1",
3838
"@types/node": "^11.13.7",
3939
"chalk": "^2.4.2",
4040
"colors": "^1.1.2",

src/dynamic-param-interfaces.ts

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

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

src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ const appendNodeJSOverride = (outputLines: string[]) => {
5454
);
5555
utils.extendArray(outputLines, [
5656
' interface ProcessVersions {',
57-
' electron: string;',
58-
' chrome: string;',
57+
' readonly electron: string;',
58+
' readonly chrome: string;',
5959
' }',
6060
]);
6161

src/module-declaration.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
TypeInformation,
1313
DetailedFunctionType,
1414
ElementDocumentationContainer,
15+
DocumentationTag,
1516
} from '@electron/docs-parser';
1617

1718
const modules: Record<string, string[]> = {};
@@ -65,7 +66,7 @@ export const generateModuleDeclaration = (
6566
_.concat([], module.instanceEvents || [], module.events || [])
6667
.sort((a, b) => a.name.localeCompare(b.name))
6768
.forEach(moduleEvent => {
68-
utils.extendArray(moduleAPI, utils.wrapComment(moduleEvent.description));
69+
utils.extendArray(moduleAPI, utils.wrapComment(moduleEvent.description, moduleEvent.additionalTags));
6970
let listener = 'Function';
7071

7172
if (moduleEvent.parameters && moduleEvent.parameters.length) {
@@ -102,7 +103,7 @@ export const generateModuleDeclaration = (
102103
}
103104

104105
let newType = argType || utils.typify(eventListenerArg);
105-
const functionListenerArg = eventListenerArg as DetailedFunctionType &
106+
const functionListenerArg = eventListenerArg as any as DetailedFunctionType &
106107
DocumentationBlock &
107108
TypeInformation;
108109
if (newType === 'Function') {
@@ -133,7 +134,7 @@ export const generateModuleDeclaration = (
133134
if (module.type === 'Element') {
134135
if (module.events) {
135136
module.events.forEach(domEvent => {
136-
utils.extendArray(moduleAPI, utils.wrapComment(domEvent.description));
137+
utils.extendArray(moduleAPI, utils.wrapComment(domEvent.description, domEvent.additionalTags));
137138
let eventType = 'Event';
138139

139140
if (domEvent.parameters && domEvent.parameters.length) {
@@ -189,7 +190,7 @@ export const generateModuleDeclaration = (
189190
['on', 'once', 'removeAllListeners', 'removeListener'].includes(moduleMethod.name);
190191

191192
const addMethod = (moduleMethod: MethodDocumentationBlock, prefix = '') => {
192-
utils.extendArray(moduleAPI, utils.wrapComment(moduleMethod.description));
193+
utils.extendArray(moduleAPI, utils.wrapComment(moduleMethod.description, moduleMethod.additionalTags));
193194
let returnType: string | TypeInformation = returnsThis(moduleMethod) ? 'this' : 'void';
194195

195196
if (moduleMethod.returns) {
@@ -245,6 +246,7 @@ export const generateModuleDeclaration = (
245246
...module.constructorMethod,
246247
description: module.name,
247248
returns: null,
249+
additionalTags: [],
248250
});
249251
}
250252

@@ -273,7 +275,8 @@ export const generateModuleDeclaration = (
273275
.sort((a, b) => a.name.localeCompare(b.name))
274276
.forEach(prop => {
275277
const isOptional = !prop.required ? '?' : '';
276-
moduleAPI.push(`${prop.name}${isOptional}: ${utils.typify(prop)};`);
278+
const isReadonly = prop.additionalTags.includes(DocumentationTag.AVAILABILITY_READONLY) ? 'readonly ' : '';
279+
moduleAPI.push(`${isReadonly}${prop.name}${isOptional}: ${utils.typify(prop)};`);
277280
});
278281
}
279282

@@ -282,12 +285,8 @@ export const generateModuleDeclaration = (
282285
module.staticProperties
283286
.sort((a, b) => a.name.localeCompare(b.name))
284287
.forEach(prop => {
285-
// if (prop.type === 'Class') {
286-
// moduleAPI.push(`static ${prop.name}: typeof ${prop.name};`);
287-
// generateModuleDeclaration(prop, -1, API);
288-
// } else {
289-
moduleAPI.push(`static ${prop.name}: ${utils.typify(prop)};`);
290-
// }
288+
const isReadonly = prop.additionalTags.includes(DocumentationTag.AVAILABILITY_READONLY) ? 'readonly ' : '';
289+
moduleAPI.push(`static${isReadonly} ${prop.name}: ${utils.typify(prop)};`);
291290
});
292291
}
293292

@@ -305,16 +304,17 @@ export const generateModuleDeclaration = (
305304

306305
const isStatic = isStaticVersion ? 'static ' : '';
307306
const isOptional = utils.isOptional(p) ? '?' : '';
307+
const isReadonly = p.additionalTags.includes(DocumentationTag.AVAILABILITY_READONLY) ? 'readonly ' : '';
308308
type = type || utils.typify(paramType);
309309

310-
utils.extendArray(moduleAPI, utils.wrapComment(p.description));
310+
utils.extendArray(moduleAPI, utils.wrapComment(p.description, p.additionalTags));
311311
if (module.name === 'process' && p.name === 'versions') return;
312312

313313
if (p.name.match(/^\d/)) {
314314
// Wrap key in quotes if it starts with a number, e.g. `2d_canvas`
315-
moduleAPI.push(`'${isStatic}${p.name}${isOptional}': ${type};`);
315+
moduleAPI.push(`'${isStatic}${isReadonly}${p.name}${isOptional}': ${type};`);
316316
} else {
317-
moduleAPI.push(`${isStatic}${p.name}${isOptional}: ${type};`);
317+
moduleAPI.push(`${isStatic}${isReadonly}${p.name}${isOptional}: ${type};`);
318318
}
319319
});
320320
}

src/utils.ts

+30-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
DetailedObjectType,
88
DocumentationBlock,
99
DetailedFunctionType,
10+
DocumentationTag,
1011
} from '@electron/docs-parser';
1112
import _ from 'lodash';
1213
import d from 'debug';
@@ -26,7 +27,7 @@ export const extendArray = <T>(arr1: T[], arr2: T[]): T[] => {
2627
return arr1;
2728
};
2829

29-
export const wrapComment = (comment: string): string[] => {
30+
export const wrapComment = (comment: string, additionalTags: DocumentationTag[] = []): string[] => {
3031
if (!comment) return [];
3132
comment = comment.replace(/^\(optional\)(?: - )?/gi, '').trim();
3233
if (!comment) return [];
@@ -46,6 +47,34 @@ export const wrapComment = (comment: string): string[] => {
4647
result.push(` * ${comment.substring(0, index)}`);
4748
comment = comment.substring(index + 1);
4849
}
50+
if (additionalTags.length) {
51+
result.push(' *');
52+
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)))
74+
if (nodePlatforms.length) {
75+
result.push(` * @platform ${nodePlatforms.join(',')}`);
76+
}
77+
}
4978
return result.concat(' */');
5079
};
5180

yarn.lock

+9-22
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
read-pkg "^4.0.0"
2828
registry-auth-token "^3.3.1"
2929

30-
"@electron/docs-parser@^0.2.1":
31-
version "0.2.1"
32-
resolved "https://registry.yarnpkg.com/@electron/docs-parser/-/docs-parser-0.2.1.tgz#14bcc7ec5afa1b2e89dc6dbdb7152dc74c2b5c46"
33-
integrity sha512-vQNxIQzIudFp3TECH9TEkPZUvLrDzZO5wZPmgN5e3c69yqmMj779I86Ny8giQfY93jFpRZ/+gRqIwVfD6gFEWA==
30+
"@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==
3434
dependencies:
3535
"@types/markdown-it" "^0.0.7"
3636
chai "^4.2.0"
@@ -1513,12 +1513,7 @@ [email protected], escape-string-regexp@^1.0.2, escape-string-regexp@^1
15131513
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
15141514
integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=
15151515

1516-
esprima@^4.0.0:
1517-
version "4.0.1"
1518-
resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71"
1519-
integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==
1520-
1521-
esprima@~4.0.0:
1516+
esprima@^4.0.0, esprima@~4.0.0:
15221517
version "4.0.1"
15231518
resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71"
15241519
integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==
@@ -2693,15 +2688,7 @@ java-properties@^0.2.9:
26932688
resolved "https://registry.yarnpkg.com/java-properties/-/java-properties-0.2.10.tgz#2551560c25fa1ad94d998218178f233ad9b18f60"
26942689
integrity sha512-CpKJh9VRNhS+XqZtg1UMejETGEiqwCGDC/uwPEEQwc2nfdbSm73SIE29TplG2gLYuBOOTNDqxzG6A9NtEPLt0w==
26952690

2696-
[email protected], js-yaml@^3.9.0:
2697-
version "3.13.1"
2698-
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.13.1.tgz#aff151b30bfdfa8e49e05da22e7415e9dfa37847"
2699-
integrity sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==
2700-
dependencies:
2701-
argparse "^1.0.7"
2702-
esprima "^4.0.0"
2703-
2704-
js-yaml@^3.13.0:
2691+
[email protected], js-yaml@^3.13.0, js-yaml@^3.9.0:
27052692
version "3.13.1"
27062693
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.13.1.tgz#aff151b30bfdfa8e49e05da22e7415e9dfa37847"
27072694
integrity sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==
@@ -2969,9 +2956,9 @@ libnpx@^10.2.0:
29692956
yargs "^11.0.0"
29702957

29712958
linkify-it@^2.0.0:
2972-
version "2.1.0"
2973-
resolved "https://registry.yarnpkg.com/linkify-it/-/linkify-it-2.1.0.tgz#c4caf38a6cd7ac2212ef3c7d2bde30a91561f9db"
2974-
integrity sha512-4REs8/062kV2DSHxNfq5183zrqXMl7WP0WzABH9IeJI+NLm429FgE1PDecltYfnOoFDFlZGh2T8PfZn0r+GTRg==
2959+
version "2.2.0"
2960+
resolved "https://registry.yarnpkg.com/linkify-it/-/linkify-it-2.2.0.tgz#e3b54697e78bf915c70a38acd78fd09e0058b1cf"
2961+
integrity sha512-GnAl/knGn+i1U/wjBz3akz2stz+HrHLsxMwHQGofCDfPvlf+gDKN58UtfmUquTY4/MXeE2x7k19KQmeoZi94Iw==
29752962
dependencies:
29762963
uc.micro "^1.0.1"
29772964

0 commit comments

Comments
 (0)