fix: allow prototype method names as object keys#1586
Open
kaigritun wants to merge 1 commit intoarktypeio:mainfrom
Open
fix: allow prototype method names as object keys#1586kaigritun wants to merge 1 commit intoarktypeio:mainfrom
kaigritun wants to merge 1 commit intoarktypeio:mainfrom
Conversation
Object.prototype method names like 'constructor', 'hasOwnProperty',
and 'toString' are valid object keys and should not be incorrectly
flagged as duplicate keys.
The issue was that the 'seen' object used for duplicate detection was
initialized with '{}' which has prototype methods. When checking
'key in seen' for these keys, it returned true even though they
weren't explicitly set.
Fix: Use Object.create(null) to create a prototype-free object for
the 'seen' tracking.
Fixes arktypeio#1572
Contributor
|
Leaping into action... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Fixes #1572
Object.prototype method names like
constructor,hasOwnProperty, andtoStringare valid object keys and should not be incorrectly flagged as duplicate keys.Problem
When defining an object type with a key that matches an
Object.prototypemethod name:This throws a
ParseError: Duplicate key "constructor"even though the key is valid.Root Cause
The
seenobject used for duplicate key detection was initialized with{}, which inherits fromObject.prototype. When checkingkey in seen, it returnedtruefor keys like"constructor"because they exist on the prototype chain.Solution
Use
Object.create(null)to create a prototype-free object for theseentracking. This ensures only explicitly set keys are detected as duplicates.Testing
Added a test case to verify that prototype method names can be used as object keys without error.