cue/format: fix incorrect quote removal in fmt -s
Fix https://cuelang.org/cl/1193386 introduced a regression in which
certain labels will have their quotes removed, resulting in modifying
the semantics of the original CUE.
The current algorithm is split into two:
1. It identifies, for each string in the AST, whether its quotes can
be removed without altering the original semantics of the CUE.
2. Based on the collected data, it then strips quotes from strings
where applicable.
The above CL incorrectly caused the string replacement step (2) to walk
each ast node entirely, instead of only handling the node label. This
caused the scope information to be incorrectly interpreted, since it is
only meant for a specific label and not the entire subtree.
Take for example the following CUE:
foo: {}
bar: "foo": foo
When the algorithm reaches the node representing "bar", it will now
descend into both the label node (ident bar) and the value node
(the {"foo": foo} struct). But the scoping information is built in
such a way that it's applicable only to the bar label - not the entire
tree located under bar. The data then causes "foo" to incorrectly be
replaced with foo.
Note that this introduces a minor "regression" in which fields are not
simplified inside of complex label, so that the following expression
will no longer be simplified:
[!={"a": 1}.a]: {}
It is now clear to me that the original algorithm intentionally did not
take such cases into account, and they were simplified because of the
above bug.
Fixes #3189.
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
Change-Id: I84c2db55f62054950ad77c0fa69a777218fcc86d
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1195742
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
authored by