From 8e6d7b3f544d7cea888349daf650709d763af7a8 Mon Sep 17 00:00:00 2001 From: Joshua de Haan Date: Wed, 18 Nov 2026 12:30:10 +0100 Subject: [PATCH] fix: parse_xpath keeps what follows true() / false() (closes #1894) In XPath mode parseIdentLed returned a BoolLit for true/false before the '(' check, so the empty argument list of true() / false() was left on the stream. The parse ended there and Parse reported E014; builtinParseXPath discarded the hints, so a rule got the first comparison of [IsConfidential = false() or Mod.Report_Owner = '[%CurrentUser%]'] as if it were the whole constraint. - XPath mode consumes the empty () after true / false. In a microflow expression false() stays an error (E014). - parse_xpath returns a recovered root, with the whole source, when the parse leaves tokens over (E014), as the write-lint-rules skill says parse failures do. Co-Authored-By: Claude Opus 5.5 --- mdl/exprcheck/parser.go | 19 +++++++-- mdl/exprcheck/xpath_mode_test.go | 55 +++++++++++++++++++++++++ mdl/linter/starlark.go | 10 ++++- mdl/linter/starlark_xpath_paths_test.go | 43 +++++++++++++++++++ 4 files changed, 122 insertions(+), 5 deletions(-) diff --git a/mdl/exprcheck/parser.go b/mdl/exprcheck/parser.go index 14d83f5049..cda02f85e1 100644 --- a/mdl/exprcheck/parser.go +++ b/mdl/exprcheck/parser.go @@ -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": diff --git a/mdl/exprcheck/xpath_mode_test.go b/mdl/exprcheck/xpath_mode_test.go index 80fffb7907..5380a25149 100644 --- a/mdl/exprcheck/xpath_mode_test.go +++ b/mdl/exprcheck/xpath_mode_test.go @@ -4,6 +4,7 @@ package exprcheck import ( "reflect" + "strings" "testing" ) @@ -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) + } +} diff --git a/mdl/linter/starlark.go b/mdl/linter/starlark.go index 8abfe2c018..7e918d5cf0 100644 --- a/mdl/linter/starlark.go +++ b/mdl/linter/starlark.go @@ -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 } diff --git a/mdl/linter/starlark_xpath_paths_test.go b/mdl/linter/starlark_xpath_paths_test.go index 739e5d3488..51894cf09a 100644 --- a/mdl/linter/starlark_xpath_paths_test.go +++ b/mdl/linter/starlark_xpath_paths_test.go @@ -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 = ) OR (Mod.Report_Owner = '[%CurrentUser%]'))`}, + {`[Mod.Report_Owner = '[%CurrentUser%]' or IsConfidential = false()]`, + `((Mod.Report_Owner = '[%CurrentUser%]') OR (IsConfidential = ))`}, + {`[Active = true() and (Mod.A/Mod.E/Name = 'x')]`, + `((Active = ) 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) + } +}