Add preserveRelativeDates parse option#141
Conversation
Consumers that need to render or round-trip relative-date filters (e.g.
the Members admin UI showing "in the last 7 days") previously lost the
relative form because the parser eagerly resolved `now-7d` to an absolute
SQL-formatted date. Opting in via `parse(input, {preserveRelativeDates: true})`
now emits `{$relativeDate: {op, amount, unit}}` so callers can preserve
the original intent. Default behaviour is unchanged.
|
ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
WalkthroughThis pull request adds a Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #141 +/- ##
==========================================
+ Coverage 81.42% 81.69% +0.26%
==========================================
Files 9 9
Lines 1701 1726 +25
Branches 339 342 +3
==========================================
+ Hits 1385 1410 +25
Misses 311 311
Partials 5 5 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
kevinansfield
left a comment
There was a problem hiding this comment.
As mentioned in the mob call, I think there's an argument for not needing the preserveRelativeDates option at all and having the parse output always fully encode the NQL string's intent in the AST, keeping any absolute date conversions on the AST->Output side.
However, change looks good and won't break anything so we can always look at simplifying the options later and do a major bump when publishing in case there are any consumers depending on the absolute dates after parsing 👍
|
Thanks @kevinansfield, I checked for other users of the library and there's a good few leveraging the same entrypoint so it would be a breaking change for them to not resolve the relative dates by default. It's true that filters is the only thing that uses the AST results, but its not the only thing that parses filters so we can't break compatibility, so adding the option is the best non-breaking change we can add today. |
Summary
Adds an opt-in
preserveRelativeDatesoption tonql-lang.parse(). When enabled, relative-date expressions likenow-7dare emitted as a tagged value rather than being resolved to an absolute SQL-formatted date at parse time:Default behaviour is unchanged for callers that don't pass the option.
Why
UI clients that round-trip NQL strings through the parser currently lose the relative form (e.g. "in the last 7 days" becomes a fixed date the moment
parse()runs). Consumers wanting to render the relative form have to detect it via string-level regex before calling the parser — duplicating the grammar's tokenisation work, and inevitably falling short for nested or compound expressions.The grammar already recognises relative-date syntax as a distinct production rule (
NOW DATEOP AMOUNT INTERVALinnql.y); this PR just lets that recognition reach the AST when the caller asks for it. Server-side / SQL callers continue to get absolute dates.