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.
| Key | Value | Holds when |
|---|---|---|
addedContains | A list of strings. | An added line of a counted file contains one of the strings. |
addedMatches | A list of patterns (see Regex dialect). | An added line of a counted file matches one of the patterns. |
removedContains | A list of strings. | A removed line of a counted file contains one of the strings. |
removedMatches | A list of patterns (see Regex dialect). | A removed line of a counted file matches one of the patterns. |
droppedContains | A 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. |
droppedMatches | A 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. |
changed | A list of globs. | A changed file anywhere in the change matches one of the globs. |
unchanged | A 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
.loreconfigdisablelist 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
rit isr - 1minus the count of the file's removed lines numbered belowr, and for an added line at new-side lineait isa - 1minus the count of the file's added lines numbered belowa. 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. addedContainscompares 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.removedContainsdoes the same over removed lines.addedMatchessearches each line's text for each pattern, as Regex dialect defines.removedMatchesdoes the same over removed lines.droppedContainscompares asremovedContainsdoes anddroppedMatchessearches asremovedMatchesdoes. 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.changedandunchangedlook at every path of every changed file in the change, in the entry's scope or not and whateverdisablesays. 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 likeregen-types-after-graphqlbelow is eligible only when the generated files were left out of the change.- A
changedorunchangedglob MAY start with/. It then resolves from the governed repo's root (for an external folder, from the directory itsrepokey 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,
unchangedalways holds and the other seven conditions never do. - When a rule is eligible, each added line of a counted file that one of
its
addedContainsoraddedMatchesconditions finds is a hit, as a check's would be (see Running checks), and each removed line one of itsremovedContainsorremovedMatchesconditions finds is a hit on the old side, named by the file's old path. A removed line that adroppedContainsordroppedMatchescondition 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:
- the checks of Structured blocks;
invalid-when, when the value is not a mapping;empty-when, when the mapping has no keys, as with an empty block;unknown-condition, when a key is not one of the eight above;- each condition, in the order of the table above:
invalid-whenwhen its value is not a list of strings, or when anaddedContains,removedContainsordroppedContainsstring is empty or holds a line feed or a carriage return; then each item of the list in order. A pattern is checked forregex-not-one-lineand thenregex-outside-dialect(see Regex dialect), and a glob forinvalid-globand thenglob-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)
<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>
const fallbackCurrency = 'USD'
export function Price({ quote }) {
const amount = parseFloat(quote.total)
return <span>{amount}</span>
}
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'
{
"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)
<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>
M apps/web/src/checkout/Shipping.tsx
+7 const shipping = 4.99
{
"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)
<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>
export function Price({ quote }) {
const label = quote.label
return <span>{label}</span>
}
M apps/web/src/checkout/Price.tsx
-2 const label = quote.label ?? ''
+2 const label = quote.label
{
"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)
<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>
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
{
"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)
<rule id="guard-dropped" category="functional_correctness">
<when>
droppedContains: ["if (!quote)"]
</when>
A component keeps its guard for a missing quote.
</rule>
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 />
{
"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)
<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>
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) {
{
"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)
<rule id="currency-literal-dropped" category="functional_correctness">
<when>
droppedContains: ["'USD'"]
</when>
Removing a hard-coded currency also removes what depended on it.
</rule>
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'
{
"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)
<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>
# Rounding
M apps/web/src/checkout/Price.tsx
+4 const amount = parseFloat(quote.total)
+5 return amount.toFixed(2)
+6 return amount
{
"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)
<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>
M apps/web/src/checkout/Price.tsx
M apps/api/schema.graphql
{ "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)
<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>
M apps/web/src/checkout/quote.graphql
+3 total: Money!
M apps/web/src/__generated__/graphql.ts
+120 total: Money;
{ "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)
<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>
R apps/web/src/checkout/Price.tsx -> apps/web/src/checkout/QuotedPrice.tsx
+3 export function QuotedPrice({ quote }) {
{ "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)
<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>
{
"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": []
}