Add a rule for not accidentally using webContents.send directly

This commit is contained in:
Markus Olsson 2022-01-21 14:06:08 +01:00
parent f4d9ac196e
commit f076aa4b8c
2 changed files with 59 additions and 0 deletions

View file

@ -25,6 +25,7 @@ rules:
react-no-unbound-dispatcher-props: error
react-readonly-props-and-state: error
react-proper-lifecycle-methods: error
no-loosely-typed-webcontents-ipc: error
###########
# PLUGINS #

View file

@ -0,0 +1,58 @@
// @ts-check
/**
* @typedef {import('eslint').Rule.RuleModule} RuleModule
*/
function isLooselyTypesWebContentsCall(context, node) {
const { callee } = node
if (!['MemberExpression', 'OptionalMemberExpression'].includes(callee.type)) {
return
}
const prop = callee.property
if (prop.type !== 'Identifier' || prop.name !== 'send') {
return
}
// wc.send() but not foo.wc.send
if (callee.object.type === 'Identifier' && callee.object.name === 'wc') {
context.report({ node, messageId: 'useStronglyTypedWebContentsIPC' })
return
}
const obj = callee.object
// *.webContents?.send
if (obj.type === 'MemberExpression' && obj.property.name === 'webContents') {
context.report({ node, messageId: 'useStronglyTypedWebContentsIPC' })
}
// webContents.send
if (obj.type === 'Identifier' && obj.name === 'webContents') {
context.report({ node, messageId: 'useStronglyTypedWebContentsIPC' })
}
}
/** @type {RuleModule} */
module.exports = {
meta: {
docs: {
description: 'Do not use loosely typed webContents methods',
category: 'Best Practices',
},
// strings from https://github.com/Microsoft/tslint-microsoft-contrib/blob/b720cd9/src/insecureRandomRule.ts
messages: {
useStronglyTypedWebContentsIPC:
'Please use the strongly typed IPC helper methods from `ipc-webcontents` instead',
},
},
create(context) {
return {
OptionalCallExpression: n => isLooselyTypesWebContentsCall(context, n),
CallExpression: n => isLooselyTypesWebContentsCall(context, n),
}
},
}