Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions mdl/exprcheck/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,10 +595,21 @@ func parseIdentLed(s *Stream, ctx Context) (RobustExpr, []Hint) {
t := s.Consume()
name := t.Text
switch strings.ToLower(name) {
case "true":
return &BoolLit{baseNode: baseNode{P: t.Pos}, Value: true}, nil
case "false":
return &BoolLit{baseNode: baseNode{P: t.Pos}, Value: false}, nil
case "true", "false":
// An XPath writes a Boolean as true() or false(). Without consuming the
// empty argument list the parse ended at its '(' and everything after it
// was lost: [IsConfidential = false() or Mod.Report_Owner = '[%CurrentUser%]']
// came out as just the first comparison (mendixlabs/mxcli#1894).
if ctx.XPath && s.Peek().Kind == TokLParen {
mark := s.Mark()
s.Consume()
if s.Peek().Kind == TokRParen {
s.Consume()
} else {
s.Reset(mark)
}
}
return &BoolLit{baseNode: baseNode{P: t.Pos}, Value: strings.EqualFold(name, "true")}, nil
case "empty":
return &EmptyExpr{baseNode: baseNode{P: t.Pos}}, nil
case "null":
Expand Down
55 changes: 55 additions & 0 deletions mdl/exprcheck/xpath_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package exprcheck

import (
"reflect"
"strings"
"testing"
)

Expand Down Expand Up @@ -54,3 +55,57 @@ func TestExpressionModeKeepsRejectingEntityRootedPaths(t *testing.T) {
t.Errorf("no E014 for an entity-rooted path in an expression: %+v", hs)
}
}

// An XPath writes a Boolean as true() or false(). The parse used to end at the
// '(' and drop the rest of the constraint (mendixlabs/mxcli#1894).
func TestXPathModeParsesBooleanFunctions(t *testing.T) {
for _, src := range []string{
`IsConfidential = false() or Mod.Report_Owner = '[%CurrentUser%]'`,
`Mod.Report_Owner = '[%CurrentUser%]' or IsConfidential = false()`,
`IsConfidential = true() and (IsAssigned = false() or Mod.Report_Owner = '[%CurrentUser%]')`,
} {
ctx := NewSyntaxContext("", "")
ctx.XPath = true
expr, hs := NewParser().Parse(src, ctx)
for _, h := range hs {
if h.Code == "E014" {
t.Errorf("%s: trailing tokens left: %+v", src, h)
}
}
bin, ok := expr.(*BinExpr)
if !ok {
t.Errorf("%s: root = %T, want *BinExpr", src, expr)
continue
}
if op := strings.ToLower(bin.Op); op != "or" && op != "and" {
t.Errorf("%s: root op = %q, want the top-level or/and", src, bin.Op)
}
}
}

// true() is a Boolean literal, not a call: the empty argument list is part of it.
func TestXPathModeBooleanFunctionIsALiteral(t *testing.T) {
ctx := NewSyntaxContext("", "")
ctx.XPath = true
expr, _ := NewParser().Parse(`Active = true()`, ctx)
bin, ok := expr.(*BinExpr)
if !ok {
t.Fatalf("root = %T, want *BinExpr", expr)
}
if lit, ok := bin.R.(*BoolLit); !ok || !lit.Value {
t.Errorf("right = %#v, want BoolLit true", bin.R)
}
}

// Control: a microflow expression writes true and false without parentheses,
// so false() there stays an error.
func TestExpressionModeKeepsRejectingBooleanFunctions(t *testing.T) {
_, hs := NewParser().Parse(`$X = false() or $Y`, NewSyntaxContext("", ""))
found := false
for _, h := range hs {
found = found || h.Code == "E014"
}
if !found {
t.Errorf("no E014 for false() in an expression: %+v", hs)
}
}
10 changes: 9 additions & 1 deletion mdl/linter/starlark.go
Original file line number Diff line number Diff line change
Expand Up @@ -1641,7 +1641,15 @@ func (r *StarlarkRule) builtinParseXPath(_ *starlark.Thread, _ *starlark.Builtin
parser := exprcheck.NewParser()
ctx := exprcheck.NewSyntaxContext("", "")
ctx.XPath = true
ast, _ := parser.Parse(inner, ctx)
ast, hs := parser.Parse(inner, ctx)
// Tokens left after the parse (E014) mean the tree covers only part of the
// constraint. Returning it as is let a rule judge a fragment as if it were
// the whole XPath (mendixlabs/mxcli#1894); a recovered root says so instead.
for _, h := range hs {
if h.Code == "E014" {
return robustExprToStarlark(&exprcheck.RecoveredExpr{SourceFragment: inner, Reason: h.Problem}), nil
}
}
return robustExprToStarlark(ast), nil
}

Expand Down
43 changes: 43 additions & 0 deletions mdl/linter/starlark_xpath_paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,46 @@ def check():
t.Errorf("count_not = %+v, want 2", vs)
}
}

// "parse_xpath silently drops everything after an XPath true() or false()"
// (mendixlabs/mxcli#1894): both branches of the or come back, in either order.
func TestParseXPathBooleanFunctions(t *testing.T) {
for _, tc := range []struct{ in, want string }{
{`[IsConfidential = false() or Mod.Report_Owner = '[%CurrentUser%]']`,
`((IsConfidential = <bool>) OR (Mod.Report_Owner = '[%CurrentUser%]'))`},
{`[Mod.Report_Owner = '[%CurrentUser%]' or IsConfidential = false()]`,
`((Mod.Report_Owner = '[%CurrentUser%]') OR (IsConfidential = <bool>))`},
{`[Active = true() and (Mod.A/Mod.E/Name = 'x')]`,
`((Active = <bool>) AND ((Mod.A/Mod.E/Name = 'x')))`},
} {
vs, _ := runSrc(t, lintCtx(t), renderXPath+`
def check():
return [violation(message = render(parse_xpath(`+"\""+escapeStarlark(tc.in)+"\""+`)))]
`)
if len(vs) != 1 || vs[0].Message != tc.want {
var got []string
for _, v := range vs {
got = append(got, v.Message)
}
t.Errorf("parse_xpath(%s) renders %v, want %q", tc.in, got, tc.want)
}
}
}

// A constraint the parser cannot read to the end comes back as one recovered
// node holding the whole source, as the write-lint-rules skill says, not as a
// partial tree a rule would take for the whole XPath (mendixlabs/mxcli#1894).
func TestParseXPathTrailingTokensAreRecovered(t *testing.T) {
vs, _ := runSrc(t, lintCtx(t), `
def check():
n = parse_xpath("[Name = 'x' 'y']")
return [violation(message = n.kind + "|" + n.source)]
`)
if len(vs) != 1 || vs[0].Message != "recovered|Name = 'x' 'y'" {
var got []string
for _, v := range vs {
got = append(got, v.Message)
}
t.Errorf("parse_xpath with trailing tokens gives %v, want a recovered root with the whole source", got)
}
}
Loading