Skip to content

Commit

Permalink
cue/parser: better error messages for use of deprecated constructs
Browse files Browse the repository at this point in the history
Also:
- detects old-style aliases in one more case
- does not unconditionally suggest the use of using
  v0.2.2 for cue fix, as newer versions work too in most
  cases.
- updates the current version: not really necessary, as
  there isn't anything that was depracated since.

Fixes #945

Change-Id: I555c91d979ee8c16c236accc63683d11a585d9a7
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9686
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
  • Loading branch information
mpvl committed May 5, 2021
1 parent c2a68a9 commit 3e10918
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions cue/parser/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ type DeprecationError struct {
}

func (e *DeprecationError) Error() string {
return "try running `cue fix` using CUE v0.2.2 on the file or module to upgrade"
return "try running `cue fix` (possibly with an earlier version, like v0.2.2) to upgrade"
}

// Latest specifies the latest version of the parser, effectively setting
// the strictest implementation.
const Latest = latest

const latest = 1000
const latest = -600

// FileOffset specifies the File position info to use.
func FileOffset(pos int) Option {
Expand Down
10 changes: 5 additions & 5 deletions cue/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ func (p *parser) assertV0(pos token.Pos, minor, patch int, name string) {
if p.version != 0 && p.version > v {
p.errors = errors.Append(p.errors,
errors.Wrapf(&DeprecationError{v}, pos,
"%s deprecated as of v0.%d.%d", name, minor, patch+1))
"use of deprecated %s (deprecated as of v0.%d.%d)", name, minor, patch+1))
}
}

Expand Down Expand Up @@ -849,6 +849,7 @@ func (p *parser) parseField() (decl ast.Decl) {
expr = p.parseRHS()
}
if a, ok := expr.(*ast.Alias); ok {
p.assertV0(a.Pos(), 1, 3, `old-style alias; use "let X = expr" instead`)
p.consumeDeclComma()
return a
}
Expand Down Expand Up @@ -876,8 +877,7 @@ func (p *parser) parseField() (decl ast.Decl) {

case token.RBRACE, token.EOF:
if a, ok := expr.(*ast.Alias); ok {
p.assertV0(p.pos, 1, 3, `old-style alias; use "let X = expr"`)

p.assertV0(a.Pos(), 1, 3, `old-style alias; use "let X = expr" instead`)
return a
}
switch tok {
Expand All @@ -897,7 +897,7 @@ func (p *parser) parseField() (decl ast.Decl) {
m.TokenPos = p.pos
m.Token = p.tok
if p.tok == token.ISA {
p.assertV0(p.pos, 2, 0, "use of '::'")
p.assertV0(p.pos, 2, 0, "'::'")
}
if p.tok != token.COLON && p.tok != token.ISA {
p.errorExpected(pos, "':' or '::'")
Expand Down Expand Up @@ -930,7 +930,7 @@ func (p *parser) parseField() (decl ast.Decl) {
m.TokenPos = p.pos
m.Token = p.tok
if p.tok == token.ISA {
p.assertV0(p.pos, 2, 0, "use of '::'")
p.assertV0(p.pos, 2, 0, "'::'")
}
if p.tok != token.COLON && p.tok != token.ISA {
if p.tok.IsLiteral() {
Expand Down
6 changes: 6 additions & 0 deletions cue/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,12 @@ func TestStrict(t *testing.T) {
`a b c: 2`},
{"reserved identifiers",
`__foo: 3`},
{"old-style definition",
`foo :: 3`},
{"old-style alias 1",
`X=3`},
{"old-style alias 2",
`X={}`},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
Expand Down

0 comments on commit 3e10918

Please sign in to comment.