Skip to main content

Checks

A <check> is a child of a rule that holds a search. When the rule is eligible, a tool runs the search over the rule's counted files (see Conditions), and its hits go to the reviewer with the rule. A hit is a pointer to a line, never a verdict: a hit does not mean the rule is broken, and no hits does not mean it passed.

A rule's added-line, removed-line and dropped-line conditions already point at the lines they find (see Conditions). A check is for a search that should not decide whether the rule is eligible, or one that a condition cannot express, such as an ast-grep pattern.

A rule MAY have any number of checks. A context has none (see Context).

  • <check> takes one attribute, lang, the check's language. Without it, the language is regex. lang with no value, or with the empty value lang="", is invalid-attribute-value. Any other attribute is unknown-attribute.
  • This specification defines two languages, regex and ast-grep. Any other lang value is kept as written: it is a language no conforming tool supports (see below), not an error.
  • A check's body is the lines strictly between its tags, after the entry's common indentation and then the check's own are removed (see Indentation), joined with line feeds. Nothing else is trimmed: a blank line is kept as an empty line, and spaces at the end of a line are part of the body.
  • A regex body is one pattern in the Regex dialect. A body of two or more lines is regex-not-one-line. Otherwise, a body outside the dialect, the empty body included, is regex-outside-dialect.
  • A tool checks a <check> as Children describes, and its contents last: for a regex check, regex-not-one-line and then regex-outside-dialect. A body in any other language is not checked when parsing. These two checks are part of parsing, so they bind every tool that parses Lore files, whether or not it runs regex checks.
  • Every error in a <check> is entry-level, at its opening tag line.
  • In the document model, checks lists each check's lang and body, in the order the checks appear, with lang "regex" for a check that has no lang.

A two-line regex check is an error, and so is lang with no value or an empty one. A language this specification does not define is kept, and an ast-grep body may span lines:

Example 80 (parse)​

.lore/checkout.lore
<rule id="currency-from-settings" category="functional_correctness">
<check>
'USD'
|'EUR'
</check>
Display text never hard-codes a currency code.
</rule>

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

<rule id="dates-from-locale" category="functional_correctness">
<check lang="">
toLocaleDateString\(\)
</check>
Displayed dates are formatted with the shop's locale.
</rule>

<rule id="totals-from-quote" category="functional_correctness">
<check lang="structural">
items.reduce(...)
</check>
<check lang="ast-grep">
$ITEMS.reduce(
$$$
)
</check>
Cart totals come from the quote, never from summing line items.
</rule>
Expected output
{
"entries": [
{
"kind": "rule", "id": "totals-from-quote", "file": ".lore/checkout.lore",
"category": "functional_correctness", "severity": "advisory", "disabled": false,
"scopes": [{ "files": ["**"], "ignores": [] }],
"when": null,
"checks": [
{ "lang": "structural", "body": "items.reduce(...)" },
{ "lang": "ast-grep", "body": "$ITEMS.reduce(\n $$$\n)" }
],
"meta": null,
"summary": "Cart totals come from the quote, never from summing line items.",
"prose": "Cart totals come from the quote, never from summing line items.",
"src": null,
"sources": []
}
],
"errors": [
{ "name": "regex-not-one-line", "file": ".lore/checkout.lore", "entry": "currency-from-settings", "line": 2 },
{ "name": "invalid-attribute-value", "file": ".lore/checkout.lore", "entry": "price-from-quote", "line": 10 },
{ "name": "invalid-attribute-value", "file": ".lore/checkout.lore", "entry": "dates-from-locale", "line": 17 }
],
"warnings": []
}

Running checks​

A tool runs the checks of every eligible rule, and of no other rule. Each check searches the rule's counted files that have added lines; a deleted file has none. A hit names the rule, a side, the changed file and a line's number and text: on the new side the file's new path and an added line, on the old side the file's old path and a removed line (see Conditions). A check's hits and an added-line condition's hits are on the new side; a removed-line or dropped-line condition's hits are on the old side. An eligible rule's hits are those of its checks and those of its added-line, removed-line and dropped-line conditions. A rule has at most one hit per side, file and line, however many of its checks or conditions find that line.

A regex check has a hit for each added line whose text its pattern matches, as Regex dialect defines.

Supporting a check language is OPTIONAL for a conforming tool, language by language. A tool that does not support a check's language, which includes every language this specification does not define, MUST skip that check and report the warning unsupported-check-language for it, at the check's opening tag line. The rule stays eligible, and its other checks still run. Only the checks of eligible rules are reported this way, and the document model never carries this warning. A plain agent with no tooling MAY read any check as a hint about what to look for.

The first check has no lang, so it is a regex check. Both checks find line 4 of Summary.tsx, which gives one hit. currency.ts is outside the scope. The second rule is not eligible, so its check is not run:

Example 81 (eligibility, requires regex)​

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

<rule id="currency-from-settings" category="functional_correctness">
<check>
'(USD|EUR)'
</check>
<check lang="regex">
'EUR'
</check>
Display text never hard-codes a currency code.
</rule>

