Mantle Query Filter Operators Migration Guide
@ngrok/mantle/query-filter now reads more of a query as structure. Under the "full" and "conjunction" syntax, NOT and a leading ! exclude, as - does, and ( and ) group. A key with a type takes operators such as >3, 2..5, and api*. FilterToken has three new kinds: "not", "group-start", and "group-end". FilterKeyDefinition is now a union of a key with matches and a typed key with getValue, so matches is optional on the union. parseFilterExpression returns the query as a tree.
TL;DR: Handle the three new token kinds wherever you switch on token.kind. Narrow a key with definition.matches != null before you call matches. Read a group or an OR from parseFilterExpression, not from the token list. A grammar that only declares keys and a list that only calls filterItemsByQuery change nothing.
Codemod
Codemod: manual: each step depends on what the call site does with a token or a key, and a query string that now parses differently is data, not syntax.
You are an agent migrating a codebase. Do this:
- Search for
parseFilterQuery(and for theFilterTokentype. Where code switches ontoken.kindor compares it with"or"or"and", decide what"not","group-start", and"group-end"mean there. A check that a query holds only qualifiers keeps working: it already rejects every kind but"facet". - Where code rebuilds the
ORgroups from the token list, replace that code withparseFilterExpression(grammar, query)and walk itsand,or,not,facet, andtextnodes. The tree has the grouping and the negation ofNOT,!, and parentheses already applied. - Search for
.matches(on a value typedFilterKeyDefinition. Narrow it first:if (definition.matches != null) { definition.matches(item, value, context) }. A key literal in a grammar needs no change. - Search for
.negatedon a token. Where code reads it as whether a filter excludes, as for a pressed state, readgetEffectiveNegations(tokens)[index]instead. ANOTor a negated group around a token negates it too, soNOT -is:sharedincludes. - Where code switches on
problem.kindfromcheckFilterQuery, handle the two new kinds:"invalid-value"(a value that does not parse for its key's type) and"unsupported-operator"(an operator the key does not accept). - Where code calls
formatFacetto write a query for a grammar under the"qualifiers"syntax, passgrammar. Without it,formatFacetquotes a value with a parenthesis or an edge=or*, and that syntax reads the quotes as text. - Where code stores or builds
FilterTokenvalues, such as a token cache or a fixture, parse the query again withparseFilterQuery. A qualifier member now carriesoperatorandcomparison, andfilterItemsByTokensreads a member with nocomparisonas a filter that matches no item. - Search stored queries, saved views, and
?q=links for a term that starts with!, the bare wordNOT, and a term that starts with(. Each one now parses as structure, and a later)closes the group. To keep one as a search term, quote it, and keep a leading-outside the quotes:"!important","NOT",-"(beta)". A)inside a qualifier value outside a group, as inname:fn(x), stays part of the value, so leave it as written. - Leave the stored queries of a grammar under the
"qualifiers"syntax alone. It readsNOT,!, and parentheses as text, as it reads-. - After editing, run the project's formatter, linter, type checker, and tests.
Why change the language? A list could not say "created in the last 7 days", "3 or more replicas", or "name starts with api-" in a way the field could check, suggest, and paint. Each app parsed its own operators out of the value. The operators, the typed keys, and the tree now live in the language, so every list and every backend reads one query the same way.
Before → after
A switch on the token kind
OR groups rebuilt from the tokens
A call to a key's matcher
A stored query with a literal !
Edge cases & gotchas
- A key with no
typetakes no operator.name:>3on a key withmatchesstill reaches the matcher as the value>3. - Inside a group, a
)ends the token. Quote a value that holds one:(name:"fn(x)" OR api). - A
-,!, orNOTon a key withnegatable: falsematches no item, also when it negates a whole group that holds the key.
How to verify
Run the type checker: a switch on token.kind with an exhaustive check names each missing kind, and a call to matches on the union names each missing narrow. Then, for each stored query from step 8, read parseFilterExpression(grammar, query) and check that it holds the filters the saved view meant. checkFilterQuery alone cannot show this: a term that became a group, and a qualifier that became a quoted search term, both report nothing.