Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/actions/prose-gate/prose_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,34 @@ def resume_at(carry: Carried, line: str) -> tuple[Carried, int | None]:
# The token count carries the test, since a colon ending real prose always has words before it.
KEY_ONLY = re.compile(r"^\S+:$")

# One key, a colon, and one value is a line of configuration a reader pastes, not a sentence.
# `comment-case` reads the first character, so every key opening lowercase was rejected.
# The keys that passed are the ones opening on anything else, and each changes what is pasted.
# A capitalized key changes the key, and a quoted or underscored one changes how it is written.
# A leading dash is a different YAML construct from the mapping the line belongs to.
# The same snippet already passes inside a Markdown fence, which is what makes this a gap.
# A value carrying a space is not exempt, which keeps the shape to a key-value line.
# A body ending as a sentence is judged as one, the exemption pairing a guard with its anchor.
# `is_tool_directive` pairs one for the same reason, an anchor alone having exempted such a body.
# The exemption reads `SNIPPET_END` rather than `SENT_END`, whose set carries the colon.
# A colon ends configuration rather than a sentence here, which is why `KEY_ONLY` exists above.
# Read with the colon in, `address: fd00::` and `image: ghcr.io/o/n:` were refused.
# The exemption costs two detections and the guard refuses one shape, all stated here.
# The common cost is a marker comment, `todo: refactor` and `note: obsolete` being exempt now.
# The second is a two-token body with no terminator inside a sentence that wraps.
# A sentence wrapping through one reports the line under it for case instead of the wrap.
# A sentence ending on one loses its wrap with nothing reported at all.
# The guard refuses a value ending in a full stop, an FQDN being the case.
# Dropping the full stop from `SNIPPET_END` leaves `!` and `?` guarded.
# It re-exempts a wrapped sentence whose continuation ends in one, which the guard is for.
# So the full stop stays in that set, and the refusal of an FQDN stands with it.
# This repository carries no instance of any of the three, so measuring it bounds none of them.
# `is_comment_prose` deliberately does not carry this exemption, unlike its three siblings.
# `comment-added` reads a body of any case, so exempting the shape there frees `Owner: alice`.
# Pricing an added comment is what that rule is for.
SNIPPET = re.compile(r"^\S+:\s+\S+$")
SNIPPET_END = re.compile(r"[.!?][\"')\]]?\s*$")

