Skip to content

Commit 312d7f0

Browse files
committed
fix lint errors
1 parent a362874 commit 312d7f0

File tree

12 files changed

+90
-169
lines changed

12 files changed

+90
-169
lines changed

src/core/index.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { createPerfObserver } from './web/perf-observer';
1313
import { initReactScanOverlay } from './web/overlay';
1414
import {
1515
createInspectElementStateMachine,
16-
States,
16+
type States,
1717
} from './web/inspect-element/inspect-state-machine';
1818
import { createToolbar } from './web/toolbar';
1919

@@ -112,6 +112,7 @@ export interface Internals {
112112
count: number;
113113
time: number;
114114
badRenders: Render[];
115+
displayName: string | null;
115116
}
116117
>;
117118
reportData: Record<
@@ -129,21 +130,24 @@ export interface Internals {
129130
type Listener<T> = (value: T) => void;
130131

131132
export interface StoreMethods<T extends object> {
132-
subscribe<K extends keyof T>(key: K, listener: Listener<T[K]>): () => void;
133-
set<K extends keyof T>(key: K, value: T[K]): void;
134-
setState(state: Partial<T>): void;
135-
emit<K extends keyof T>(key: K, value: T[K]): void;
136-
subscribeMultiple(
137-
subscribeTo: Array<keyof T>,
133+
subscribe: <K extends keyof T>(
134+
key: K,
135+
listener: Listener<T[K]>,
136+
) => () => void;
137+
set: <K extends keyof T>(key: K, value: T[K]) => void;
138+
setState: (state: Partial<T>) => void;
139+
emit: <K extends keyof T>(key: K, value: T[K]) => void;
140+
subscribeMultiple: (
141+
subscribeTo: (keyof T)[],
138142
listener: Listener<T>,
139-
): () => void;
143+
) => () => void;
140144
}
141145

142146
type Store<T extends object> = T & StoreMethods<T>;
143147

144148
const createStore = <T extends object>(initialData: T): Store<T> => {
145149
const data: T = { ...initialData };
146-
const listeners: { [K in keyof T]?: Array<Listener<T[K]>> } = {};
150+
const listeners: { [K in keyof T]?: Listener<T[K]>[] } = {};
147151

148152
const emit = <K extends keyof T>(key: K, value: T[K]): void => {
149153
listeners[key]?.forEach((listener) => listener(value));
@@ -172,14 +176,14 @@ const createStore = <T extends object>(initialData: T): Store<T> => {
172176

173177
const setState = (state: Partial<T>) => {
174178
for (const key in state) {
175-
if (state.hasOwnProperty(key)) {
179+
if (Object.prototype.hasOwnProperty.call(state, key)) {
176180
set(key as keyof T, state[key] as T[keyof T]);
177181
}
178182
}
179183
};
180184

181185
const subscribeMultiple = (
182-
subscribeTo: Array<keyof T>,
186+
subscribeTo: (keyof T)[],
183187
listener: (store: typeof data) => void,
184188
) => {
185189
subscribeTo.forEach((key) => {
@@ -191,9 +195,7 @@ const createStore = <T extends object>(initialData: T): Store<T> => {
191195

192196
return () => {
193197
subscribeTo.forEach((key) => {
194-
listeners[key as keyof T] = listeners[key as keyof T]?.filter(
195-
(cb) => cb !== listener,
196-
);
198+
listeners[key] = listeners[key]?.filter((cb) => cb !== listener);
197199
});
198200
};
199201
};
@@ -208,13 +210,12 @@ const createStore = <T extends object>(initialData: T): Store<T> => {
208210

209211
return Reflect.get(target, prop, receiver);
210212
},
211-
set(target, prop, value, receiver) {
213+
set(target, prop, value) {
212214
if (prop in target) {
213215
set(prop as keyof T, value as T[keyof T]);
214216
return true;
215-
} else {
216-
throw new Error(`Property "${String(prop)}" does not exist`);
217217
}
218+
throw new Error(`Property "${String(prop)}" does not exist`);
218219
},
219220
deleteProperty(_, prop) {
220221
throw new Error(`Cannot delete property "${String(prop)}" from store`);

src/core/instrumentation/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Fiber, FiberRoot } from 'react-reconciler';
22
import * as React from 'react';
3-
import { type NO_OP } from '../utils';
43
import { ReactScanInternals } from '../index';
54
import { getDisplayName, fastSerialize, getType } from './utils';
65
import {
@@ -182,8 +181,7 @@ export const reportRenderFiber = (fiber: Fiber, renders: (Render | null)[]) => {
182181
ReactScanInternals.reportDataByFiber.set(reportFiber, {
183182
count: (report?.count ?? 0) + 1,
184183
time: (report?.time ?? 0) + (time !== 0 ? time : 0.1), // .1ms lowest precision
185-
badRenders: report?.badRenders || [],
186-
// @ts-expect-error
184+
badRenders: report?.badRenders ?? [],
187185
displayName: getDisplayName(fiber.type),
188186
});
189187
ReactScanInternals.emit(

src/core/instrumentation/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const fastSerialize = (value: unknown) => {
5151
}
5252
};
5353

54-
export const getType = (type: any) => {
54+
export const getType = (type: any): any => {
5555
if (typeof type === 'function') {
5656
return type;
5757
}

src/core/utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { ReactScanInternals } from '../index';
21
import type { Render } from './instrumentation/index';
32

43
export const NO_OP = () => {

src/core/web/custom-element.ts

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/core/web/inspect-element/inspect-state-machine.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { Fiber } from 'react-reconciler';
2-
import { Internals, ReactScanInternals } from '../../index';
3-
1+
import { type Internals, ReactScanInternals } from '../../index';
42
import { throttle } from '../utils';
3+
import { didFiberRender } from '../../instrumentation/fiber';
54
import { renderPropsAndState } from './view-state';
65
import {
76
currentLockIconRect,
@@ -10,7 +9,6 @@ import {
109
updateCanvasSize,
1110
} from './overlay';
1211
import { getCompositeComponentFromElement, hasValidParent } from './utils';
13-
import { didFiberRender } from '../../instrumentation/fiber';
1412

1513
export type States =
1614
| {
@@ -62,7 +60,7 @@ export const createInspectElementStateMachine = () => {
6260
if (!ctx) {
6361
return;
6462
}
65-
updateCanvasSize(canvas!, ctx);
63+
updateCanvasSize(canvas, ctx);
6664
window.addEventListener('resize', () => {
6765
updateCanvasSize(canvas!, ctx);
6866
}); // todo add cleanup/dispose logic for createInspectElementStateMachine
@@ -82,11 +80,11 @@ export const createInspectElementStateMachine = () => {
8280

8381
ctx.restore();
8482
};
85-
let unsubscribeFns: Partial<{ [_ in keyof States as Kinds]: () => void }> =
83+
const unsubscribeFns: Partial<{ [_ in keyof States as Kinds]: () => void }> =
8684
{};
8785

8886
const unsubscribeAll = () => {
89-
Object.entries(unsubscribeFns).forEach(([unSubKey, unSub]) => {
87+
Object.entries(unsubscribeFns).forEach(([_, unSub]) => {
9088
unSub();
9189
});
9290
};
@@ -148,11 +146,11 @@ export const createInspectElementStateMachine = () => {
148146
top: 0;
149147
width: 100vw;
150148
height: 100vh;
151-
z-index: ${parseInt(canvas!.style.zIndex) - 1};
149+
z-index: ${parseInt(canvas.style.zIndex) - 1};
152150
pointer-events: auto;
153151
`;
154152

155-
canvas!.parentNode!.insertBefore(eventCatcher, canvas);
153+
canvas.parentNode!.insertBefore(eventCatcher, canvas);
156154
let currentHoveredElement: HTMLElement | null = null;
157155
const mouseMove = throttle((e: MouseEvent) => {
158156
if (ReactScanInternals.inspectState.kind !== 'inspecting') {
@@ -222,7 +220,9 @@ export const createInspectElementStateMachine = () => {
222220
}
223221
};
224222
window.addEventListener('keydown', keyDown);
225-
let cleanup = () => {};
223+
let cleanup = () => {
224+
/**/
225+
};
226226
if (inspectState.hoveredDomElement) {
227227
cleanup = trackElementPosition(
228228
inspectState.hoveredDomElement,
@@ -274,14 +274,14 @@ export const createInspectElementStateMachine = () => {
274274
);
275275
const element = inspectState.focusedDomElement;
276276

277-
let { parentCompositeFiber } =
277+
const { parentCompositeFiber } =
278278
getCompositeComponentFromElement(element);
279279
if (!parentCompositeFiber) {
280280
return;
281281
}
282282

283283
const reportDataFiber =
284-
store.reportDataByFiber.get(parentCompositeFiber) ||
284+
store.reportDataByFiber.get(parentCompositeFiber) ??
285285
(parentCompositeFiber.alternate
286286
? store.reportDataByFiber.get(parentCompositeFiber.alternate)
287287
: null);
@@ -388,7 +388,9 @@ export const createInspectElementStateMachine = () => {
388388
}, 16),
389389
);
390390

391-
return () => {};
391+
return () => {
392+
/**/
393+
};
392394
};
393395
type CleanupFunction = () => void;
394396
type PositionCallback = (element: Element) => void;

src/core/web/inspect-element/overlay.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Fiber } from 'react-reconciler';
1+
import type { Fiber } from 'react-reconciler';
22
import { ReactScanInternals } from '../..';
33
import { getDisplayName } from '../../instrumentation/utils';
44
import { getCompositeComponentFromElement } from './utils';
@@ -23,6 +23,7 @@ interface PerformanceStats {
2323
}
2424

2525
let currentRect: Rect | null = null;
26+
// eslint-disable-next-line import/no-mutable-exports
2627
export let currentLockIconRect: LockIconRect | null = null;
2728
export const OVERLAY_DPR: number =
2829
typeof window !== 'undefined' ? window.devicePixelRatio || 1 : 1;
@@ -45,7 +46,7 @@ export const drawHoverOverlay = (
4546
}
4647

4748
const reportDataFiber =
48-
ReactScanInternals.reportDataByFiber.get(parentCompositeFiber) ||
49+
ReactScanInternals.reportDataByFiber.get(parentCompositeFiber) ??
4950
(parentCompositeFiber.alternate
5051
? ReactScanInternals.reportDataByFiber.get(parentCompositeFiber.alternate)
5152
: null);
@@ -161,7 +162,9 @@ export const drawStatsPill = (
161162
) => {
162163
const pillHeight = 24;
163164
const pillPadding = 8;
164-
const componentName = fiber ? getDisplayName(fiber) || 'Unknown' : 'Unknown';
165+
const componentName = fiber
166+
? (getDisplayName(fiber) ?? 'Unknown')
167+
: 'Unknown';
165168
const text = `${componentName} • x${stats.count} (${stats.time.toFixed(1)}ms)`;
166169

167170
ctx.save();

src/core/web/inspect-element/utils.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Fiber, FiberRoot } from 'react-reconciler';
1+
import type { Fiber } from 'react-reconciler';
22
import { ReactScanInternals } from '../../index';
33
import {
44
FunctionComponentTag,
@@ -11,7 +11,7 @@ import { getRect } from '../outline';
1111

1212
export const getFiberFromElement = (element: HTMLElement): Fiber | null => {
1313
if ('__REACT_DEVTOOLS_GLOBAL_HOOK__' in window) {
14-
const { renderers } = window.__REACT_DEVTOOLS_GLOBAL_HOOK__!;
14+
const { renderers } = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
1515
if (!renderers) return null;
1616
for (const [_, renderer] of Array.from(renderers)) {
1717
try {
@@ -56,8 +56,8 @@ export const getFirstStateNode = (fiber: Fiber): HTMLElement | null => {
5656

5757
export const getNearestFiberFromElement = (element: HTMLElement | null) => {
5858
if (!element) return null;
59-
let target: HTMLElement | null = element;
60-
let originalFiber = getFiberFromElement(target);
59+
const target: HTMLElement | null = element;
60+
const originalFiber = getFiberFromElement(target);
6161
if (!originalFiber) {
6262
return null;
6363
}
@@ -119,9 +119,8 @@ export const getStateFromFiber = (fiber: Fiber): any => {
119119
} else if (fiber.tag === ClassComponentTag) {
120120
// Class component, memoizedState is the component state
121121
return fiber.memoizedState || {};
122-
} else {
123-
return {};
124122
}
123+
return {};
125124
};
126125

127126
export const getChangedState = (fiber: Fiber): Set<string> => {
@@ -161,7 +160,7 @@ export const isCurrentTree = (fiber: Fiber) => {
161160
return false;
162161
}
163162

164-
const fiberRoot = rootFiber.stateNode as FiberRoot;
163+
const fiberRoot = rootFiber.stateNode;
165164
const currentRootFiber = fiberRoot.current;
166165

167166
return isFiberInTree(fiber, currentRootFiber);
@@ -246,7 +245,7 @@ export const hasValidParent = () => {
246245

247246
let hasValidParent = false;
248247
if (focusedDomElement.parentElement) {
249-
let currentFiber = getNearestFiberFromElement(focusedDomElement);
248+
const currentFiber = getNearestFiberFromElement(focusedDomElement);
250249
let nextParent: typeof focusedDomElement.parentElement | null =
251250
focusedDomElement.parentElement;
252251

0 commit comments

Comments
 (0)