fix: bind a const pattern element to its default rather than assigning it - #59
Merged
Merged
Conversation
…g it
A const binding whose default was actually taken failed at run time:
const [a = 7] = []; TypeError: Assignment to constant "a"
const { x = 8 } = {}; same
const [a = 7] = [1]; 1, fine - the default was not needed
declareBindings emitted the declaration and then filled the default in with a
follow-up assignment, "if (a === undefined) a = 7;". A const binding cannot be
assigned to a second time, so the guard threw whenever it fired. let and var
were unaffected, which is why the default path looked like it worked.
The default now goes into the initializer instead, as a conditional. The value is
read once into a temporary first and the temporary is what gets tested and used,
rather than testing the read expression and then reading it again - a getter is
still invoked exactly once, as it was with the guard.
Found while adding patterns to for-of; it is not specific to loops and reproduces
on a plain declaration.
./gradlew build testOptimistic testPessimistic:
suite before after
test 666, 0 fail 666, 0 fail
testOptimistic 1718, 0 fail 1718, 0 fail
testPessimistic 1718, 0 fail 1718, 0 fail
marevol
force-pushed
the
es6/fix-const-pattern-default
branch
from
August 28, 2026 05:11
68cb1de to
ffc3485
Compare
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.
Stacked on #58.
A
constbinding whose default was actually taken failed at run time:Cause
declareBindingsemitted the declaration and then filled the default in with a follow-up assignment,if (a === undefined) a = 7;. Aconstbinding cannot be assigned to a second time, so the guard threw whenever it fired.letandvarwere unaffected, which is why the default path looked like it worked.Fix
The default now goes into the initializer instead, as a conditional. The value is read once into a temporary first and the temporary is what gets tested and used, rather than testing the read expression and then reading it again — a getter is still invoked exactly once, as it was with the guard.
Found while adding patterns to for-of (#58); it is not specific to loops and reproduces on a plain declaration.
Verification
./gradlew build testOptimistic testPessimistic