Skip to content

refactor: Use object.prototype to check for reserved properties #5670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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