Skip to main content

Conditions

<when> is a child of a rule or a context that holds conditions on the change. It is a structured block (see Structured blocks), and its value is a mapping in which each key is a condition. An entry with a <when> is eligible only when every one of its conditions holds.

KeyValueHolds when
addedContainsA list of strings.An added line of a counted file contains one of the strings.
addedMatchesA list of patterns (see Regex dialect).An added line of a counted file matches one of the patterns.
removedContainsA list of strings.A removed line of a counted file contains one of the strings.
removedMatchesA list of patterns (see Regex dialect).A removed line of a counted file matches one of the patterns.
droppedContainsA list of strings.A removed line of a counted file contains one of the strings, and no added line of the same edit contains that string.
droppedMatchesA list of patterns (see Regex dialect).A removed line of a counted file matches one of the patterns, and no added line of the same edit matches that pattern.
changedA list of globs.A changed file anywhere in the change matches one of the globs.
unchangedA list of globs.No changed file anywhere in the change matches any of the globs.
  • An entry's counted files are the changed files in its scope, apart from those that a .loreconfig disable list switches the entry off for (see .loreconfig).
  • An added line's text, and a removed line's text, is the line as it is in the file's new or old version, without its line break: the line feed, and a carriage return directly before it. A line that is not valid UTF-8 is decoded for matching by replacing each maximal subpart of an ill-formed sequence with one U+FFFD, the practice the Unicode Standard recommends (U+FFFD substitution of maximal subparts) and the WHATWG Encoding Standard's UTF-8 decoder follows. A maximal subpart is the longest run of bytes, starting at the first byte that cannot be decoded, that is the start of some well-formed UTF-8 sequence, or that one byte when it starts none. In a conformance example it is the <text> of a +<n> <text> or -<n> <text> line. The added-line conditions see added lines only and the removed-line conditions removed lines only; the unchanged lines around them never count for either. The dropped-line conditions see removed lines, and added lines only to decide whether the same edit added the item back.
  • Within one changed file, the unchanged lines of the old version and of the new version are the same lines in the same order. A removed line lies after some number of them in the old version, and an added line after some number of them in the new version. An edit is the set of that file's removed and added lines that lie after the same number of unchanged lines, so two changed lines are in the same edit when no unchanged line lies between them. The number follows from the line numbers alone: for a removed line at old-side line r it is r - 1 minus the count of the file's removed lines numbered below r, and for an added line at new-side line a it is a - 1 minus the count of the file's added lines numbered below a. A renamed file's removed lines, named by its old path, and its added lines, named by its new path, form edits together. A deleted file's edits have no added line. An edit never spans two files.
  • addedContains compares code point by code point and is case-sensitive: a string is contained in a line when it occurs anywhere in the line's text. removedContains does the same over removed lines.
  • addedMatches searches each line's text for each pattern, as Regex dialect defines. removedMatches does the same over removed lines.
  • droppedContains compares as removedContains does and droppedMatches searches as removedMatches does. Each item is paired on its own: a removed line that matches two items is found when either of them is on no added line of its edit. Pairing is by edit, not line by line: one added line that matches an item keeps every removed line of that edit from being found through that item.
  • changed and unchanged look at every path of every changed file in the change, in the entry's scope or not and whatever disable says. They are the only conditions that look past the entry's scope, because files that change together are a property of the whole change. A rule like regen-types-after-graphql below is eligible only when the generated files were left out of the change.
  • A changed or unchanged glob MAY start with /. It then resolves from the governed repo's root (for an external folder, from the directory its repo key marks), and the rest of the glob, after the /, is read as Glob syntax defines. A glob without a leading / resolves against the base, as in a scope.
  • With an empty list, unchanged always holds and the other seven conditions never do.
  • When a rule is eligible, each added line of a counted file that one of its addedContains or addedMatches conditions finds is a hit, as a check's would be (see Running checks), and each removed line one of its removedContains or removedMatches conditions finds is a hit on the old side, named by the file's old path. A removed line that a droppedContains or droppedMatches condition finds is a hit on the old side in the same way. All of the conditions together decide eligibility; each added-line, removed-line or dropped-line condition then points at the lines it finds on its own. A context has no hits.

