this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cue/ast: add Interpolation.Quotes helper

Three call sites (two in cue/format, one in internal/core/compile)
independently extracted the opening and closing BasicLit fragments of an
Interpolation to feed them to literal.ParseQuotes. Fold that pattern
into a method on *ast.Interpolation.

The method panics on invariant violations — an empty Elts slice or a
non-BasicLit boundary — rather than returning an ok flag, since a valid
Interpolation must always satisfy that contract. A single-element
Interpolation returns the same BasicLit for both first and last, which
keeps the method usable for forthcoming tagged interpolations.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I0e4b9ba19e62377049f807d1e75825902fe46d3d
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1236161
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>

+21 -28
+17
cue/ast/ast.go
··· 562 562 label 563 563 } 564 564 565 + // Quotes returns the opening and closing string fragments of x. 566 + // A well-formed Interpolation has an odd number of elements whose first 567 + // and last are [*BasicLit]s; when there is only one element, it is 568 + // returned as both first and last. Quotes panics if x is empty or if 569 + // either boundary element is not a [*BasicLit]. 570 + func (x *Interpolation) Quotes() (first, last *BasicLit) { 571 + if len(x.Elts) == 0 { 572 + panic("ast.Interpolation has no elements") 573 + } 574 + first, ok1 := x.Elts[0].(*BasicLit) 575 + last, ok2 := x.Elts[len(x.Elts)-1].(*BasicLit) 576 + if !ok1 || !ok2 { 577 + panic("ast.Interpolation boundary element is not a *BasicLit") 578 + } 579 + return first, last 580 + } 581 + 565 582 // A Func node represents a function type. 566 583 // 567 584 // This is an experimental type and the contents will change without notice.
+3 -20
cue/format/node.go
··· 859 859 // position-driven newlines are emitted, collapsing the interpolation 860 860 // to a single line. 861 861 func interpolationNormalize(x *ast.Interpolation) []bool { 862 - if len(x.Elts) < 3 { 863 - return nil 864 - } 865 - first, ok := x.Elts[0].(*ast.BasicLit) 866 - if !ok { 867 - return nil 868 - } 869 - last, ok := x.Elts[len(x.Elts)-1].(*ast.BasicLit) 870 - if !ok { 871 - return nil 872 - } 862 + first, last := x.Quotes() 873 863 qi, _, _, _ := literal.ParseQuotes(first.Value, last.Value) 874 864 if !qi.IsMulti() { 875 865 // Remove newlines from all elements except the first. ··· 914 904 func (f *formatter) interpolationReindent(x *ast.Interpolation) (search, replace string) { 915 905 // TODO: only do this when simplifying, like the indent < 6 916 906 // check in the printer for token.STRING. 917 - if f.indent >= 6 || len(x.Elts) < 2 { 907 + if f.indent >= 6 { 918 908 return "", "" 919 909 } 920 - first, ok := x.Elts[0].(*ast.BasicLit) 921 - if !ok { 922 - return "", "" 923 - } 924 - last, ok := x.Elts[len(x.Elts)-1].(*ast.BasicLit) 925 - if !ok { 926 - return "", "" 927 - } 910 + first, last := x.Quotes() 928 911 qi, _, _, err := literal.ParseQuotes(first.Value, last.Value) 929 912 if err != nil || !qi.IsMulti() { 930 913 return "", ""
+1 -8
internal/core/compile/compile.go
··· 1203 1203 return c.parse(n) 1204 1204 1205 1205 case *ast.Interpolation: 1206 - if len(n.Elts) == 0 { 1207 - return c.errf(n, "invalid interpolation") 1208 - } 1209 - first, ok1 := n.Elts[0].(*ast.BasicLit) 1210 - last, ok2 := n.Elts[len(n.Elts)-1].(*ast.BasicLit) 1211 - if !ok1 || !ok2 { 1212 - return c.errf(n, "invalid interpolation") 1213 - } 1214 1206 if len(n.Elts) == 1 { 1215 1207 return c.expr(n.Elts[0]) 1216 1208 } 1209 + first, last := n.Quotes() 1217 1210 lit := &adt.Interpolation{Src: n} 1218 1211 info, prefixLen, _, err := literal.ParseQuotes(first.Value, last.Value) 1219 1212 if err != nil {