From 10ab90ea612f80de21c6c433c2d792eaf7b45f1c Mon Sep 17 00:00:00 2001 From: hwook <110236953+YuHyeonWook@users.noreply.github.com> Date: Sat, 21 Sep 2024 14:01:19 +0900 Subject: [PATCH] refactor: Use object.prototype to check for reserved properties (#5670) --- src/finalisers/shared/setupNamespace.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/finalisers/shared/setupNamespace.ts b/src/finalisers/shared/setupNamespace.ts index d20f2e2ad17..5d33e1e0b8e 100644 --- a/src/finalisers/shared/setupNamespace.ts +++ b/src/finalisers/shared/setupNamespace.ts @@ -13,14 +13,18 @@ export default function setupNamespace( log?: LogHandler ): string { const parts = name.split('.'); - // Check if the key is exist in the prototype of the object - const isReserved = parts[0] in {}; + // Check if the key exists in the object's prototype. + const isReserved = parts[0] in Object.prototype; if (log && isReserved) { log(LOGLEVEL_WARN, logReservedNamespace(parts[0])); } parts[0] = - (typeof globals === 'function' ? globals(parts[0]) : isReserved ? null : globals[parts[0]]) || - parts[0]; + (typeof globals === 'function' + ? globals(parts[0]) + : isReserved + ? parts[0] + : globals[parts[0]]) || parts[0]; + parts.pop(); let propertyPath = root; @@ -43,14 +47,18 @@ export function assignToDeepVariable( log?: LogHandler ): string { const parts = deepName.split('.'); - // Check if the key is exist in the prototype of the object - const isReserved = parts[0] in {}; + // Check if the key exists in the object's prototype. + const isReserved = parts[0] in Object.prototype; if (log && isReserved) { log(LOGLEVEL_WARN, logReservedNamespace(parts[0])); } parts[0] = - (typeof globals === 'function' ? globals(parts[0]) : isReserved ? null : globals[parts[0]]) || - parts[0]; + (typeof globals === 'function' + ? globals(parts[0]) + : isReserved + ? parts[0] + : globals[parts[0]]) || parts[0]; + const last = parts.pop()!; let propertyPath = root;