The dropped-line conditions have known limits. An edit that adds the same item for another reason keeps a removal from being found. When an edit keeps the item but changes what it applies to, its removed line is not found, so a rule about such a change uses removedContains or removedMatches. A block moved within a file or to another file is found at its old place.

A tool checks a <when> in this order, and reports the first problem it finds:

  1. the checks of Structured blocks;
  2. invalid-when, when the value is not a mapping;
  3. empty-when, when the mapping has no keys, as with an empty block;
  4. unknown-condition, when a key is not one of the eight above;
  5. each condition, in the order of the table above: invalid-when when its value is not a list of strings, or when an addedContains, removedContains or droppedContains string is empty or holds a line feed or a carriage return; then each item of the list in order. A pattern is checked for regex-not-one-line and then regex-outside-dialect (see Regex dialect), and a glob for invalid-glob and then glob-escapes-base (see Scope and globs).

The shape is also given as when.schema.json; where a name above covers a value the schema rejects, that name applies instead of invalid-when. Every error in a <when> is entry-level. Apart from a YAML problem, its line is the <when> opening tag line. A tool MUST NOT ignore a key it does not know, so a file never means less than its author wrote. In the document model, when is the mapping as parsed, with its globs as written, or null when the entry has no <when>.

Unchanged lines never count. 'USD' is on an unchanged line of Price.tsx, so the first rule is not eligible. The third rule's scope holds format.ts but not Price.tsx, so the parseFloat( on the added line of Price.tsx does not count for it. The line the second rule's condition finds is a hit:

Example 64 (eligibility)​

.lore/checkout.lore
<rule id="currency-from-settings" category="functional_correctness">
<when>
addedContains: ["'USD'", "'EUR'"]
</when>
Display text never hard-codes a currency code.
</rule>

<rule id="prices-not-parsed" category="functional_correctness">
<when>
addedContains: ["parseFloat("]
</when>
A price is never parsed from text with `parseFloat`.
</rule>

<rule id="helpers-not-parsing-prices" category="functional_correctness">
<scope>
files: ["**/*.ts"]
</scope>
<when>
addedContains: ["parseFloat("]
</when>
Checkout helpers never parse a price from text.
</rule>
apps/web/src/checkout/Price.tsx
const fallbackCurrency = 'USD'

export function Price({ quote }) {
const amount = parseFloat(quote.total)
return <span>{amount}</span>
}
Change
M apps/web/src/checkout/Price.tsx
+4 const amount = parseFloat(quote.total)
M apps/web/src/checkout/format.ts
+1 export const ZERO = '0.00'
Expected output
{
"eligible": ["prices-not-parsed"],
"hits": [
{ "rule": "prices-not-parsed", "file": "apps/web/src/checkout/Price.tsx", "line": 4, "text": " const amount = parseFloat(quote.total)" }
],
"excluded": [],
"warnings": []
}

A pattern in addedMatches is searched for in each added line, and one in removedMatches or droppedMatches in each removed line; a droppedMatches pattern is also searched for in the added lines of the removed line's edit. ^ matches only at the start of the line's text, which here starts with spaces:

Example 65 (eligibility)​

apps/web/src/checkout/.lore/prices.lore
<rule id="no-price-literals" category="functional_correctness">
<when>
addedMatches: ['\d+\.\d{2}']
</when>
A price is never written as a number literal in checkout code.
</rule>

<rule id="shipping-from-quote" category="functional_correctness">
<when>
addedMatches: ['^const shipping']
</when>
Shipping costs come from the quote.
</rule>
Change
M apps/web/src/checkout/Shipping.tsx
+7 const shipping = 4.99
Expected output
{
"eligible": ["no-price-literals"],
"hits": [
{ "rule": "no-price-literals", "file": "apps/web/src/checkout/Shipping.tsx", "line": 7, "text": " const shipping = 4.99" }
],
"excluded": [],
"warnings": []
}

A removed line never counts for an added-line condition. The first rule's condition finds the removed fallback, which is a hit on the old side. The second rule's addedContains sees the added line only, and ?? is on no added line:

Example 66 (eligibility)​

