Skip to content

Commit 0ed31aa

Browse files
authored
chore(lint): Fix suppressed ESLint errors in base-controller package (#7443)
## Explanation This fixes all suppressed ESLint errors in the `base-controller` package. ## References Closes #7344. ## Checklist - [ ] I've updated the test suite for new or updated code as appropriate - [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [ ] I've communicated my changes to consumers by [updating changelogs for packages I've changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs) - [ ] I've introduced [breaking changes](https://github.com/MetaMask/core/tree/main/docs/breaking-changes.md) in this PR and have prepared draft pull requests for clients and consumer packages to resolve them <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Refactors BaseController types and tests to satisfy ESLint by adding explicit return types, refining generics, and cleaning up imports; removes related suppressions. > > - **Core (`packages/base-controller/src/BaseController.ts`)** > - Add explicit return types to `state` getter, `applyPatches`, `destroy`, and `update` result. > - Rename generic/type params for clarity (`T`→`Type`), and tighten typings (`StateChangeListener`, `StateDeriver`, `StateMetadata`). > - Minor callback param rename in `produceWithPatches` cast (`callbackFn`). > - **Tests (`packages/base-controller/src/BaseController.test.ts`)** > - Add explicit return types to controller overrides and helper functions; annotate arrow properties. > - Switch `sinon` import to default; reorder imports; add `// eslint-disable-next-line no-new` where needed. > - **Lint config** > - Update `eslint-suppressions.json` to remove suppressions for `base-controller` files now addressed. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 71469bd. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 9d56202 commit 0ed31aa

3 files changed

Lines changed: 46 additions & 51 deletions

File tree

eslint-suppressions.json

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -699,28 +699,6 @@
699699
"count": 3
700700
}
701701
},
702-
"packages/base-controller/src/BaseController.test.ts": {
703-
"@typescript-eslint/explicit-function-return-type": {
704-
"count": 15
705-
},
706-
"import-x/namespace": {
707-
"count": 13
708-
},
709-
"no-new": {
710-
"count": 2
711-
}
712-
},
713-
"packages/base-controller/src/BaseController.ts": {
714-
"@typescript-eslint/explicit-function-return-type": {
715-
"count": 3
716-
},
717-
"@typescript-eslint/naming-convention": {
718-
"count": 3
719-
},
720-
"id-denylist": {
721-
"count": 1
722-
}
723-
},
724702
"packages/bridge-controller/src/bridge-controller.test.ts": {
725703
"@typescript-eslint/explicit-function-return-type": {
726704
"count": 2

packages/base-controller/src/BaseController.test.ts

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* eslint-disable jest/no-export */
2-
import { MOCK_ANY_NAMESPACE, Messenger } from '@metamask/messenger';
32
import type { MockAnyNamespace } from '@metamask/messenger';
3+
import { Messenger, MOCK_ANY_NAMESPACE } from '@metamask/messenger';
44
import type { Json } from '@metamask/utils';
55
import type { Draft, Patch } from 'immer';
6-
import * as sinon from 'sinon';
6+
import sinon from 'sinon';
77

88
import type {
99
ControllerActions,
@@ -67,16 +67,19 @@ export class CountController extends BaseController<
6767
callback: (
6868
state: Draft<CountControllerState>,
6969
) => void | CountControllerState,
70-
) {
71-
const res = super.update(callback);
72-
return res;
70+
): {
71+
nextState: CountControllerState;
72+
patches: Patch[];
73+
inversePatches: Patch[];
74+
} {
75+
return super.update(callback);
7376
}
7477

75-
applyPatches(patches: Patch[]) {
78+
applyPatches(patches: Patch[]): void {
7679
super.applyPatches(patches);
7780
}
7881

79-
destroy() {
82+
destroy(): void {
8083
super.destroy();
8184
}
8285
}
@@ -140,16 +143,19 @@ class MessagesController extends BaseController<
140143
callback: (
141144
state: Draft<MessagesControllerState>,
142145
) => void | MessagesControllerState,
143-
) {
144-
const res = super.update(callback);
145-
return res;
146+
): {
147+
nextState: MessagesControllerState;
148+
patches: Patch[];
149+
inversePatches: Patch[];
150+
} {
151+
return super.update(callback);
146152
}
147153

148-
applyPatches(patches: Patch[]) {
154+
applyPatches(patches: Patch[]): void {
149155
super.applyPatches(patches);
150156
}
151157

152-
destroy() {
158+
destroy(): void {
153159
super.destroy();
154160
}
155161
}
@@ -172,6 +178,8 @@ describe('BaseController', () => {
172178

173179
it('should allow getting state via the getState action', () => {
174180
const messenger = getCountMessenger();
181+
182+
// eslint-disable-next-line no-new
175183
new CountController({
176184
messenger,
177185
name: countControllerName,
@@ -542,6 +550,8 @@ describe('BaseController', () => {
542550

543551
it('should throw when unsubscribing listener who was never subscribed', () => {
544552
const messenger = getCountMessenger();
553+
554+
// eslint-disable-next-line no-new
545555
new CountController({
546556
messenger,
547557
name: 'CountController',
@@ -633,19 +643,19 @@ describe('BaseController', () => {
633643
messenger.registerActionHandler('VisitorController:clear', this.clear);
634644
}
635645

636-
clear = () => {
646+
clear: () => void = () => {
637647
this.update(() => {
638648
return { visitors: [] };
639649
});
640650
};
641651

642-
addVisitor(visitor: string) {
652+
addVisitor(visitor: string): void {
643653
this.update(({ visitors }) => {
644654
return { visitors: [...visitors, visitor] };
645655
});
646656
}
647657

648-
destroy() {
658+
destroy(): void {
649659
super.destroy();
650660
}
651661
}
@@ -709,19 +719,21 @@ describe('BaseController', () => {
709719
messenger.subscribe('VisitorController:stateChange', this.onVisit);
710720
}
711721

712-
onVisit = ({ visitors }: VisitorControllerState) => {
722+
onVisit: ({ visitors }: VisitorControllerState) => void = ({
723+
visitors,
724+
}: VisitorControllerState) => {
713725
if (visitors.length > this.state.maxVisitors) {
714726
this.messenger.call('VisitorController:clear');
715727
}
716728
};
717729

718-
updateMax = (max: number) => {
730+
updateMax: (max: number) => void = (max: number) => {
719731
this.update(() => {
720732
return { maxVisitors: max };
721733
});
722734
};
723735

724-
destroy() {
736+
destroy(): void {
725737
super.destroy();
726738
}
727739
}
@@ -876,7 +888,7 @@ describe('deriveStateFromMetadata', () => {
876888

877889
if (property !== 'usedInUi') {
878890
it('should use function to derive state', () => {
879-
const normalizeTransactionHash = (hash: string) => {
891+
const normalizeTransactionHash = (hash: string): string => {
880892
return hash.toLowerCase();
881893
};
882894

@@ -900,7 +912,12 @@ describe('deriveStateFromMetadata', () => {
900912
});
901913

902914
it('should allow returning a partial object from a deriver', () => {
903-
const getDerivedTxMeta = (txMeta: { hash: string; value: number }) => {
915+
const getDerivedTxMeta = (txMeta: {
916+
hash: string;
917+
value: number;
918+
}): {
919+
value: number;
920+
} => {
904921
return { value: txMeta.value };
905922
};
906923

@@ -931,7 +948,7 @@ describe('deriveStateFromMetadata', () => {
931948
hash: string;
932949
value: number;
933950
history: { hash: string; value: number }[];
934-
}) => {
951+
}): { history: { value: number }[]; value: number } => {
935952
return {
936953
history: txMeta.history.map((entry) => {
937954
return { value: entry.value };

packages/base-controller/src/BaseController.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export type StateConstraint = Record<string, Json>;
2929
* @param patches - A list of patches describing any changes (see here for more
3030
* information: https://immerjs.github.io/immer/docs/patches)
3131
*/
32-
export type StateChangeListener<T> = (state: T, patches: Patch[]) => void;
32+
export type StateChangeListener<Type> = (state: Type, patches: Patch[]) => void;
3333

3434
/**
3535
* An function to derive state.
@@ -40,16 +40,16 @@ export type StateChangeListener<T> = (state: T, patches: Patch[]) => void;
4040
* @param value - A piece of controller state.
4141
* @returns Something derived from controller state.
4242
*/
43-
export type StateDeriver<T extends Json> = (value: T) => Json;
43+
export type StateDeriver<Type extends Json> = (value: Type) => Json;
4444

4545
/**
4646
* State metadata.
4747
*
4848
* This metadata describes which parts of state should be persisted, and how to
4949
* get an anonymized representation of the state.
5050
*/
51-
export type StateMetadata<T extends StateConstraint> = {
52-
[P in keyof T]-?: StatePropertyMetadata<T[P]>;
51+
export type StateMetadata<Type extends StateConstraint> = {
52+
[Key in keyof Type]-?: StatePropertyMetadata<Type[Key]>;
5353
};
5454

5555
/**
@@ -276,7 +276,7 @@ export class BaseController<
276276
*
277277
* @returns The current state.
278278
*/
279-
get state() {
279+
get state(): ControllerState {
280280
return this.#internalState;
281281
}
282282

@@ -309,7 +309,7 @@ export class BaseController<
309309
const [nextState, patches, inversePatches] = (
310310
produceWithPatches as unknown as (
311311
state: ControllerState,
312-
cb: typeof callback,
312+
callbackFn: typeof callback,
313313
) => [ControllerState, Patch[], Patch[]]
314314
)(this.#internalState, callback);
315315

@@ -333,7 +333,7 @@ export class BaseController<
333333
* @param patches - An array of immer patches that are to be applied to make
334334
* or undo changes.
335335
*/
336-
protected applyPatches(patches: Patch[]) {
336+
protected applyPatches(patches: Patch[]): void {
337337
const nextState = applyPatches(this.#internalState, patches);
338338
this.#internalState = nextState;
339339
this.#messenger.publish(
@@ -352,7 +352,7 @@ export class BaseController<
352352
* least ensures this instance won't be responsible for preventing the
353353
* listeners from being garbage collected.
354354
*/
355-
protected destroy() {
355+
protected destroy(): void {
356356
this.messenger.clearEventSubscriptions(`${this.name}:stateChange`);
357357
}
358358
}

0 commit comments

Comments
 (0)