Skip to content

Commit d892885

Browse files
committed
refactor(core): Enhance option validation in scan package
1 parent b64f13a commit d892885

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

packages/scan/src/core/index.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ export const ReactScanInternals: Internals = {
215215
Store,
216216
};
217217

218-
type LocalStorageOptions = Omit<Options,
218+
type LocalStorageOptions = Omit<
219+
Options,
219220
| 'onCommitStart'
220221
| 'onRender'
221222
| 'onCommitFinish'
@@ -253,7 +254,9 @@ const validateOptions = (options: Partial<Options>): Partial<Options> => {
253254
break;
254255
case 'animationSpeed':
255256
if (!['slow', 'fast', 'off'].includes(value as string)) {
256-
errors.push(`- Invalid animation speed "${value}". Using default "fast"`);
257+
errors.push(
258+
`- Invalid animation speed "${value}". Using default "fast"`,
259+
);
257260
} else {
258261
(validOptions as any)[key] = value;
259262
}
@@ -295,38 +298,30 @@ export const getReport = (type?: React.ComponentType<any>) => {
295298
};
296299

297300
export const setOptions = (userOptions: Partial<Options>) => {
298-
// Validate user options first
299301
const validOptions = validateOptions(userOptions);
300302

301-
// Skip if no valid options
302303
if (Object.keys(validOptions).length === 0) {
303304
return;
304305
}
305306

306-
// Special handling for sound + enabled state
307307
if ('playSound' in validOptions && validOptions.playSound) {
308308
validOptions.enabled = true;
309309
}
310310

311-
// Update options with validated values
312311
const newOptions = {
313312
...ReactScanInternals.options.value,
314-
...validOptions
313+
...validOptions,
315314
};
316315

317-
// Update instrumentation state if needed
318316
const { instrumentation } = ReactScanInternals;
319317
if (instrumentation && 'enabled' in validOptions) {
320318
instrumentation.isPaused.value = validOptions.enabled === false;
321319
}
322320

323-
// Update options
324321
ReactScanInternals.options.value = newOptions;
325322

326-
// Save to localStorage
327323
saveLocalStorage('react-scan-options', newOptions);
328324

329-
// Handle toolbar visibility only if showToolbar changed
330325
if ('showToolbar' in validOptions) {
331326
if (toolbarContainer && !newOptions.showToolbar) {
332327
toolbarContainer.remove();
@@ -418,14 +413,14 @@ const startFlushOutlineInterval = (ctx: CanvasRenderingContext2D) => {
418413
export const start = () => {
419414
if (typeof window === 'undefined') return;
420415

421-
// Load options from localStorage first
422-
const localStorageOptions = readLocalStorage<LocalStorageOptions>('react-scan-options');
416+
const localStorageOptions =
417+
readLocalStorage<LocalStorageOptions>('react-scan-options');
423418
if (localStorageOptions) {
424419
const validLocalOptions = validateOptions(localStorageOptions);
425420
if (Object.keys(validLocalOptions).length > 0) {
426421
ReactScanInternals.options.value = {
427422
...ReactScanInternals.options.value,
428-
...validLocalOptions
423+
...validLocalOptions,
429424
};
430425
}
431426
}
@@ -461,7 +456,7 @@ export const start = () => {
461456
// eslint-disable-next-line no-console
462457
console.warn(
463458
'[React Scan] Running in production mode is not recommended.\n' +
464-
'If you really need this, set dangerouslyForceRunInProduction: true in options.'
459+
'If you really need this, set dangerouslyForceRunInProduction: true in options.',
465460
);
466461
return;
467462
}

0 commit comments

Comments
 (0)