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

Only enable Node lint rules for Node files #3672

Merged
merged 11 commits into from
Nov 27, 2024
17 changes: 15 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module.exports = {
root: true,
extends: ['@metamask/eslint-config', '@metamask/eslint-config-nodejs'],
extends: ['@metamask/eslint-config'],
ignorePatterns: [
'!.eslintrc.js',
'!jest.config.js',
'!.prettierrc.js',
'node_modules',
'**/dist',
'**/docs',
Expand All @@ -12,6 +12,19 @@ module.exports = {
'scripts/create-package/package-template',
],
overrides: [
{
files: [
'**/jest.config.js',
'**/jest.environment.js',
'**/tests/**/*.{ts,js}',
'*.js',
'*.test.{ts,js}',
'scripts/*.ts',
'scripts/create-package/*.ts',
'yarn.config.cjs',
],
extends: ['@metamask/eslint-config-nodejs'],
},
{
files: ['*.test.{ts,js}', '**/tests/**/*.{ts,js}'],
extends: ['@metamask/eslint-config-jest'],
Expand Down
4 changes: 4 additions & 0 deletions packages/message-manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ or

`npm install @metamask/message-manager`

## Compatibility

This package relies implicitly upon the `EventEmitter` module. This module is available natively in Node.js, but when using this package for the browser, make sure to use a polyfill such as `events`.

## Contributing

This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
2 changes: 2 additions & 0 deletions packages/message-manager/src/AbstractMessageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { BaseConfig, BaseState } from '@metamask/base-controller';
import { BaseControllerV1 } from '@metamask/base-controller';
import type { ApprovalType } from '@metamask/controller-utils';
import type { Hex, Json } from '@metamask/utils';
// This package purposefully relies on Node's EventEmitter module.
// eslint-disable-next-line import/no-nodejs-modules
import { EventEmitter } from 'events';
import { v1 as random } from 'uuid';

Expand Down
69 changes: 29 additions & 40 deletions packages/network-controller/src/NetworkController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ import { errorCodes } from '@metamask/rpc-errors';
import { createEventEmitterProxy } from '@metamask/swappable-obj-proxy';
import type { SwappableProxy } from '@metamask/swappable-obj-proxy';
import type { Hex } from '@metamask/utils';
import { isStrictHexString, hasProperty, isPlainObject } from '@metamask/utils';
import { strict as assert } from 'assert';
import { hasProperty, isPlainObject, isStrictHexString } from '@metamask/utils';
import type { Draft } from 'immer';
import type { Logger } from 'loglevel';
import { createSelector } from 'reselect';
import * as URI from 'uri-js';
import { inspect } from 'util';
import { v4 as uuidV4 } from 'uuid';

import { INFURA_BLOCKED_KEY, NetworkStatus } from './constants';
Expand Down Expand Up @@ -788,9 +786,9 @@ function validateNetworkControllerState(state: NetworkState) {

if (!networkClientIds.includes(state.selectedNetworkClientId)) {
throw new Error(
`NetworkController state is invalid: \`selectedNetworkClientId\` ${inspect(
state.selectedNetworkClientId,
)} does not refer to an RPC endpoint within a network configuration`,
// False negative - this is a string.
Copy link
Member

Choose a reason for hiding this comment

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

Huh. I can reproduce this locally as well. Strange bug.

I don't see any reports of this on the @typescript-eslint repo. Maybe this is caused by us using an incompatible version of TypeScript though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I'd be curious how the situation is once we upgrade.

Gudahtt marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`NetworkController state is invalid: \`selectedNetworkClientId\` '${state.selectedNetworkClientId}' does not refer to an RPC endpoint within a network configuration`,
);
}
}
Expand Down Expand Up @@ -1354,19 +1352,20 @@ export class NetworkController extends BaseController<
* removed in a future release
*/
async setProviderType(type: InfuraNetworkType) {
assert.notStrictEqual(
type,
NetworkType.rpc,
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`NetworkController - cannot call "setProviderType" with type "${NetworkType.rpc}". Use "setActiveNetwork"`,
);
assert.ok(
isInfuraNetworkType(type),
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`Unknown Infura provider type "${type}".`,
);
if ((type as unknown) === NetworkType.rpc) {
throw new Error(
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
Gudahtt marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`NetworkController - cannot call "setProviderType" with type "${NetworkType.rpc}". Use "setActiveNetwork"`,
);
}
if (!isInfuraNetworkType(type)) {
throw new Error(
// TODO: Either fix this lint violation or explain why it's necessary to ignore.
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`Unknown Infura provider type "${type}".`,
Gudahtt marked this conversation as resolved.
Show resolved Hide resolved
);
}

await this.setActiveNetwork(type);
}
Expand Down Expand Up @@ -1635,9 +1634,7 @@ export class NetworkController extends BaseController<

if (existingNetworkConfiguration === undefined) {
throw new Error(
`Could not update network: Cannot find network configuration for chain ${inspect(
chainId,
)}`,
`Could not update network: Cannot find network configuration for chain '${chainId}'`,
);
}

Expand Down Expand Up @@ -1899,7 +1896,7 @@ export class NetworkController extends BaseController<

if (existingNetworkConfiguration === undefined) {
throw new Error(
`Cannot find network configuration for chain ${inspect(chainId)}`,
`Cannot find network configuration for chain '${chainId}'`,
);
}

Expand Down Expand Up @@ -2031,9 +2028,7 @@ export class NetworkController extends BaseController<
!isSafeChainId(networkFields.chainId)
) {
throw new Error(
`${errorMessagePrefix}: Invalid \`chainId\` ${inspect(
networkFields.chainId,
)} (must start with "0x" and not exceed the maximum)`,
`${errorMessagePrefix}: Invalid \`chainId\` '${networkFields.chainId}' (must start with "0x" and not exceed the maximum)`,
);
}

Expand Down Expand Up @@ -2082,9 +2077,9 @@ export class NetworkController extends BaseController<
for (const rpcEndpointFields of networkFields.rpcEndpoints) {
if (!isValidUrl(rpcEndpointFields.url)) {
throw new Error(
`${errorMessagePrefix}: An entry in \`rpcEndpoints\` has invalid URL ${inspect(
rpcEndpointFields.url,
)}`,
// False negative - this is a string.
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`${errorMessagePrefix}: An entry in \`rpcEndpoints\` has invalid URL '${rpcEndpointFields.url}'`,
);
}
const networkClientId =
Expand Down Expand Up @@ -2113,13 +2108,9 @@ export class NetworkController extends BaseController<
)
) {
throw new Error(
`${errorMessagePrefix}: RPC endpoint '${
// This is a string.
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
rpcEndpointFields.url
}' refers to network client ${inspect(
networkClientId,
)} that does not exist`,
// This is a string.
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`${errorMessagePrefix}: RPC endpoint '${rpcEndpointFields.url}' refers to network client '${networkClientId}' that does not exist`,
);
}

Expand Down Expand Up @@ -2561,7 +2552,7 @@ export class NetworkController extends BaseController<
/* istanbul ignore if */
if (!possibleAutoManagedNetworkClient) {
throw new Error(
`No Infura network client found with ID ${inspect(networkClientId)}`,
`No Infura network client found with ID '${networkClientId}'`,
);
}

Expand All @@ -2573,9 +2564,7 @@ export class NetworkController extends BaseController<
];

if (!possibleAutoManagedNetworkClient) {
throw new Error(
`No network client found with ID ${inspect(networkClientId)}`,
);
throw new Error(`No network client found with ID '${networkClientId}'`);
}

autoManagedNetworkClient = possibleAutoManagedNetworkClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ export default class UserStorageController extends BaseController<
mapInternalAccountToUserStorageAccount(internalAccount);

await this.performSetStorage(
// False negative.
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`accounts.${internalAccount.address}`,
JSON.stringify(mappedAccount),
);
Expand Down
4 changes: 4 additions & 0 deletions packages/signature-controller/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ or

`npm install @metamask/signature-controller`

## Compatibility

This package relies implicitly upon the `EventEmitter` module. This module is available natively in Node.js, but when using this package for the browser, make sure to use a polyfill such as `events`.

## Contributing

This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
2 changes: 2 additions & 0 deletions packages/signature-controller/src/SignatureController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {
} from '@metamask/logging-controller';
import type { NetworkControllerGetNetworkClientByIdAction } from '@metamask/network-controller';
import type { Hex, Json } from '@metamask/utils';
// This package purposefully relies on Node's EventEmitter module.
// eslint-disable-next-line import/no-nodejs-modules
import EventEmitter from 'events';
import { v1 as random } from 'uuid';

Expand Down
4 changes: 4 additions & 0 deletions packages/transaction-controller/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ or

`npm install @metamask/transaction-controller`

## Compatibility

This package relies implicitly upon the `EventEmitter` module. This module is available natively in Node.js, but when using this package for the browser, make sure to use a polyfill such as `events`.

## Contributing

This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import type { Hex } from '@metamask/utils';
import { add0x } from '@metamask/utils';
import { Mutex } from 'async-mutex';
import { MethodRegistry } from 'eth-method-registry';
// This package purposefully relies on Node's EventEmitter module.
// eslint-disable-next-line import/no-nodejs-modules
import { EventEmitter } from 'events';
import { cloneDeep, mapValues, merge, pickBy, sortBy } from 'lodash';
import { v1 as random } from 'uuid';
Expand Down
2 changes: 2 additions & 0 deletions packages/transaction-controller/src/helpers/GasFeePoller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import type {
import type { NetworkClientId, Provider } from '@metamask/network-controller';
import type { Hex } from '@metamask/utils';
import { createModuleLogger } from '@metamask/utils';
// This package purposefully relies on Node's EventEmitter module.
// eslint-disable-next-line import/no-nodejs-modules
import EventEmitter from 'events';

import { projectLogger } from '../logger';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { AccountsController } from '@metamask/accounts-controller';
import type { BlockTracker } from '@metamask/network-controller';
import type { Hex } from '@metamask/utils';
import { Mutex } from 'async-mutex';
// This package purposefully relies on Node's EventEmitter module.
// eslint-disable-next-line import/no-nodejs-modules
import EventEmitter from 'events';

import { incomingTransactionsLogger as log } from '../logger';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import type {
BlockTracker,
NetworkClientId,
} from '@metamask/network-controller';
// This package purposefully relies on Node's EventEmitter module.
// eslint-disable-next-line import/no-nodejs-modules
import EventEmitter from 'events';
import { cloneDeep, merge } from 'lodash';

Expand Down
4 changes: 4 additions & 0 deletions packages/user-operation-controller/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ or

`npm install @metamask/user-operation-controller`

## Compatibility

This package relies implicitly upon the `EventEmitter` module. This module is available natively in Node.js, but when using this package for the browser, make sure to use a polyfill such as `events`.

## Contributing

This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
type TransactionType,
} from '@metamask/transaction-controller';
import { add0x } from '@metamask/utils';
// This package purposefully relies on Node's EventEmitter module.
// eslint-disable-next-line import/no-nodejs-modules
import EventEmitter from 'events';
import type { Patch } from 'immer';
import { cloneDeep } from 'lodash';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type {
} from '@metamask/network-controller';
import { BlockTrackerPollingControllerOnly } from '@metamask/polling-controller';
import { createModuleLogger, type Hex } from '@metamask/utils';
// This package purposefully relies on Node's EventEmitter module.
// eslint-disable-next-line import/no-nodejs-modules
import EventEmitter from 'events';

import { projectLogger } from '../logger';
Expand Down
9 changes: 7 additions & 2 deletions scripts/create-package/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jest.mock('fs', () => ({
mkdir: jest.fn(),
readFile: jest.fn(),
writeFile: jest.fn(),
stat: jest.fn(),
},
}));

