Skip to content

Commit 765bbb1

Browse files
feat: add new Event support for extensible params (#218)
1 parent fd5803d commit 765bbb1

File tree

7 files changed

+67
-19
lines changed

7 files changed

+67
-19
lines changed

base/base_header.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55

66
/// <reference types="node" />
77

8-
type GlobalEvent = Event & { returnValue: any };
8+
type DOMEvent = Event;
99
type GlobalResponse = Response;

base/base_inner.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
const NodeEventEmitter: typeof import('events').EventEmitter;
22

3-
class Accelerator extends String {
3+
type EmptyParams = {};
4+
type Event<Params extends object, Sender extends NodeJS.EventEmitter> = {
5+
preventDefault: () => void;
6+
readonly defaultPrevented: boolean;
7+
sender: Sender;
8+
} & Params;
49

5-
}
10+
class Accelerator extends String {}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"license": "MIT",
2222
"devDependencies": {
2323
"@continuous-auth/semantic-release-npm": "^3.0.0",
24-
"@electron/docs-parser": "^1.0.1",
24+
"@electron/docs-parser": "^1.1.0",
2525
"@types/debug": "^4.1.4",
2626
"@types/fs-extra": "^5.0.5",
2727
"@types/lodash": "^4.14.123",

src/dynamic-param-interfaces.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,7 @@ const flushParamInterfaces = (
118118
)
119119
.forEach(paramKey => {
120120
if (paramKey === 'Event') {
121-
delete paramInterfacesToDeclare[paramKey];
122-
return;
121+
throw 'Unexpected dynamic Event type, should be routed through the Event handler';
123122
}
124123
if (declared[paramKey]) {
125124
const toDeclareCheck: ParamInterface = Object.assign(

src/module-declaration.ts

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import {
1313
DetailedFunctionType,
1414
ElementDocumentationContainer,
1515
DocumentationTag,
16+
PropertyDocumentationBlock,
17+
DetailedEventType,
18+
DetailedEventReferenceType,
1619
} from '@electron/docs-parser';
1720

1821
const modules: Record<string, string[]> = {};
@@ -100,16 +103,17 @@ export const generateModuleDeclaration = (
100103

101104
moduleEvent.parameters.forEach((eventListenerArg, index) => {
102105
let argString = '';
103-
if (eventListenerArg.description) {
106+
const additionalTags = (eventListenerArg as any).additionalTags || [];
107+
if (eventListenerArg.description || additionalTags.length) {
104108
if (index === 0) argString += `\n${indent}`;
105109
argString += utils
106-
.wrapComment(eventListenerArg.description)
110+
.wrapComment(eventListenerArg.description, additionalTags)
107111
.map((l, i) => `${l}\n${indent}`)
108112
.join('');
109113
}
110114

111115
let argType: string | null = null;
112-
const objectListenerArg = eventListenerArg as DetailedObjectType &
116+
const objectListenerArg = eventListenerArg as (DetailedObjectType) &
113117
DocumentationBlock &
114118
TypeInformation & { required: boolean };
115119
if (
@@ -127,6 +131,38 @@ export const generateModuleDeclaration = (
127131
);
128132
}
129133

134+
const eventGenericListenerArg = eventListenerArg as (DetailedEventType) &
135+
DocumentationBlock &
136+
TypeInformation & { required: boolean };
137+
const eventReferenceListenerArg = eventListenerArg as (DetailedEventReferenceType) &
138+
DocumentationBlock &
139+
TypeInformation & { required: boolean };
140+
141+
if (eventGenericListenerArg.type === 'Event') {
142+
let eventParamsType = 'EmptyParams';
143+
if (
144+
eventGenericListenerArg.eventProperties &&
145+
eventGenericListenerArg.eventProperties.length
146+
) {
147+
const fakeObject: any = {
148+
name: 'EventParams',
149+
type: 'Object',
150+
collection: false,
151+
properties: eventGenericListenerArg.eventProperties,
152+
};
153+
eventParamsType = DynamicParamInterfaces.createParamInterface(
154+
fakeObject,
155+
`${_.upperFirst(_.camelCase(module.name))}${_.upperFirst(
156+
_.camelCase(moduleEvent.name),
157+
)}`,
158+
);
159+
}
160+
if (eventReferenceListenerArg.eventPropertiesReference) {
161+
eventParamsType = utils.typify(eventReferenceListenerArg.eventPropertiesReference);
162+
}
163+
argType = `Event<${eventParamsType}, Electron.${_.upperFirst(module.name)}>`;
164+
}
165+
130166
let newType = argType || utils.typify(eventListenerArg);
131167
const functionListenerArg = (eventListenerArg as any) as DetailedFunctionType &
132168
DocumentationBlock &
@@ -163,15 +199,15 @@ export const generateModuleDeclaration = (
163199
moduleAPI,
164200
utils.wrapComment(domEvent.description, domEvent.additionalTags),
165201
);
166-
let eventType = 'Event';
202+
let eventType = 'DOMEvent';
167203

168204
if (domEvent.parameters && domEvent.parameters.length) {
169205
const fakeObject: any = {
170206
name: 'event',
171207
type: 'Object',
172208
collection: false,
173209
properties: [],
174-
extends: 'Event',
210+
extends: 'DOMEvent',
175211
};
176212

177213
domEvent.parameters.forEach((eventListenerProp, index) => {
@@ -365,6 +401,15 @@ export const generateModuleDeclaration = (
365401
: '';
366402
type = type || utils.typify(paramType);
367403

404+
if (type === 'Function') {
405+
type = utils.genMethodString(
406+
DynamicParamInterfaces,
407+
module,
408+
p as any, // FIXME: <--
409+
undefined,
410+
);
411+
}
412+
368413
utils.extendArray(moduleAPI, utils.wrapComment(p.description, p.additionalTags));
369414
if (module.name === 'process' && p.name === 'versions') return;
370415

src/utils.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ export const extendArray = <T>(arr1: T[], arr2: T[]): T[] => {
2828
};
2929

3030
export const wrapComment = (comment: string, additionalTags: DocumentationTag[] = []): string[] => {
31-
if (!comment) return [];
31+
if (!comment && !additionalTags.length) return [];
3232
comment = comment.replace(/^\(optional\)(?: - )?/gi, '');
33-
if (!comment) return [];
33+
if (!comment && !additionalTags.length) return [];
3434
const result = ['/**'];
3535
while (comment.length > 0) {
3636
let index = 0;
@@ -48,7 +48,7 @@ export const wrapComment = (comment: string, additionalTags: DocumentationTag[]
4848
comment = comment.substring(index + 1);
4949
}
5050
if (additionalTags.length) {
51-
result.push(' *');
51+
if (result.length > 1) result.push(' *');
5252
const nodePlatforms: string[] = [];
5353
result.push(
5454
...additionalTags
@@ -241,7 +241,6 @@ export const paramify = (paramName: string) => {
241241
// TODO: Infer through electron-docs-linter/parser
242242
export const isEmitter = (module: Pick<ModuleDocumentationContainer, 'name'>) => {
243243
const nonEventEmitters = [
244-
'menu',
245244
'menuitem',
246245
'nativeimage',
247246
'shell',

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
semver "^7.1.2"
3535
tempy "^1.0.0"
3636

37-
"@electron/docs-parser@^1.0.1":
38-
version "1.0.1"
39-
resolved "https://registry.yarnpkg.com/@electron/docs-parser/-/docs-parser-1.0.1.tgz#f9856d00ec1663a0fb6301f55bc674f44c7dc543"
40-
integrity sha512-jqUHwo3MWUhWusHtTVpSHTZqWSVuc1sPUfavI5Zwdx64q7qd4phqOPGoxScWS3JthKt7Wydvo/eReIUNDJ0gRg==
37+
"@electron/docs-parser@^1.1.0":
38+
version "1.1.0"
39+
resolved "https://registry.yarnpkg.com/@electron/docs-parser/-/docs-parser-1.1.0.tgz#ba095def41746bde56bee731feaf22272bf0b765"
40+
integrity sha512-qrjIKJk8t4/xAYldDVNQgcF8zdAAuG260bzPxdh/xI3p/yddm61bftoct+Tx2crnWFnOfOkr6nGERsDknNiT8A==
4141
dependencies:
4242
"@types/markdown-it" "^12.0.0"
4343
chai "^4.2.0"

0 commit comments

Comments
 (0)