Skip to main content

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:

  1. Search for parseFilterQuery( and for the FilterToken type. Where code switches on token.kind or 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".
  2. Where code rebuilds the OR groups from the token list, replace that code with parseFilterExpression(grammar, query) and walk its and, or, not, facet, and text nodes. The tree has the grouping and the negation of NOT, !, and parentheses already applied.
  3. Search for .matches( on a value typed FilterKeyDefinition. Narrow it first: if (definition.matches != null) { definition.matches(item, value, context) }. A key literal in a grammar needs no change.
  4. Search for .negated on a token. Where code reads it as whether a filter excludes, as for a pressed state, read getEffectiveNegations(tokens)[index] instead. A NOT or a negated group around a token negates it too, so NOT -is:shared includes.
  5. Where code switches on problem.kind from checkFilterQuery, 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).
  6. Where code calls formatFacet to write a query for a grammar under the "qualifiers" syntax, pass grammar. Without it, formatFacet quotes a value with a parenthesis or an edge = or *, and that syntax reads the quotes as text.
  7. Where code stores or builds FilterToken values, such as a token cache or a fixture, parse the query again with parseFilterQuery. A qualifier member now carries operator and comparison, and filterItemsByTokens reads a member with no comparison as a filter that matches no item.
  8. Search stored queries, saved views, and ?q= links for a term that starts with !, the bare word NOT, 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 in name:fn(x), stays part of the value, so leave it as written.
  9. Leave the stored queries of a grammar under the "qualifiers" syntax alone. It reads NOT, !, and parentheses as text, as it reads -.
  10. 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

// BEFOREfor (const token of parseFilterQuery(grammar, query)) {	switch (token.kind) {		case "facet":		case "text":			terms.push(token);			break;		case "or":		case "and":			break;	}} // AFTERfor (const token of parseFilterQuery(grammar, query)) {	switch (token.kind) {		case "facet":		case "text":			terms.push(token);			break;		case "or":		case "and":		case "not":		case "group-start":		case "group-end":			break;	}}

OR groups rebuilt from the tokens

// BEFOREconst groups: FilterToken[][] = [[]];for (const token of parseFilterQuery(grammar, query)) {	if (token.kind === "or") {		groups.push([]);	} else if (token.kind !== "and") {		groups.at(-1)?.push(token);	}}const filter = groups.map(compileGroup).join(" || "); // AFTERconst expression = parseFilterExpression(grammar, query);const filter = expression == null ? null : compileExpression(expression);

A call to a key's matcher

// BEFOREconst definition = getFilterKeyDefinition(grammar, key);const matched = definition?.matches(item, value, context) ?? false; // AFTERconst definition = getFilterKeyDefinition(grammar, key);const matched = definition?.matches != null ? definition.matches(item, value, context) : false;

A stored query with a literal !

// BEFORE: searched for the text "!important"const savedQuery = "!important is:open"; // AFTER: quote the term to keep searching for itconst savedQuery = '"!important" is:open';

Edge cases & gotchas

  • A key with no type takes no operator. name:>3 on a key with matches still 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 -, !, or NOT on a key with negatable: false matches 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.