apps/web/src/checkout/.lore/reads.lore
<rule id="fallback-kept" category="functional_correctness">
<when>
removedContains: ["??"]
</when>
A read of an optional field keeps its `??` fallback.
</rule>

<rule id="fallback-added" category="functional_correctness">
<when>
addedContains: ["??"]
</when>
A new read of an optional field has a `??` fallback.
</rule>
apps/web/src/checkout/Price.tsx
export function Price({ quote }) {
const label = quote.label
return <span>{label}</span>
}
Change
M apps/web/src/checkout/Price.tsx
-2 const label = quote.label ?? ''
+2 const label = quote.label
Expected output
{
"eligible": ["fallback-kept"],
"hits": [
{ "rule": "fallback-kept", "file": "apps/web/src/checkout/Price.tsx", "line": 2, "text": " const label = quote.label ?? ''", "side": "old" }
],
"excluded": [],
"warnings": []
}

A droppedContains condition finds a removed line only when its edit does not add the item back. In Price.tsx the edit keeps ?? on its added line, so only the removedContains rule finds that line. In Total.tsx the fallback is gone, and both rules find it:

Example 67 (eligibility)​

apps/web/src/checkout/.lore/reads.lore
<rule id="fallback-dropped" category="functional_correctness">
<when>
droppedContains: ["??"]
</when>
A read of an optional field keeps its `??` fallback.
</rule>

<rule id="fallback-touched" category="functional_correctness">
<when>
removedContains: ["??"]
</when>
A change to a line with a `??` fallback keeps the default it had.
</rule>
Change
M apps/web/src/checkout/Price.tsx
-2 const label = quote.label ?? ''
+2 const label = quote.title ?? ''
M apps/web/src/checkout/Total.tsx
-5 const tax = quote.tax ?? 0
+5 const tax = quote.tax
Expected output
{
"eligible": ["fallback-dropped", "fallback-touched"],
"hits": [
{ "rule": "fallback-dropped", "file": "apps/web/src/checkout/Total.tsx", "line": 5, "text": " const tax = quote.tax ?? 0", "side": "old" },
{ "rule": "fallback-touched", "file": "apps/web/src/checkout/Price.tsx", "line": 2, "text": " const label = quote.label ?? ''", "side": "old" },
{ "rule": "fallback-touched", "file": "apps/web/src/checkout/Total.tsx", "line": 5, "text": " const tax = quote.tax ?? 0", "side": "old" }
],
"excluded": [],
"warnings": []
}

The line numbers decide the edits. In Cart.tsx the removed and the added line lie after the same two unchanged lines, so they are one edit and the guard is added back. In Summary.tsx an unchanged line lies between them, so they are two edits and the removed guard is found:

Example 68 (eligibility)​

apps/web/src/checkout/.lore/guards.lore
<rule id="guard-dropped" category="functional_correctness">
<when>
droppedContains: ["if (!quote)"]
</when>
A component keeps its guard for a missing quote.
</rule>
Change
M apps/web/src/checkout/Cart.tsx
-3 if (!quote) return null
+3 if (!quote) return <Empty />
M apps/web/src/checkout/Summary.tsx
-3 if (!quote) return null
+4 if (!quote) return <Empty />
Expected output
{
"eligible": ["guard-dropped"],
"hits": [
{ "rule": "guard-dropped", "file": "apps/web/src/checkout/Summary.tsx", "line": 3, "text": " if (!quote) return null", "side": "old" }
],
"excluded": [],
"warnings": []
}