Expand Down Expand Up @@ -86,7 +87,9 @@ describe('create-package/utils', () => {
nodeVersions: '>=18.0.0',
};

(fs.existsSync as jest.Mock).mockReturnValueOnce(false);
(fs.promises.stat as jest.Mock).mockResolvedValueOnce({
isDirectory: () => false,
});

(fsUtils.readAllFiles as jest.Mock).mockResolvedValueOnce({
'src/index.ts': 'export default 42;',
Expand Down Expand Up @@ -167,7 +170,9 @@ describe('create-package/utils', () => {
nodeVersions: '20.0.0',
};

(fs.existsSync as jest.Mock).mockReturnValueOnce(true);
(fs.promises.stat as jest.Mock).mockResolvedValueOnce({
isDirectory: () => true,
});

await expect(
finalizeAndWriteData(packageData, monorepoFileData),
Expand Down
4 changes: 2 additions & 2 deletions scripts/create-package/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import execa from 'execa';
import { existsSync, promises as fs } from 'fs';
import { promises as fs } from 'fs';
import path from 'path';
import { format as prettierFormat } from 'prettier';
import type { Options as PrettierOptions } from 'prettier';
Expand Down Expand Up @@ -94,7 +94,7 @@ export async function finalizeAndWriteData(
monorepoFileData: MonorepoFileData,
) {
const packagePath = path.join(PACKAGES_PATH, packageData.directoryName);
if (existsSync(packagePath)) {
if ((await fs.stat(packagePath)).isDirectory()) {
throw new Error(`The package directory already exists: ${packagePath}`);
}

Expand Down
Loading