Skip to content

Commit dcfa4d0

Browse files
fix all ESLint 9 and unicorn linting issues
- Replace && expressions with if statements to avoid unused expressions - Use structuredClone instead of JSON.parse(JSON.stringify()) - Replace forEach with for...of loops - Replace Array.reduce() with for...of loops or flatMap - Replace .concat() with template literals or spread operators - Use logical operators instead of ternary where appropriate - Pass inline arrow functions instead of direct function references to map() - Add error messages to Error constructors - Disable unicorn/consistent-function-scoping for debounced MobX action
1 parent 22f13d6 commit dcfa4d0

34 files changed

Lines changed: 131 additions & 161 deletions

src/Analytics.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ export class Analytics {
110110
}
111111

112112
set(key: string, value = null) {
113-
if (value !== null) {
114-
this.customParams[key] = value
115-
} else {
113+
if (value === null) {
116114
delete this.customParams[key]
115+
} else {
116+
this.customParams[key] = value
117117
}
118118
}
119119

@@ -144,7 +144,6 @@ export class Analytics {
144144
if (label) params.el = label
145145
if (value) params.ev = value
146146

147-
// eslint-disable-next-line no-console
148147
this.send('event', params).catch(console.error)
149148
}
150149

@@ -257,27 +256,24 @@ export class Analytics {
257256
.then(res => {
258257
let response = {}
259258

260-
if (res.headers.get('content-type') !== 'image/gif') {
261-
response = res.json()
262-
} else {
263-
response = res.text()
264-
}
259+
response =
260+
res.headers.get('content-type') === 'image/gif'
261+
? res.text()
262+
: res.json()
265263

266264
if (res.status === 200) {
267265
return response
268266
}
269267

270-
return Promise.reject(new Error(response as string))
268+
throw new Error(response as string)
271269
})
272270
.then((json: any) => {
273-
if (this.globalDebug) {
274-
if (json.hitParsingResult[0].valid) {
275-
return { clientId: payload.cid }
276-
}
271+
if (this.globalDebug && json.hitParsingResult[0].valid) {
272+
return { clientId: payload.cid }
277273
}
278274

279275
return { clientId: payload.cid }
280276
})
281-
.catch(err => new Error(err))
277+
.catch(error => new Error(error))
282278
}
283279
}

src/App.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import './Styles/App.scss'
1010

1111
FocusStyleManager.onlyShowFocusOnTabs()
1212

13-
const panelElement = document.getElementById('panel')
14-
const optionsElement = document.getElementById('options')
15-
const popupElement = document.getElementById('popup')
13+
const panelElement = document.querySelector('#panel')
14+
const optionsElement = document.querySelector('#options')
15+
const popupElement = document.querySelector('#popup')
1616

17-
panelElement && render(<Panel />, panelElement)
18-
optionsElement && render(<Options />, optionsElement)
19-
popupElement && render(<Popup />, popupElement)
17+
if (panelElement) render(<Panel />, panelElement)
18+
if (optionsElement) render(<Options />, optionsElement)
19+
if (popupElement) render(<Popup />, popupElement)

src/Bridge.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ export const Bridge = new (class {
6767
}
6868

6969
init() {
70-
// eslint-disable-next-line no-console
7170
console.log('Setting up bridge...')
7271

7372
if (!browser || !browser.devtools) return
@@ -119,7 +118,6 @@ Bridge.register('sync-subscriptions', (message: Message<any>) => {
119118
})
120119

121120
Bridge.register('stats', (message: Message<any>) => {
122-
// eslint-disable-next-line no-console
123121
console.log(message.data)
124122

125123
PanelStore.setGitCommitHash(message.data.gitCommitHash)

src/Browser/Background.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,23 @@ const Cache = new Map<number, string[]>()
1212

1313
const connections: Connection = new Map()
1414

15-
self.connections = connections
15+
globalThis.connections = connections
1616

1717
const panelListener = () => {
1818
browser.runtime.onConnect.addListener(port => {
1919
console.debug('runtime.onConnect', port)
2020

2121
port.onMessage.addListener(request => {
22-
// eslint-disable-next-line no-console
2322
console.debug('port.onMessage', request)
2423

2524
if (request.name === 'init') {
2625
connections.set(request.tabId, port)
2726

2827
// Pick things from cache and send it to the panel.
2928
if (Cache.has(request.tabId)) {
30-
Cache.get(request.tabId).forEach(message => {
29+
for (const message of Cache.get(request.tabId)) {
3130
port.postMessage(message)
32-
})
31+
}
3332
}
3433

3534
port.onDisconnect.addListener(() => {
@@ -61,7 +60,7 @@ action.onClicked.addListener(e => {
6160
.create({
6261
url: 'http://cloud.meteor.com/?utm_source=chrome_extension&utm_medium=extension&utm_campaign=meteor_devtools_evolved',
6362
})
64-
// eslint-disable-next-line no-console
63+
6564
.catch(console.error)
6665
})
6766

@@ -70,10 +69,8 @@ const handleConsole = (
7069
{ data: { type, message } }: Message<{ type: ConsoleType; message: string }>,
7170
) => {
7271
if (type in console) {
73-
// eslint-disable-next-line no-console
7472
console[type](`[${tabId}]`, message)
7573
} else {
76-
// eslint-disable-next-line no-console
7774
console.warn('Wrong console type.')
7875
}
7976
}
@@ -88,7 +85,6 @@ const contentListener = () => {
8885

8986
// The message event has to from the panel to the content and then through here.
9087
if (request?.eventType === 'cache:clear') {
91-
// eslint-disable-next-line no-console
9288
console.debug('clear cache')
9389
Cache.delete(tabId)
9490
return
@@ -102,7 +98,7 @@ const contentListener = () => {
10298
if (Cache.has(tabId)) {
10399
const entry = Cache.get(tabId)
104100

105-
if (entry.length >= 10000) {
101+
if (entry.length >= 10_000) {
106102
entry.shift()
107103
}
108104

@@ -132,19 +128,17 @@ const tabListener = () => {
132128
/**
133129
* @issue https://stackoverflow.com/a/73836810/10567157
134130
*/
135-
chrome.runtime.onMessage.addListener(function (
136-
request,
137-
sender,
138-
sendResponse,
139-
) {
140-
sendResponse({ foo: true })
131+
chrome.runtime.onMessage.addListener(
132+
function (request, sender, sendResponse) {
133+
sendResponse({ foo: true })
141134

142-
if (request.source !== 'meteor-devtools-evolved') return true
135+
if (request.source !== 'meteor-devtools-evolved') return true
143136

144-
tabEvent[request.eventType]?.(request)
137+
tabEvent[request.eventType]?.(request)
145138

146-
return true
147-
})
139+
return true
140+
},
141+
)
148142
}
149143

150144
panelListener()

src/Browser/Content.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import browser from 'webextension-polyfill'
22

33
const messageHandler = (event: MessageEvent) => {
44
// Only accept messages from same frame
5-
if (event.source !== window) return
5+
if (event.source !== globalThis) return
66

77
// Only accept messages that we know are ours
88
if (event.data.source !== 'meteor-devtools-evolved') return

src/Browser/Inject.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { MeteorAdapter } from '@/Injectors/MeteorAdapter'
77

88
const isFrame = (function () {
99
try {
10-
return window.self !== window.top
11-
} catch (e) {
10+
return globalThis.self !== window.top
11+
} catch {
1212
return true
1313
}
1414
})()
@@ -41,7 +41,7 @@ const getStackTrace = (stackTraceLimit: number) => {
4141

4242
try {
4343
Error.stackTraceLimit = stackTraceLimit
44-
const error = new Error()
44+
const error = new Error('Stack trace')
4545

4646
if (!error.stack) return []
4747

@@ -66,7 +66,7 @@ const getStackTrace = (stackTraceLimit: number) => {
6666
export const sendLogMessage = (message: DDPLog) => {
6767
const stackTrace = getStackTrace(15)
6868

69-
if (stackTrace && stackTrace.length) {
69+
if (stackTrace && stackTrace.length > 0) {
7070
stackTrace.splice(0, 2)
7171
}
7272

@@ -108,17 +108,19 @@ export const Registry: IRegistry = {
108108
},
109109

110110
run(message: IMessagePayload<any>) {
111-
this.subscriptions.forEach(
112-
({ eventType, handler }) =>
111+
for (const { eventType, handler } of this.subscriptions) {
112+
if (
113113
message.source === 'meteor-devtools-evolved' &&
114-
eventType === message.eventType &&
115-
handler(message),
116-
)
114+
eventType === message.eventType
115+
) {
116+
handler(message)
117+
}
118+
}
117119
},
118120
}
119121

120122
export function injectAll() {
121-
if (!window.__meteor_devtools_evolved) {
123+
if (!globalThis.__meteor_devtools_evolved) {
122124
if (isFrame) return false
123125

124126
warning(
@@ -133,14 +135,14 @@ export function injectAll() {
133135
function inject() {
134136
--attempts
135137

136-
if (typeof Meteor === 'object' && !window.__meteor_devtools_evolved) {
137-
window.__meteor_devtools_evolved = true
138+
if (typeof Meteor === 'object' && !globalThis.__meteor_devtools_evolved) {
139+
globalThis.__meteor_devtools_evolved = true
138140

139141
DDPInjector()
140142
MinimongoInjector()
141143
MeteorAdapter()
142144

143-
window.__meteor_devtools_evolved_receiveMessage =
145+
globalThis.__meteor_devtools_evolved_receiveMessage =
144146
Registry.run.bind(Registry)
145147

146148
warning(`Initialized. Attempts: ${100 - attempts}.`)
@@ -149,7 +151,7 @@ export function injectAll() {
149151
if (attempts === 0) {
150152
clearInterval(interval)
151153

152-
if (!window.Meteor) {
154+
if (!globalThis.Meteor) {
153155
warning(
154156
isFrame
155157
? `Unable to find Meteor on iframe "${location.href}"`
@@ -161,7 +163,7 @@ export function injectAll() {
161163

162164
inject()
163165

164-
interval = window.setInterval(inject, 10)
166+
interval = globalThis.setInterval(inject, 10)
165167
}
166168
}
167169

src/Components/TabBar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ export const TabBar: FunctionComponent<Props> = ({ tabs, menu, onChange }) => {
117117
key={tab.key}
118118
onClick={() => {
119119
setKey(tab.key)
120-
onChange && onChange(tab.key)
121-
tab.handler && tab.handler()
120+
if (onChange) onChange(tab.key)
121+
if (tab.handler) tab.handler()
122122
}}
123123
className={classnames('mde-tab', {
124124
active: activeKey === tab.key,

src/Database/PanelDatabase.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,12 @@ class Database extends Dexie {
4545
}
4646

4747
async saveSettings(settings: ISettings) {
48-
if (await this.data.get('settings')) {
49-
return this.data.update('settings', settings)
50-
} else {
51-
return this.data.add({
52-
id: 'settings',
53-
...settings,
54-
})
55-
}
48+
return (await this.data.get('settings'))
49+
? this.data.update('settings', settings)
50+
: this.data.add({
51+
id: 'settings',
52+
...settings,
53+
})
5654
}
5755
}
5856

src/Injectors/MeteorAdapter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const MeteorAdapter = () => {
2727

2828
const prototype = Mongo.Collection.prototype
2929

30-
Object.entries(prototype).forEach(([key, val]) => {
30+
for (const [key, val] of Object.entries(prototype)) {
3131
if (
3232
['find', 'findOne', 'insert', 'update', 'upsert', 'remove'].includes(
3333
key,
@@ -50,5 +50,5 @@ export const MeteorAdapter = () => {
5050
return result
5151
}
5252
}
53-
})
53+
}
5454
}

src/Injectors/MinimongoInjector.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Registry, sendMessage } from '@/Browser/Inject'
33
import throttle from 'lodash.throttle'
44

55
function cloneDeep(obj: any) {
6-
return JSON.parse(JSON.stringify(obj))
6+
return structuredClone(obj)
77
}
88

99
function isArray(obj: any) {
@@ -17,7 +17,7 @@ const cleanup = (object: any) => {
1717

1818
if (!clonedObject) return clonedObject
1919

20-
Object.keys(clonedObject).forEach((key: string) => {
20+
for (const key of Object.keys(clonedObject)) {
2121
if (!clonedObject[key]) {
2222
return
2323
}
@@ -49,17 +49,15 @@ const cleanup = (object: any) => {
4949

5050
clonedObject[key] = cleanup(clonedObject[key])
5151
}
52-
})
52+
}
5353

5454
return clonedObject
5555
}
5656

5757
const getDocs = (collection: any) => {
58-
if (collection._docs._map instanceof Map) {
59-
return collection._docs._map?.values() || []
60-
} else {
61-
return Object.values(collection._docs._map || {})
62-
}
58+
return collection._docs._map instanceof Map
59+
? collection._docs._map?.values() || []
60+
: Object.values(collection._docs._map || {})
6361
}
6462

6563
const getCollections = () => {
@@ -72,12 +70,11 @@ const getCollections = () => {
7270
return
7371
}
7472

75-
const data = Object.values(collections).reduce(
76-
(acc: object, collection: any) =>
77-
Object.assign(acc, {
78-
[collection.name]: Array.from(getDocs(collection)).map(cleanup),
79-
}),
80-
{},
73+
const data = Object.fromEntries(
74+
Object.values(collections).map((collection: any) => [
75+
collection.name,
76+
[...getDocs(collection)].map(item => cleanup(item)),
77+
]),
8178
)
8279

8380
sendMessage('minimongo-get-collections', data as any)

0 commit comments

Comments
 (0)