A droppedMatches condition pairs each pattern on its own. Line 4 of Discount.tsx keeps its if ( but loses its ??, so the ?? pattern finds it. Line 8 of Coupon.tsx lost its fallback and is still not found, because line 7 of the same edit adds ??; pairing is by edit, not line by line:

Example 69 (eligibility)​

apps/web/src/checkout/.lore/branches.lore
<rule id="branch-or-fallback-dropped" category="functional_correctness">
<when>
droppedMatches: ['\?\?', '^\s*if \(']
</when>
A cleanup keeps the branches and fallbacks that still do something.
</rule>
Change
M apps/web/src/checkout/Coupon.tsx
-7 const rate = coupon.rate ?? 0
-8 const cap = coupon.cap ?? Infinity
+7 const rate = coupon.rate ?? 0.05
+8 const cap = coupon.cap
M apps/web/src/checkout/Discount.tsx
-4 if (code ?? defaultCode) {
+4 if (code) {
Expected output
{
"eligible": ["branch-or-fallback-dropped"],
"hits": [
{ "rule": "branch-or-fallback-dropped", "file": "apps/web/src/checkout/Discount.tsx", "line": 4, "text": " if (code ?? defaultCode) {", "side": "old" }
],
"excluded": [],
"warnings": []
}

A deleted file's edits have no added line, so each matching removed line of it is found. A renamed file's removed lines pair with its added lines, so the removed line of the edit in Total.tsx that keeps 'USD' under the new path is not found:

Example 70 (eligibility)​

apps/web/src/.lore/currency.lore
<rule id="currency-literal-dropped" category="functional_correctness">
<when>
droppedContains: ["'USD'"]
</when>
Removing a hard-coded currency also removes what depended on it.
</rule>
Change
D apps/web/src/legacy/Price.tsx
-3 const currency = 'USD'
R apps/web/src/legacy/Total.tsx -> apps/web/src/checkout/Total.tsx
-6 const currency = 'USD'
+6 const currency = settings.currency ?? 'USD'
Expected output
{
"eligible": ["currency-literal-dropped"],
"hits": [
{ "rule": "currency-literal-dropped", "file": "apps/web/src/legacy/Price.tsx", "line": 3, "text": "const currency = 'USD'", "side": "old" }
],
"excluded": [],
"warnings": []
}

Each added line that the rule's condition finds is a hit. Its check also finds line 5, which is still one hit. The context is eligible, and has no hits:

Example 71 (eligibility, requires regex)​

.lore/money.lore
<rule id="money-in-minor-units" category="functional_correctness">
<when>
addedContains: ["parseFloat(", "toFixed("]
</when>
<check>
toFixed\(
</check>
Amounts are integers in minor units (cents).
</rule>

<context id="rounding-guide" src="docs/rounding.md">
<when>
addedContains: ["toFixed("]
</when>
Read before changing how amounts are rounded.
</context>
docs/rounding.md
# Rounding
Change
M apps/web/src/checkout/Price.tsx
+4 const amount = parseFloat(quote.total)
+5 return amount.toFixed(2)
+6 return amount
Expected output
{
"eligible": ["money-in-minor-units", "rounding-guide"],
"hits": [
{ "rule": "money-in-minor-units", "file": "apps/web/src/checkout/Price.tsx", "line": 4, "text": " const amount = parseFloat(quote.total)" },
{ "rule": "money-in-minor-units", "file": "apps/web/src/checkout/Price.tsx", "line": 5, "text": " return amount.toFixed(2)" }
],
"excluded": [],
"warnings": []
}

changed looks at the whole change. The API schema is outside the rule's scope and its base, and the condition still holds:

Example 72 (eligibility)​

apps/web/src/checkout/.lore/api.lore
<rule id="checkout-follows-api-schema" category="backwards_compatibility">
<scope>
files: ["**/*.tsx"]
</scope>
<when>
changed: ["/apps/api/schema.graphql"]
</when>
A checkout change made alongside an API schema change still works with
the schema that is deployed now.
</rule>
Change
M apps/web/src/checkout/Price.tsx
M apps/api/schema.graphql
Expected output
{ "eligible": ["checkout-follows-api-schema"], "hits": [], "excluded": [], "warnings": [] }

The generated types changed, so regen-types-after-graphql is not eligible: there is nothing to remind the author of. Its glob starts with /, so it resolves from the repository root. The second rule's glob has no /, so it resolves against the base, to apps/web/src/checkout/__generated__/**. Nothing there changed, and that rule is eligible:

Example 73 (eligibility)​

apps/web/src/checkout/.lore/graphql.lore
<scope>
files: ["**/*.graphql"]
</scope>

<rule id="regen-types-after-graphql" category="functional_correctness">
<when>
unchanged: ["/apps/web/src/__generated__/**"]
</when>
A `.graphql` change ships with regenerated types in
`apps/web/src/__generated__/`.
</rule>

<rule id="regen-checkout-types-after-graphql" category="functional_correctness">
<when>
unchanged: ["__generated__/**"]
</when>
A `.graphql` change ships with regenerated types in the checkout's own
`__generated__/` folder.
</rule>
Change
M apps/web/src/checkout/quote.graphql
+3 total: Money!
M apps/web/src/__generated__/graphql.ts
+120 total: Money;
Expected output
{ "eligible": ["regen-checkout-types-after-graphql"], "hits": [], "excluded": [], "warnings": [] }

With an empty list, addedContains never holds and unchanged always does. A rename changes two paths, and changed and unchanged see both: the old path Price.tsx makes price-imports-updated eligible and price-shown-with-component not:

Example 74 (eligibility)​

.lore/checkout.lore
<rule id="currency-from-settings" category="functional_correctness">
<when>
addedContains: []
</when>
Display text never hard-codes a currency code.
</rule>

<rule id="price-from-quote" category="functional_correctness">
<when>
unchanged: []
</when>
A displayed price must come from the quote that produced it.
</rule>

<rule id="price-imports-updated" category="functional_correctness">
<when>
changed: ["/apps/web/src/checkout/Price.tsx"]
</when>
When `Price.tsx` moves or changes, every import of it is updated.
</rule>

<rule id="price-shown-with-component" category="code_style">
<when>
unchanged: ["/apps/web/src/checkout/Price.tsx"]
</when>
A price is shown with the `Price` component, never formatted by hand.
</rule>
Change
R apps/web/src/checkout/Price.tsx -> apps/web/src/checkout/QuotedPrice.tsx
+3 export function QuotedPrice({ quote }) {
Expected output
{ "eligible": ["price-from-quote", "price-imports-updated"], "hits": [], "excluded": [], "warnings": [] }

An unknown key is an error, even next to another problem: it is checked before the shape of each condition. A <when> with no conditions is an error, and so is a condition whose value is not a list. A .. segment is not allowed in changed, while a leading / is:

Example 75 (parse)​

.lore/checkout.lore
<rule id="currency-from-settings" category="functional_correctness">
<when>
addedContains: "'USD'"
contains: ["'USD'"]
</when>
Display text never hard-codes a currency code.
</rule>

<rule id="price-from-quote" category="functional_correctness">
<when>
</when>
A displayed price must come from the quote that produced it.
</rule>

<rule id="regen-types-after-graphql" category="functional_correctness">
<when>
unchanged: "/apps/web/src/__generated__/**"
</when>
A `.graphql` change ships with regenerated types.
</rule>

<rule id="checkout-follows-api-schema" category="backwards_compatibility">
<when>
changed: ["apps/web/../api/schema.graphql"]
</when>
A checkout change made alongside an API schema change still works with
the schema that is deployed now.
</rule>

<rule id="checkout-follows-api" category="backwards_compatibility">
<when>
changed: ["/apps/api/**"]
addedContains: ["fetch("]
</when>
New checkout requests work with the API that is deployed now.
</rule>
Expected output
{
"entries": [
{
"kind": "rule", "id": "checkout-follows-api", "file": ".lore/checkout.lore",
"category": "backwards_compatibility", "severity": "advisory", "disabled": false,
"scopes": [{ "files": ["**"], "ignores": [] }],
"when": { "changed": ["/apps/api/**"], "addedContains": ["fetch("] }, "checks": [], "meta": null,
"summary": "New checkout requests work with the API that is deployed now.",
"prose": "New checkout requests work with the API that is deployed now.",
"src": null,
"sources": []
}
],
"errors": [
{ "name": "unknown-condition", "file": ".lore/checkout.lore", "entry": "currency-from-settings", "line": 2 },
{ "name": "empty-when", "file": ".lore/checkout.lore", "entry": "price-from-quote", "line": 10 },
{ "name": "invalid-when", "file": ".lore/checkout.lore", "entry": "regen-types-after-graphql", "line": 16 },
{ "name": "glob-escapes-base", "file": ".lore/checkout.lore", "entry": "checkout-follows-api-schema", "line": 23 }
],
"warnings": []
}