<rule id="currency-symbols-from-settings" category="functional_correctness">
<when>
addedContains: ["$"]
</when>
<check>
'USD'
</check>
Display text never hard-codes a currency symbol.
</rule>
Change
M apps/web/src/checkout/Price.tsx
+12 const label = 'USD'
+13 const note = "EUR"
M apps/web/src/checkout/Summary.tsx
+4 return <span>{'EUR'} {total}</span>
M apps/web/src/checkout/currency.ts
+2 export const DEFAULT_CURRENCY = 'USD'
Expected output
{
"eligible": ["currency-from-settings"],
"hits": [
{ "rule": "currency-from-settings", "file": "apps/web/src/checkout/Price.tsx", "line": 12, "text": " const label = 'USD'" },
{ "rule": "currency-from-settings", "file": "apps/web/src/checkout/Summary.tsx", "line": 4, "text": " return <span>{'EUR'} {total}</span>" }
],
"excluded": [],
"warnings": []
}

The first check's language is not one this specification defines, so the tool skips it and warns. The regex check still runs. The second rule is not eligible, so its unsupported check is not reported:

Example 82 (eligibility, requires regex)​

.lore/checkout.lore
<rule id="totals-from-quote" category="functional_correctness">
<check lang="structural">
items.reduce(...)
</check>
<check>
\.reduce\(
</check>
Cart totals come from the quote, never from summing line items.
</rule>

<rule id="refund-totals-from-quote" category="functional_correctness">
<scope>
files: ["apps/web/src/refunds/**"]
</scope>
<check lang="structural">
refund.items.reduce(...)
</check>
Refund totals come from the quote, never from summing refunded items.
</rule>
Change
M apps/web/src/checkout/CartTotal.tsx
+3 const total = items.reduce(
Expected output
{
"eligible": ["totals-from-quote"],
"hits": [
{ "rule": "totals-from-quote", "file": "apps/web/src/checkout/CartTotal.tsx", "line": 3, "text": " const total = items.reduce(" }
],
"excluded": [],
"warnings": [
{ "name": "unsupported-check-language", "file": ".lore/checkout.lore", "entry": "totals-from-quote", "line": 2 }
]
}

ast-grep checks​

An ast-grep check holds one pattern for the structural search tool ast-grep, and may span several lines. This specification follows ast-grep 0.51.0: a pattern is read with that release's pattern syntax, and a tool that supports ast-grep checks finds the same matches that release finds.

  • A pattern SHOULD use no metavariables other than $NAME, which matches one node, and $$$ or $$$NAME, which match zero or more nodes, and no other ast-grep feature, so that it keeps its meaning in later releases.
  • A file's language comes from its extension: the part of its name after the last ., when that . is not the name's first character. Extensions are compared case-sensitively, with the table below, which is the built-in list of ast-grep 0.51.0. A renamed file's extension comes from its new path.
  • A file the check searches whose name has no extension in the table is skipped, and the tool reports the warning ast-grep-no-language for it, once for each rule and skipped file. The warning's file is the skipped file's new path, its entry is the rule's id, and its line is 1.
  • Otherwise the pattern is matched against the whole new version of the file, in its language, as ast-grep 0.51.0 matches it with its default options (no project configuration, default strictness). A match covers the lines from the line of its first character to the line of its last. Each added line that a match covers is a hit. When ast-grep 0.51.0 cannot read the pattern in a file's language, the check finds nothing in that file.
LanguageExtensions
Bashbash, bats, cgi, command, env, fcgi, ksh, sh, tmux, tool, zsh
Cc, h
Cppcc, hpp, cpp, c++, hh, cxx, cu, ino
CSharpcs
Csscss, scss
Dartdart
Elixirex, exs
Gogo
Haskellhs
Hclhcl, nomad, tf, tfvars, workflow
Htmlhtml, htm, xhtml
Javajava
JavaScriptcjs, js, mjs, jsx
Jsonjson
Kotlinkt, ktm, kts
Lualua
Markdownmarkdown, md
Nixnix
Phpphp
Pythonpy, py3, pyi, bzl, bazel
Rubyrb, rbw, gemspec
Rustrs
Scalascala, sc, sbt
Soliditysol
Swiftswift
Tsxtsx
TypeScriptts, cts, mts
Yamlyaml, yml
Zigzig

The pattern matches two calls in CartTotal.tsx. The first covers only line 2, which is unchanged. The second covers lines 3 to 6, and of those only line 4 was added. Line 7 was added, but no match covers it. .graphql has no ast-grep language, so quote.graphql is skipped:

Example 83 (eligibility, requires ast-grep)​

apps/web/src/checkout/.lore/totals.lore
<scope>
files: ["**/*.{tsx,graphql}"]
</scope>

<rule id="totals-from-quote" category="functional_correctness">
<check lang="ast-grep">
$LIST.reduce($$$)
</check>
Cart totals come from the quote, never from summing line items.
</rule>
apps/web/src/checkout/CartTotal.tsx
export function CartTotal({ items, fees }) {
const feeTotal = fees.reduce((sum, fee) => sum + fee, 0)
const total = items.reduce(
(sum, item) => sum + item.price,
0,
)
return <span>{total + feeTotal}</span>
}
Change
M apps/web/src/checkout/CartTotal.tsx
+4 (sum, item) => sum + item.price,
+7 return <span>{total + feeTotal}</span>
M apps/web/src/checkout/quote.graphql
+2 total: Money!
Expected output
{
"eligible": ["totals-from-quote"],
"hits": [
{ "rule": "totals-from-quote", "file": "apps/web/src/checkout/CartTotal.tsx", "line": 4, "text": " (sum, item) => sum + item.price," }
],
"excluded": [],
"warnings": [
{ "name": "ast-grep-no-language", "file": "apps/web/src/checkout/quote.graphql", "entry": "totals-from-quote", "line": 1 }
]
}