fix(lib/utils/proxy)!: disallow passing type-strings to ProxifyOptions.hint

This commit is contained in:
PalmDevs 2025-07-13 19:18:41 +07:00
parent c331f1fb84
commit 996e4f5994
No known key found for this signature in database
GPG key ID: 6FAE8F8677ACA8BA
10 changed files with 23 additions and 36 deletions

View file

@ -48,7 +48,7 @@ export let ActionSheetActionCreators: DiscordModules.Actions.ActionSheetActionCr
if (module) return (ActionSheetActionCreators = module)
},
{
hint: 'object',
hint: {},
},
)!
@ -75,7 +75,7 @@ export let AlertActionCreators: DiscordModules.Actions.AlertActionCreators =
if (module) return (AlertActionCreators = module)
},
{
hint: 'object',
hint: {},
},
)!

View file

@ -51,5 +51,5 @@ export let Constants: DiscordModules.Constants = proxify(
if (module) return (Constants = module)
},
{ hint: 'object' },
{ hint: {} },
)!

View file

@ -41,7 +41,7 @@ export let Design: Design = proxify(
if (module) return (Design = module)
},
{
hint: 'object',
hint: {},
},
)!

View file

@ -34,6 +34,6 @@ export let RootNavigationRef: RootNavigationRef = proxify(
if (module) return (RootNavigationRef = module)
},
{
hint: 'object',
hint: {},
},
)!

View file

@ -42,6 +42,6 @@ export let SettingListRenderer: SettingListRenderer = proxify(
if (module) return (SettingListRenderer = module)
},
{
hint: 'object',
hint: {},
},
)!

View file

@ -27,6 +27,6 @@ export let nodeUtil: typeof import('node:util') = proxify(
if (module) return (nodeUtil = module)
},
{
hint: 'object',
hint: {},
},
)!

View file

@ -45,13 +45,13 @@ let ClipboardModule: typeof import('@react-native-clipboard/clipboard') =
if (module) return (ClipboardModule = module)
},
{
hint: 'object',
hint: {},
},
)!
export const { useClipboard } = destructure(ClipboardModule, {
hint: 'function',
hint: () => {},
})
export const { default: Clipboard } = destructure(ClipboardModule, {
hint: 'object',
hint: {},
})

View file

@ -36,7 +36,7 @@ export let ReactNavigationNative: typeof import('@react-navigation/native') =
if (module) return (ReactNavigationNative = module)
},
{
hint: 'object',
hint: {},
},
)!
@ -69,7 +69,7 @@ export let ReactNavigationStack: typeof import('@react-navigation/stack') =
if (module) return (ReactNavigationStack = module)
},
{
hint: 'object',
hint: {},
},
)!

View file

@ -37,6 +37,6 @@ export let FlashList: typeof import('@shopify/flash-list') = proxify(
if (module) return (FlashList = module)
},
{
hint: 'object',
hint: {},
},
)!

View file

@ -83,9 +83,9 @@ export interface ProxifyOptions {
/**
* The hint for the proxified value.
*
* @default 'function'
* @default function () {}
*/
hint?: 'object' | 'function' | object
hint?: object
/**
* Whether the proxified value should be cached.
*/
@ -108,7 +108,7 @@ export interface ProxifyOptions {
*
* @example Without cache
* ```ts
* const proxified = proxify(() => ({ value: Math.random() }), { hint: 'object' })
* const proxified = proxify(() => ({ value: Math.random() }), { hint: {} })
* console.log(proxified) // { value: 0.123 }
* console.log(proxified.value) // 0.456
* console.log(proxified) // { value: 0.789 }
@ -116,28 +116,15 @@ export interface ProxifyOptions {
*
* @example With cache
* ```ts
* const proxified = proxify(() => ({ value: Math.random() }), { hint: 'object', cache: true })
* const proxified = proxify(() => ({ value: Math.random() }), { hint: {}, cache: true })
* console.log(proxified) // { value: 0.123 }
* console.log(proxified.value) // 0.123
* console.log(proxified) // { value: 0.123 }
* ```
*/
export function proxify<T>(signal: () => T, options?: ProxifyOptions): T {
let hint: any
switch (options?.hint) {
case undefined:
case 'function':
// biome-ignore lint/complexity/useArrowFunction: We need a function with a constructor
hint = function () {}
break
case 'object':
hint = {}
break
default:
hint = options!.hint
break
}
// biome-ignore lint/complexity/useArrowFunction: We need a function with a constructor
const hint = options?.hint ?? function () {}
_metas.set(hint, [
signal,
@ -150,7 +137,7 @@ export function proxify<T>(signal: () => T, options?: ProxifyOptions): T {
if (v == null) DEBUG_warnNullishProxifiedValue()
}
return new Proxy(hint, _handler)
return new Proxy(hint, _handler) as T
}
/**
@ -165,7 +152,7 @@ export function proxify<T>(signal: () => T, options?: ProxifyOptions): T {
*
* @example Without cache
* ```ts
* const proxified = proxify(() => ({ value: Math.random() }), { hint: 'object' })
* const proxified = proxify(() => ({ value: Math.random() }), { hint: {} })
* const x = unproxify(proxified)
* console.log(x) // { value: 0.123 }
* console.log(x.value) // 0.123
@ -174,7 +161,7 @@ export function proxify<T>(signal: () => T, options?: ProxifyOptions): T {
*
* @example With cache
* ```ts
* const proxified = proxify(() => ({ value: Math.random() }), { hint: 'object', cache: true })
* const proxified = proxify(() => ({ value: Math.random() }), { hint: {}, cache: true })
* const x = unproxify(proxified)
* console.log(x) // { value: 0.123 }
* console.log(x.value) // 0.123
@ -209,7 +196,7 @@ function unproxifyFromHint(hint: object) {
* // cache is not turned on, so each access will call the signal again
* const { x, y } = destructure(
* proxify(() => ({ x: Math.random(), y: [Math.random()], z: null })),
* { hint: 'object' }
* { hint: {} }
* )
*
* // Non-nullish primitives are not proxifiable