Skip to content

Commit

Permalink
refactor: Use object.prototype to check for reserved properties (#5670)
Browse files Browse the repository at this point in the history
  • Loading branch information
YuHyeonWook authored Sep 21, 2024
1 parent e1cba8e commit 10ab90e
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/finalisers/shared/setupNamespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 10ab90e

Please sign in to comment.