Skip to content

Rule lookup as a trie - #232

Draft
webdevred wants to merge 10 commits into
masterfrom
rule-lookup-trie
Draft

Rule lookup as a trie#232
webdevred wants to merge 10 commits into
masterfrom
rule-lookup-trie

Conversation

@webdevred

@webdevred webdevred commented Aug 16, 2026

Copy link
Copy Markdown
Owner

Groundwork for #187. This branch will hold the rule lookup rewrite, and starts with the tests that have to hold through it.

Two things the rewrite must preserve had nothing checking them.

How a pattern matches a cursor. sameBy walks both sequences and then decides on its last line: the pattern must be consumed whole, and leftover breadcrumbs are allowed under PrefixMatch alone. A trie descends one selector at a time and has to answer the same way, in particular by not answering early. Four specs cover equal length, a shorter pattern as a prefix, a pattern longer than the cursor, and keeping AnyObjectKey and AnyArrayIndex apart.

Which mode each property is read in. doFormatNode hardcodes it: AutoPad, AlignObjectKeys and AutoPadSubObjects come from an exact match, ComplexNewLine and TrailingComma from a prefix match. Reading AutoPad from the prefix set instead leaves the whole fixture suite green, so nothing was guarding it. Two specs now do. They pin the split as it stands before >, which is meant to replace it, and the comment says so.

Every spec was checked by mutating the source rather than the test, one mutation at a time, and each one reddens only the spec that claims it.

.test* gets its own spec in the same group. It is documented in JBFL_DOCS.md and ships with the package, so the trie has to solve it rather than choose whether to keep it, and it is the one selector that cannot be keyed on directly: the stored key is a prefix of the breadcrumb rather than the same text. Either a character trie over the key text, or a scan of just that level's prefix patterns, which is a different thing from today's scan over every pattern at every node.

The rule lookup is about to become a trie, and nothing pinned the two
things that change would have to preserve: that a pattern matches only
once consumed whole, with leftover breadcrumbs allowed under PrefixMatch
alone, and which of the two modes each property is read in. Moving AutoPad
across left every fixture green.
`.test*` is documented JBFL and shipped, so the trie has to solve it rather
than decide whether to keep it. It is also the one selector that cannot be
keyed on directly, since the stored key is a prefix of the breadcrumb rather
than the same text.
Precedence was whatever fell out of Ord NodePattern, and part of it was
plainly wrong: against the key deformGroups the pattern .de* beat .deform*,
so the less specific rule won. The rule is now specificity, meaning how many
nodes a selector can match, and these specs state it. Two of them fail until
rank stops delegating to the derived Ord on NodeSelector.

Also pinned: a less specific pattern still supplies properties the winner
does not set. Every shipped ruleset depends on it, since .* carries Indent
and TrailingComma for the whole file.
`[4]` is the last row of the selector table that no shipped ruleset uses, so
nothing else would notice it going away. The merge is how every configured
install resolves its rules: a user's file is laid over the shipped one with a
union that is left-biased per pattern and per property.
Precedence and combining two rulesets are properties of RuleSet, not of the
formatter; the formatter was only the instrument they were observed through.
Adds the two that were missing: a prefix key reaches below the node it names,
and length settles before specificity. matchModeSpec now builds its rules from
JBFL source too, so nothing here constructs a RuleSet by hand.
@webdevred
webdevred force-pushed the rule-lookup-trie branch 2 times, most recently from 1e04c2f to 2a9c631 Compare August 16, 2026 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant