formatter: preserve [expr] key syntax in object comprehensions#880
Open
ricardbejarano wants to merge 1 commit into
Open
formatter: preserve [expr] key syntax in object comprehensions#880ricardbejarano wants to merge 1 commit into
ricardbejarano wants to merge 1 commit into
Conversation
The PrettyFieldNames formatter pass was incorrectly simplifying
computed string keys inside object comprehensions, e.g. turning
['0'] into '0'. While this rewrite is valid in regular objects
(where {['a']: 1} == {a: 1}), object comprehensions require
the [expr] key syntax; the simplified form is rejected by the parser.
The fix tracks whether we are currently visiting an ObjectComp
and, if so, skips the key-kind rewrite while still recursing into
the field's value expression.
Fixes google#879
Signed-off-by: bejaratommy <tommy@bejara.net>
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.
Fixes #879.
Problem
jsonnetfmtwas producing invalid output for object comprehensions with literal computed keys. Given:{['0']: 1 for x in [1]}the formatter would output:
{ '0': 1 for x in [1] }which fails to parse — the evaluator rejects it with:
The
['literal'] -> literalsimplification applied byPrettyFieldNamesis correct for regular objects (since{['a']: 1} == {a: 1}), but object comprehensions require the[expr]key syntax, so the rewrite must not be applied there.Fix
Override
ObjectCompinPrettyFieldNamesto set a flag while traversing comprehension fields, and skip the key-kind rewrite when that flag is set. The field's value expression is still visited normally.A golden test case is added to cover the bug.