Skip to content

Fix issues with duplicate key logic#79

Merged
littledan merged 3 commits intotc39:masterfrom
rbuckton:fix-duplicate-key-logic
Apr 17, 2018
Merged

Fix issues with duplicate key logic#79
littledan merged 3 commits intotc39:masterfrom
rbuckton:fix-duplicate-key-logic

Conversation

@rbuckton
Copy link
Copy Markdown
Collaborator

Fixes #78

spec.html Outdated
<emu-alg>
1. Let _extras_ be a new empty List.
1. Let _finishers_ be a new empty List.
1. Let _placementKeys_ be *undefined*.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it should be ~empty~, since *undefined* is the JavaScript value. Also, maybe it can just be used

1. If _element_.[[Placement]] is `"own"`, let _placementKeys_ to _keys_.[[OwnKeys]].
...

without declaring _placementKeys_ first.

(example: https://tc39.github.io/ecma262/#sec-toprimitive)

spec.html Outdated
1. Else if _element_.[[Placement]] is `"static"`, then
1. set _placementKeys_ to _keys_.[[StaticKeys]].
1. Else,
1. set _placementKeys_ to _keys_.[[PrototypeKeys]].
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this if/else if/else steps are used different times, maybe we can extract them into a different "function"?
e.g.

1. Let _placementKeys_ be GetPlacementKeys(_keys_, _element_.[[Placement]])

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to pollute the spec with an additional tangential function.

Copy link
Copy Markdown
Member

@ljharb ljharb Mar 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I’d prefer more abstract operations if it can clean up the algorithms, but generally it’s best to only do so when things are very complex or needed in more than one place.

nicolo-ribaudo added a commit to nicolo-ribaudo/babel that referenced this pull request Mar 25, 2018
@rbuckton rbuckton force-pushed the fix-duplicate-key-logic branch from ac4a9ad to 86d108b Compare March 27, 2018 22:00
nicolo-ribaudo added a commit to nicolo-ribaudo/babel that referenced this pull request Mar 29, 2018
nicolo-ribaudo added a commit to nicolo-ribaudo/babel that referenced this pull request Apr 3, 2018
spec.html Outdated
1. Append _element_.[[Key]] to _keys_.
1. Perform ? AddElementPlacement(_element_, _placements_).
1. For each _element_ in _elements_, do
1. Let _elementFinishersExtras_ be ? DecorateElement(_element_, _keys_).
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you pass placements rather than keys here?

1. Assert: _element_.[[Key]] is an element of _keys_.
1. Remove _element_.[[Key]] from _keys_.
1. For each _decorator_ in _element_.[[Decorators]], in reverse list order do
1. Perform RemoveElementPlacement(_element_, _placements_).
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This process of removing and re-adding the elements will change the enumeration order, e.g. extras may precede the element. Is this intentional?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't change order at all. placements are just bags of property names.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, never mind.

@rbuckton
Copy link
Copy Markdown
Collaborator Author

I made an additional change to not throw a TypeError when initially creating placements, since we don't deduplicate fields in CoalesceClassElements.

@rbuckton
Copy link
Copy Markdown
Collaborator Author

@littledan I've addressed PR feedback and fixed merge conflicts with master. Can you take another look?

1. Let _placements_ be the Record { [[StaticKeys]]: « », [[PrototypeKeys]]: « », [[OwnKeys]]: « » }.
1. For each _element_ in _elements_, do
1. Append _element_.[[Key]] to _keys_.
1. Perform ? AddElementPlacement(_element_, _placements_, *true*).
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially I assumed that elements was already deduplicated from CoalesceClassElements, so it would be fine to reuse AddElementPlacement as it wouldn't introduce an error for user-supplied declarations. However, upon rereading that algorithm it seems that we don't coalesce field initializers, so there exists the possibility of user-written duplicates. Since this mechanism is only to prevent decorators from adding a duplicate definition, I've modified AddElementPlacement to accept an optional silent argument to suppress errors when we initially create placements.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, makes sense to me.

Copy link
Copy Markdown
Member

@littledan littledan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for thinking this all through.

1. Assert: _element_.[[Key]] is an element of _keys_.
1. Remove _element_.[[Key]] from _keys_.
1. For each _decorator_ in _element_.[[Decorators]], in reverse list order do
1. Perform RemoveElementPlacement(_element_, _placements_).
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, never mind.

1. Let _placements_ be the Record { [[StaticKeys]]: « », [[PrototypeKeys]]: « », [[OwnKeys]]: « » }.
1. For each _element_ in _elements_, do
1. Append _element_.[[Key]] to _keys_.
1. Perform ? AddElementPlacement(_element_, _placements_, *true*).
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, makes sense to me.

@littledan
Copy link
Copy Markdown
Member

Could you rebase the patch?

@rbuckton rbuckton force-pushed the fix-duplicate-key-logic branch from 02ca37d to 5a4f66d Compare April 17, 2018 19:48
@rbuckton
Copy link
Copy Markdown
Collaborator Author

@littledan rebased

@littledan
Copy link
Copy Markdown
Member

Thanks!

@littledan littledan merged commit 694bb37 into tc39:master Apr 17, 2018
nicolo-ribaudo added a commit to nicolo-ribaudo/babel that referenced this pull request Jun 6, 2018
nicolo-ribaudo added a commit to nicolo-ribaudo/babel that referenced this pull request Jun 8, 2018
nicolo-ribaudo added a commit to nicolo-ribaudo/babel that referenced this pull request Jul 1, 2018
nicolo-ribaudo added a commit to nicolo-ribaudo/babel that referenced this pull request Jul 3, 2018
nicolo-ribaudo added a commit to nicolo-ribaudo/babel that referenced this pull request Jul 10, 2018
nicolo-ribaudo added a commit to nicolo-ribaudo/babel that referenced this pull request Jul 28, 2018
nicolo-ribaudo added a commit to nicolo-ribaudo/babel that referenced this pull request Aug 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants