Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: infer EventEmitter extensions from API #259

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/module-declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ export const generateModuleDeclaration = (
) => {
const moduleAPI = modules[_.upperFirst(module.name)] || [];
const newModule = !modules[_.upperFirst(module.name)];
const isStaticVersion =
module.type === 'Module' &&
API.some(
(tModule, tIndex) =>
index !== tIndex && tModule.name.toLowerCase() === module.name.toLowerCase(),
);
const instanceModuleForStaticVersion = API.find(
(tModule, tIndex) =>
index !== tIndex && tModule.name.toLowerCase() === module.name.toLowerCase(),
);
const isStaticVersion = module.type === 'Module' && !!instanceModuleForStaticVersion;
const isClass = module.type === 'Class' || isStaticVersion;
const parentModules: ParsedDocumentationResult = [];
let parentModule:
Expand All @@ -53,11 +52,23 @@ export const generateModuleDeclaration = (
// Interface Declaration
if (newModule) {
if (module.type !== 'Structure') {
if (utils.isEmitter(module)) {
let extendsInfo = '';
if (module.extends) {
extendsInfo = ` extends ${module.extends}`;
} else if (
utils.isEmitter(module) ||
(isStaticVersion &&
instanceModuleForStaticVersion &&
utils.isEmitter(instanceModuleForStaticVersion))
) {
extendsInfo = ` extends ${isClass ? 'NodeEventEmitter' : 'NodeJS.EventEmitter'}`;
}
if (module.name.toLowerCase() === 'session' && isStaticVersion) {
console.log({ isStaticVersion, instanceModuleForStaticVersion, extendsInfo });
}
Comment on lines +66 to +68
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarshallOfSound, was this debug code that got left in accidentally? Seeing the output on e/e now.

if (extendsInfo) {
moduleAPI.push(
`${isClass ? 'class' : 'interface'} ${_.upperFirst(
module.name,
)} extends ${module.extends || (isClass ? 'NodeEventEmitter' : 'NodeJS.EventEmitter')} {`,
`${isClass ? 'class' : 'interface'} ${_.upperFirst(module.name)}${extendsInfo} {`,
);
moduleAPI.push('', `// Docs: ${module.websiteUrl}`, '');
} else {
Expand Down
53 changes: 25 additions & 28 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DocumentationBlock,
DetailedFunctionType,
DocumentationTag,
ParsedDocumentationResult,
} from '@electron/docs-parser';
import _ from 'lodash';
import d from 'debug';
Expand Down Expand Up @@ -257,34 +258,30 @@ export const paramify = (paramName: string) => {
}
return paramName;
};
// TODO: Infer through electron-docs-linter/parser
export const isEmitter = (module: Pick<ModuleDocumentationContainer, 'name'>) => {
const nonEventEmitters = [
'menuitem',
'nativeimage',
'shell',
'browserview',
'webrequest',
'crashreporter',
'dock',
'commandline',
'browserwindowproxy',
'clipboard',
'contenttracing',
'desktopcapturer',
'dialog',
'globalshortcut',
'powersaveblocker',
'touchbar',
'touchbarbutton',
'net',
'netlog',
'protocol',
'contextbridge',
'webframe',
'messagechannelmain',
];
return !nonEventEmitters.includes(module.name.toLowerCase());
export const isEmitter = (doc: ParsedDocumentationResult[0]) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we make this type cleaner by giving ParsedDocumentationResult[0] a type and making type ParsedDocumentationResult = MyNewType[]? Personally find that working backward from index access types makes this harder to grok at first glance.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do that, but ParsedDocumentationResult comes from @electron/docs-parser not this module. So here we are.

// Is a module, has events, is an eventemitter
if (doc.type === 'Module' && doc.events.length) {
return true;
}

// Is a class, has instance events, is an eventemitter
if (doc.type === 'Class' && doc.instanceEvents.length) {
return true;
}

// Implements the on and removeListener methods normally means
// it's an EventEmitter wrapper like ipcMain or ipcRenderer
const relevantMethods =
doc.type === 'Class' ? doc.instanceMethods : doc.type === 'Module' ? doc.methods : [];
if (
relevantMethods.find(m => m.name === 'on') &&
relevantMethods.find(m => m.name === 'removeListener')
) {
return true;
}

// Structure and Elements are not eventemitters, so bail here
return false;
};
export const isPrimitive = (type: string) => {
const primitives = ['boolean', 'number', 'any', 'string', 'void', 'unknown'];
Expand Down
11 changes: 9 additions & 2 deletions test/utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,18 @@ describe('utils', () => {

describe('isEmitter', () => {
it('should return true on most modules', () => {
expect(utils.isEmitter({ name: 'app' })).to.eq(true);
expect(utils.isEmitter({ name: 'app', type: 'Module', events: [1] })).to.eq(true);
});

it('should return false for specific non-emitter modules', () => {
expect(utils.isEmitter({ name: 'menuitem' })).to.eq(false);
expect(
utils.isEmitter({
name: 'menuitem',
type: 'Class',
instanceEvents: [],
instanceMethods: [],
}),
).to.eq(false);
});
});

Expand Down
Loading