Add rule for warning against electron.remote

Summary:
I don't think there's an easy way to do this based on types
which would be ideal ...

So instead I'm checking for

- Importing `remote` from `electron`.
- Accessing `electron.remote`.

You can still hack this by importing `electron`, saving
it to a differently named variable and accessing `remote` on it,
but this should cover all reasonable cases we see in real code.

Reviewed By: mweststrate

Differential Revision: D26453006

fbshipit-source-id: 4b3d223bed43ca3f0d1a4f592ea8f8060a823479
This commit is contained in:
Pascal Hartig
2021-02-16 04:14:04 -08:00
committed by Facebook GitHub Bot
parent fe3a6bd55e
commit d42932c111
4 changed files with 118 additions and 0 deletions

View File

@@ -10,9 +10,13 @@
import noRelativeImportsAcrossPackages, {
RULE_NAME as noRelativeImportsAcrossPackagesRuleName,
} from './rules/noRelativeImportsAcrossPackages';
import noElectronRemoteImports, {
RULE_NAME as noElectronRemoteImportsRuleName,
} from './rules/noElectronRemoteImports';
module.exports = {
rules: {
[noRelativeImportsAcrossPackagesRuleName]: noRelativeImportsAcrossPackages,
[noElectronRemoteImportsRuleName]: noElectronRemoteImports,
},
};

View File

@@ -0,0 +1,53 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import {TSESLint} from '@typescript-eslint/experimental-utils';
import rule, {RULE_NAME} from '../noElectronRemoteImports';
const tester = new TSESLint.RuleTester({
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: {
sourceType: 'module',
ecmaVersion: 2020,
},
});
tester.run(RULE_NAME, rule, {
valid: [
{
code: `import {something} from 'electron';`,
filename: __filename,
},
{
code: `import remote from 'electron';`,
filename: __filename,
},
{
code: `import remote from './electron';`,
filename: __filename,
},
],
invalid: [
{
code: `import {remote} from 'electron';`,
filename: __filename,
errors: [{messageId: 'noElectronRemoteImports'}],
},
{
code: `import {remote, somethingelse} from 'electron';`,
filename: __filename,
errors: [{messageId: 'noElectronRemoteImports'}],
},
{
code: `import {remote as notRemote} from 'electron';`,
filename: __filename,
errors: [{messageId: 'noElectronRemoteImports'}],
},
],
});

View File

@@ -0,0 +1,53 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import {TSESTree} from '@typescript-eslint/experimental-utils';
import {createESLintRule} from '../utils/createEslintRule';
type Options = [];
export type MessageIds = 'noElectronRemoteImports';
export const RULE_NAME = 'no-electron-remote-imports';
export default createESLintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description: '`remote` is slow. Please be careful when using it.',
category: 'Possible Errors',
recommended: 'warn',
},
schema: [],
messages: {
noElectronRemoteImports:
'Accessing properties on the `remote` object is blocking and 10,000x slower than a local one. Please consider alternatives or cache your access.',
},
},
defaultOptions: [],
create(context) {
return {
ImportDeclaration(node: TSESTree.ImportDeclaration) {
if (node.source.value === 'electron') {
const hasImport =
node.specifiers.filter(
(v) =>
v.type === 'ImportSpecifier' && v.imported.name === 'remote',
).length > 0;
if (hasImport) {
context.report({
node,
messageId: 'noElectronRemoteImports',
});
}
}
},
};
},
});