# A label opening a definition names the thing being defined, so it is not the sentence's first word.
# `# publish - 'true' when ...` documents an output named `publish`.
# Capitalizing it renames the output the workflow declares.
Expand Down Expand Up @@ -1860,6 +1888,7 @@ def comment_wrap_findings(path: Path, raw: str, lines: list[str]) -> list[tuple[
or NOT_PROSE.search(body)
or BARE_URI.match(body.strip())
or KEY_ONLY.match(body)
or (SNIPPET.match(body) and not SNIPPET_END.search(body))
):
prev_body = ""
continue
Expand Down
2 changes: 2 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ A comment sentence also has to start with a capital, which `comment-case` checks

**A comment body that is one token closing on a colon is a key or a heading rather than a sentence**, and neither rule applies to it. A `# ignore:` heading a commented-out block is disabled configuration, so the capitalization `comment-case` asks for corrupts the key a reader uncomments, and the rule damages the file it was pointed at. The token count carries the test, because a colon ending real prose always has words before it. Measured over this repo, the shape matches exactly two bodies, both of them a key or a heading, where the broader reading of a body with no whitespace at all covers 99 and sweeps in every reference URI and version marker with them.

**A comment body of one key, a colon and one value is configuration rather than a sentence**, and neither rule applies to it. `comment-case` reads the first character, so a snippet pasted into a comment could not pass while its key opened lowercase, and the keys that did pass are the ones opening on something else, each changing what a reader pastes: a capitalized, quoted or underscored key changes the key, and a leading dash is a different YAML construct from the mapping it belongs to. The same snippet already passes inside a Markdown fence, which is what makes this a gap rather than a constraint. The shape is one value carrying no space, and a body ending as a sentence is judged as one, the exemption pairing a guard with its own anchor as `is_tool_directive` does and for the same reason. The exemption reads `SNIPPET_END` rather than `SENT_END`, whose set carries the colon: a colon ends configuration rather than a sentence here, which is why the lone-key exemption above exists, and reading it as a terminator refused `address: fd00::` and `image: ghcr.io/o/n:`. The exemption costs two detections and the guard refuses one shape, all stated rather than left to be found. The common cost is a marker comment, since `todo: refactor` and `note: obsolete` are the same shape as a config line. The second is a two-token body with no terminator inside a wrapping sentence, which loses that sentence its wrap and reports the line under it for case instead, or loses it silently where the body ends the wrap. The refusal is not a lost detection but a finding the exemption fails to remove: a value ending in a full stop is judged as before, an FQDN such as `origin: example.com.` being the case. Dropping the full stop from `SNIPPET_END` would leave `!` and `?` guarded and re-exempt the wrapped sentence the guard exists for, so it stays. `is_comment_prose` deliberately does not carry this exemption, unlike its three siblings, since `comment-added` reads a body of any case and exempting the shape there would free `Owner: alice` from the label as well. Measured over this repository the exemption newly reaches one body, a command to paste, and that measurement bounds neither cost nor the refusal, this tree carrying no instance of any of them.

**A label opening a definition is exempt from `comment-case`, because the lowercase word is the name being defined.** A `# publish - 'true' when ...` documents an output named `publish`, and capitalizing it renames the output the workflow declares. This is the comment spelling of the `- **Label** - text` construct the dash rule already exempts, and it is the same reason a step marker is stripped before the sentence checks. The exemption costs a detection, stated rather than left to be found: a comment that genuinely opens a sentence on one word followed by a spaced dash is no longer read as a lowercase opening. It is scoped to a line that opens a definition and is never applied to one continuing a wrapped sentence, since a continuation whose first word takes a spaced dash is a parenthetical, which is the construction the dash rule exists to catch. Both live instances in this repo are continuations, so the corpus rather than the intuition chose the unit: the first design read any comment body and would have exempted both.

**A comment whose whole body is a URI is a reference rather than a sentence**, and neither rule applies to it. It cannot be capitalized or restructured without corrupting the address it exists to carry, so before the exemption every repo carrying a reference block inherited a finding no edit could answer. Consecutive reference lines are separate addresses rather than one sentence wrapping, which is why the exemption also stops the line below a URI from reading as its continuation. A URI inside a sentence is still prose, so the exemption requires the whole body to be the address and nothing else.
Expand Down
113 changes: 113 additions & 0 deletions scripts/tests/test_prose_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,119 @@ def test_a_commented_out_key_is_not_a_sentence(self) -> None:
with self.subTest(text=text.splitlines()[0]):
self.assertEqual([], self.flag("a.yml", text))

def test_a_key_and_its_value_are_configuration_rather_than_a_sentence(self) -> None:
"""`comment-case` reads the first character, so a key opening lowercase could not pass.

The reported case is the `External usage:` header a fleet of ESPHome templates carries.
`# packages:` was already exempt as a lone key and the line under it was not. What passed
is any key opening on something other than a lowercase letter, and each such spelling
changes what a reader pastes: a capitalized, quoted or underscored key changes the key,
and a leading dash is a different YAML construct from the mapping it belongs to. The same
snippet already passes inside a Markdown fence.
"""
header = (
"# External usage:\n"
"# packages:\n"
"# device: github://ptr727/ESPHome-Config/templates/x.yaml@main\n"
)
self.assertEqual([], self.flag("a.yml", header))
# Two snippet lines running together are two snippet lines, not a wrapped sentence.
self.assertEqual([], self.flag("b.yml", "# Wiring:\n# sda: GPIO17\n# scl: GPIO16\n"))

def test_a_value_carrying_a_space_is_still_judged(self) -> None:
"""The shape is one key and one value, which is what keeps prose inside the rule."""
self.assertEqual(["comment-case"], self.flag("a.yml", "# note: this value has spaces\n"))

def test_the_shape_needs_the_space_after_its_colon(self) -> None:
"""The space is the shape rather than decoration, a key and a value being two tokens.

An empty key is not asserted here: a body opening on its colon opens on no lowercase
letter, so `comment-case` never reads it and widening the key would pin nothing.
"""
self.assertEqual(["comment-case"], self.flag("a.yml", "# key:value\n"))

def test_a_two_token_body_punctuated_as_a_sentence_is_still_judged(self) -> None:
"""Without the guard the shape exempts any two-word sentence.

It silenced a real wrapped sentence whose continuation happened to be two tokens.
`is_tool_directive` pairs a guard with its own anchor the same way, for the same failure.
The guard does cost: a value ending in a full stop is refused, `origin: example.com.`
being the case. Dropping the full stop from `SNIPPET_END` would re-exempt the wrapped
sentence the guard exists for, so it stays.
"""
for body in ("# one: two.\n", "# conclusion: wrong!\n", "# result: unchanged.\n"):
with self.subTest(body=body.strip()):
self.assertEqual(["comment-case"], self.flag("a.yml", body))
self.assertEqual(
["comment-wrap"],
self.flag("b.yml", "# The rule reads the file at\n# hand: correctly.\n"),
)

def test_a_punctuated_markdown_html_comment_is_still_prose(self) -> None:
"""That branch keeps a punctuated HTML comment deliberately, and this must not take it."""
self.assertEqual(
["comment-case"], self.flag("a.md", "Text.\n\n<!-- result: unchanged. -->\n")
)

def test_a_sentence_wrapping_through_a_snippet_line_loses_its_wrap(self) -> None:
"""One of the exemption's costs, pinned so a later change meets it rather than finds it.

A two-token body carrying no terminator is exempt wherever it sits. A sentence wrapping
through one loses the wrap and the line under it is reported for case instead, and a
sentence ending on one loses the wrap with no finding at all, which is the quieter half.
The sentence guard cannot reach either, the body ending in no punctuation.
"""
self.assertEqual([], self.flag("c.yml", "# The value the hook reads is\n# empty: false\n"))
self.assertEqual(
["comment-case"],
self.flag("a.yml", "# The value the hook reads is\n# empty: false\n# by default.\n"),
)

def test_the_exemption_holds_in_every_comment_syntax(self) -> None:
"""One case per syntax, so a failure names the language whose extractor broke.

Seven file names covering six specs proved nothing about the markers they never used.
These eleven reach ten of the eleven syntaxes the table declares, the kicad spec being the
one neither this case nor the run-on case beside it reaches.
"""
snippet = "sda: GPIO17"
for name, text in (
("a.cs", f"// Wiring:\n// {snippet}\n"),
("a.c", f"/* Wiring: */\n/* {snippet} */\n"),
("a.py", f"# Wiring:\n# {snippet}\n"),
("a.sh", f"# Wiring:\n# {snippet}\n"),
("a.ps1", f"<# Wiring: #>\n<# {snippet} #>\n"),
("a.yml", f"# Wiring:\n# {snippet}\n"),
("a.toml", f"# Wiring:\n# {snippet}\n"),
("a.ini", f"; Wiring:\n; {snippet}\n"),
("a.jsonc", f"// Wiring:\n// {snippet}\n"),
("a.xml", f"<!-- Wiring: -->\n<!-- {snippet} -->\n"),
("a.css", f"/* Wiring: */\n/* {snippet} */\n"),
):
with self.subTest(file=name, syntax=text.strip()[:12]):
self.assertEqual([], self.flag(name, text))

def test_a_value_ending_in_a_colon_is_still_configuration(self) -> None:
"""A colon ends configuration rather than a sentence, which is why `KEY_ONLY` exists.

Guarded with `SENT_END`, whose set carries the colon, an address, an image tag and a
drive letter were each refused and told to capitalize their key, which is the finding
this exemption exists to remove.
"""
for value in ("fd00::", "ghcr.io/o/n:", "C:"):
with self.subTest(value=value):
self.assertEqual([], self.flag("a.yml", f"# Paths:\n# root: {value}\n"))

def test_a_marker_comment_is_exempt_which_is_the_common_cost(self) -> None:
"""The shape cannot tell a two-token marker from a two-token config line.

This is the cost the rule pays most often, pinned so a later change meets it rather than
discovers it. Narrowing it needs a reading of the value, which is a rule of its own.
"""
for body in ("# todo: refactor\n", "# note: obsolete\n", "# fixme: later\n"):
with self.subTest(body=body.strip()):
self.assertEqual([], self.flag("a.yml", body))

def test_a_colon_ending_real_prose_is_still_judged(self) -> None:
"""The exemption is one token wide, since prose closing on a colon has words before it."""
self.assertEqual(["comment-case"], self.flag("a.yml", "# the outputs are these:\n"))
Expand Down
Loading