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
28 changes: 21 additions & 7 deletions .github/actions/prose-gate/prose_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -1308,10 +1308,23 @@ def rule_sentence_split(unit: Unit, ctx: Context) -> list[Finding]:
r"""^\s*-\s+["']?(?:[A-Za-z]:(?=[\\/]))?(?:\$\{[^}]*\}|[^:\s"'{}])+:(?P<target>/[^:\s"']*)"""
)
LONG_MOUNT = re.compile(r"""^\s*(?:-\s+)?target:\s+["']?(?P<target>/[^\s"']*)""")
BLOCK_SCALAR = re.compile(
r"^(?P<lead>\s*(?:-\s+)*)(?P<key>[^\s#][^#]*?:\s+)?(?:[!&]\S*\s+)*[|>]"
r"(?:[-+][1-9]?|[1-9][-+]?)?\s*(?:#.*)?$"
)
SCALAR_LEAD = re.compile(r"^\s*(?:-\s+)*")
SCALAR_KEY = re.compile(r":\s+(?:[!&]\S*\s+)*$")


def scalar_openers(texts: list[str]) -> dict[int, int]:
"""The line index of each YAML block scalar header, and the column its content sits deeper than."""
out: dict[int, int] = {}
text = "\n".join(texts)
starts = Starts(text)
for offset, kind, value in lexer_for("yaml").get_tokens_unprocessed(text):
if kind not in Punctuation.Indicator or value not in "|>":
continue
number, col = starts.at(offset)
row, line = number - 1, texts[number - 1]
lead = len(line) - len(SCALAR_LEAD.sub("", line, count=1))
out[row] = lead if SCALAR_KEY.search(line[lead:col]) else len(line) - len(line.lstrip())
return out


def container_paths(lines: tuple[Line, ...]) -> dict[int, range]:
Expand All @@ -1324,16 +1337,17 @@ def container_paths(lines: tuple[Line, ...]) -> dict[int, range]:
out: dict[int, range] = {}
block: int | None = None
opaque: int | None = None
for line in lines:
openers = scalar_openers([line.text for line in lines])
for row, line in enumerate(lines):
body = line.text.lstrip()
if not body or body.startswith("#"):
continue
indent = len(line.text) - len(body)
if opaque is not None and indent > opaque:
continue
opaque = None
if scalar := BLOCK_SCALAR.match(line.text):
opaque = len(scalar["lead"]) if scalar["key"] else indent
if row in openers:
opaque = openers[row]
if block is not None and (indent < block or (indent == block and body[0] != "-")):
block = None
if VOLUMES_KEY.match(line.text):
Expand Down
16 changes: 16 additions & 0 deletions tests/test_prose_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3626,6 +3626,22 @@ def test_a_compose_block_scalar_is_text_rather_than_a_volume(self) -> None:
)
with self.subTest(indicator=indicator):
self.assertEqual(["home-path"], self.kinds(text, {"home-path"}, "compose.yml"))
for key, indicator in (
("x-a#b", "|"),
("x-a#b", "| # note"),
("x-a#b", ">-"),
('"x-a#b"', "|"),
("'x a'", "|"),
('"x #b"', "|"),
("'x # b'", "|"),
("x", "| # note"),
):
text = (
f"services:\n ledger:\n {key}: {indicator}\n volumes:\n"
f" - ledger-cache:{NIX_HOME}/data\n{real}"
)
with self.subTest(key=key, indicator=indicator):
self.assertEqual(["home-path"], self.kinds(text, {"home-path"}, "compose.yml"))
target = f" target: {NIX_HOME}/media"
for entry in (
f" - type: bind\n x-note: |\n - ./old:{NIX_HOME}/media\n{target}",
Expand Down
Loading