From 8e1a0526476ac54975180500750b0faa456ee032 Mon Sep 17 00:00:00 2001 From: Pieter Viljoen Date: Mon, 23 Nov 2026 07:45:22 -0800 Subject: [PATCH 1/3] Read a Regex Operand and a Quoted Assignment Correctly in the paste-safe Rule A bracket after =~ is the shell's regex syntax, not a placeholder, and a quoted assignment holding spaces no longer hides the command that follows it. Co-Authored-By: Claude Sonnet 5.5 --- .github/actions/prose-gate/prose_lint.py | 11 +++++++---- tests/test_prose_lint.py | 9 +++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/.github/actions/prose-gate/prose_lint.py b/.github/actions/prose-gate/prose_lint.py index 68719a787..57991c197 100755 --- a/.github/actions/prose-gate/prose_lint.py +++ b/.github/actions/prose-gate/prose_lint.py @@ -1119,7 +1119,8 @@ def extract_markdown(lines: list[str]) -> Extracted: COMMAND_NAMES = frozenset(COMMANDS["names"]) COMMAND_SUFFIXES = tuple(COMMANDS["script_suffixes"]) COMMAND_FENCES: dict[str, str] = COMMANDS["fences"] -ASSIGNMENT = re.compile(r"[A-Za-z_]\w*=\S*") +ASSIGNMENT = re.compile(r"[A-Za-z_]\w*=(?:\"[^\"]*\"|'[^']*'|[^\s\"'])*") +WORD = re.compile(r"(?:\"[^\"]*\"|'[^']*'|[^\s\"'])+") SUBSTITUTED = re.compile(r"[A-Za-z_]\w*=[\"']?\$\((\S+)") @@ -1130,7 +1131,7 @@ def opens_command(text: str) -> bool: script alone in a span is a file being named rather than run, so it needs an argument. An assignment from a command substitution opens with the command it runs. """ - words = text.split() + words = WORD.findall(text) if words[:1] == ["$"]: words = words[1:] while words and ASSIGNMENT.fullmatch(words[0]): @@ -1679,6 +1680,7 @@ def rule_dead_path(unit: Unit, ctx: Context) -> list[Finding]: CASE_ARM = re.compile(r"(?m)^[ \t]*\(?[^\s()<>]+\)") +REGEX_OPERAND = re.compile(r"=~[ \t]+(\S+)") def blanked(value: str) -> str: @@ -1714,14 +1716,15 @@ def rule_paste_safe(unit: Unit, ctx: Context) -> list[Finding]: Each command is a code span opening with a command or a shell fence's content. Each finding is reported on the line holding the placeholder. A string handed to a second shell, as `sh -c` - takes one, is read as the quotation it is to the first. A `case` arm's pattern and a regular - expression's bracket are the shell's own syntax, so neither is a placeholder. + takes one, is read as the quotation it is to the first. A `case` arm's pattern and the operand + of `=~` are the shell's own syntax, so neither is a placeholder. """ out: list[Finding] = [] for line in unit.lines: dialect = COMMANDS["dialects"][line.dialect] bare = shell_literals(line.text, dialect["lexer"]) taken = [range(m.start(), m.end()) for m in CASE_ARM.finditer(bare)] + taken += [range(m.start(1), m.end(1)) for m in REGEX_OPERAND.finditer(bare)] for shape in dialect["shapes"]: for m in PASTE_SHAPES[shape].finditer(bare): if any(m.start() in r for r in taken): diff --git a/tests/test_prose_lint.py b/tests/test_prose_lint.py index 147363368..e97d56ed8 100755 --- a/tests/test_prose_lint.py +++ b/tests/test_prose_lint.py @@ -4512,6 +4512,15 @@ def test_a_shell_pattern_in_a_fence_is_no_placeholder(self) -> None: ) self.assertEqual([], self.lines(text)) + def test_a_regex_operand_is_no_placeholder_but_a_real_one_still_is(self) -> None: + text = "```sh\n[[ $x =~ [A-Za-z] ]] && echo ok\ngit fetch origin \n```\n" + self.assertEqual([3], self.lines(text)) + + def test_a_quoted_assignment_with_spaces_still_opens_a_command(self) -> None: + for span in ('LABEL="two words" git fetch origin ', "A='x y' B= git fetch "): + with self.subTest(span=span): + self.assertEqual([1], self.lines(f"Run `{span}` here.\n")) + def test_a_fence_in_another_language_is_not_read(self) -> None: for lang in ("text", "yaml", ""): with self.subTest(lang=lang): From e9e5bf6a997a711014e329d924f0edbdb8e1f272 Mon Sep 17 00:00:00 2001 From: Pieter Viljoen Date: Mon, 23 Nov 2026 07:50:17 -0800 Subject: [PATCH 2/3] Keep a Quoted Command Substitution and an Angle Placeholder Visible to paste-safe Co-Authored-By: Claude Sonnet 5.5 --- .github/actions/prose-gate/prose_lint.py | 26 +++++++++++++----------- tests/test_prose_lint.py | 7 ++++++- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/.github/actions/prose-gate/prose_lint.py b/.github/actions/prose-gate/prose_lint.py index 57991c197..37da9bfcf 100755 --- a/.github/actions/prose-gate/prose_lint.py +++ b/.github/actions/prose-gate/prose_lint.py @@ -1120,7 +1120,6 @@ def extract_markdown(lines: list[str]) -> Extracted: COMMAND_SUFFIXES = tuple(COMMANDS["script_suffixes"]) COMMAND_FENCES: dict[str, str] = COMMANDS["fences"] ASSIGNMENT = re.compile(r"[A-Za-z_]\w*=(?:\"[^\"]*\"|'[^']*'|[^\s\"'])*") -WORD = re.compile(r"(?:\"[^\"]*\"|'[^']*'|[^\s\"'])+") SUBSTITUTED = re.compile(r"[A-Za-z_]\w*=[\"']?\$\((\S+)") @@ -1131,14 +1130,15 @@ def opens_command(text: str) -> bool: script alone in a span is a file being named rather than run, so it needs an argument. An assignment from a command substitution opens with the command it runs. """ - words = WORD.findall(text) - if words[:1] == ["$"]: - words = words[1:] - while words and ASSIGNMENT.fullmatch(words[0]): - if m := SUBSTITUTED.match(words[0]): - words = [m[1], *words[1:]] + rest = text.strip() + if rest.split()[:1] == ["$"]: + rest = rest[1:].lstrip() + while (m := ASSIGNMENT.match(rest)) and rest[m.end() : m.end() + 1].strip() == "": + if sub := SUBSTITUTED.match(rest): + rest = rest[sub.start(1) :] break - words = words[1:] + rest = rest[m.end() :].lstrip() + words = rest.split() if not words: return False head = words[0] @@ -1716,18 +1716,20 @@ def rule_paste_safe(unit: Unit, ctx: Context) -> list[Finding]: Each command is a code span opening with a command or a shell fence's content. Each finding is reported on the line holding the placeholder. A string handed to a second shell, as `sh -c` - takes one, is read as the quotation it is to the first. A `case` arm's pattern and the operand - of `=~` are the shell's own syntax, so neither is a placeholder. + takes one, is read as the quotation it is to the first. A `case` arm's pattern and the bracket + and alternation of a `=~` operand are the shell's own syntax, so neither is a placeholder. """ out: list[Finding] = [] for line in unit.lines: dialect = COMMANDS["dialects"][line.dialect] bare = shell_literals(line.text, dialect["lexer"]) taken = [range(m.start(), m.end()) for m in CASE_ARM.finditer(bare)] - taken += [range(m.start(1), m.end(1)) for m in REGEX_OPERAND.finditer(bare)] + operands = [range(m.start(1), m.end(1)) for m in REGEX_OPERAND.finditer(bare)] for shape in dialect["shapes"]: for m in PASTE_SHAPES[shape].finditer(bare): - if any(m.start() in r for r in taken): + if any(m.start() in r for r in taken) or ( + shape != "angle" and any(m.start() in r for r in operands) + ): continue taken.append(range(m.start(), m.end())) at = line.number + bare[: m.start()].count("\n") diff --git a/tests/test_prose_lint.py b/tests/test_prose_lint.py index e97d56ed8..dcbd076c5 100755 --- a/tests/test_prose_lint.py +++ b/tests/test_prose_lint.py @@ -4515,9 +4515,14 @@ def test_a_shell_pattern_in_a_fence_is_no_placeholder(self) -> None: def test_a_regex_operand_is_no_placeholder_but_a_real_one_still_is(self) -> None: text = "```sh\n[[ $x =~ [A-Za-z] ]] && echo ok\ngit fetch origin \n```\n" self.assertEqual([3], self.lines(text)) + self.assertEqual([2], self.lines("```sh\n[[ $x =~ ]] && echo ok\n```\n")) def test_a_quoted_assignment_with_spaces_still_opens_a_command(self) -> None: - for span in ('LABEL="two words" git fetch origin ', "A='x y' B= git fetch "): + for span in ( + 'LABEL="two words" git fetch origin ', + "A='x y' B= git fetch ", + 'VERSION="$(./scripts/version.sh )"', + ): with self.subTest(span=span): self.assertEqual([1], self.lines(f"Run `{span}` here.\n")) From a08b1fd68e3b5f8bbb148a7000740059bda890ff Mon Sep 17 00:00:00 2001 From: Pieter Viljoen Date: Mon, 23 Nov 2026 08:35:11 -0800 Subject: [PATCH 3/3] Consume Escaped Spaces in Assignments and Limit the Regex Exemption to [[ ]] Co-Authored-By: Claude Sonnet 5.5 --- .github/actions/prose-gate/prose_lint.py | 6 +++--- tests/test_prose_lint.py | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.github/actions/prose-gate/prose_lint.py b/.github/actions/prose-gate/prose_lint.py index 37da9bfcf..9f95d3b7a 100755 --- a/.github/actions/prose-gate/prose_lint.py +++ b/.github/actions/prose-gate/prose_lint.py @@ -1119,7 +1119,7 @@ def extract_markdown(lines: list[str]) -> Extracted: COMMAND_NAMES = frozenset(COMMANDS["names"]) COMMAND_SUFFIXES = tuple(COMMANDS["script_suffixes"]) COMMAND_FENCES: dict[str, str] = COMMANDS["fences"] -ASSIGNMENT = re.compile(r"[A-Za-z_]\w*=(?:\"[^\"]*\"|'[^']*'|[^\s\"'])*") +ASSIGNMENT = re.compile(r"[A-Za-z_]\w*=(?:\\.|\"[^\"]*\"|'[^']*'|[^\s\"'\\])*") SUBSTITUTED = re.compile(r"[A-Za-z_]\w*=[\"']?\$\((\S+)") @@ -1680,7 +1680,7 @@ def rule_dead_path(unit: Unit, ctx: Context) -> list[Finding]: CASE_ARM = re.compile(r"(?m)^[ \t]*\(?[^\s()<>]+\)") -REGEX_OPERAND = re.compile(r"=~[ \t]+(\S+)") +REGEX_OPERAND = re.compile(r"\[\[[^\n]*?=~[ \t]+(\S+)") def blanked(value: str) -> str: @@ -1717,7 +1717,7 @@ def rule_paste_safe(unit: Unit, ctx: Context) -> list[Finding]: Each command is a code span opening with a command or a shell fence's content. Each finding is reported on the line holding the placeholder. A string handed to a second shell, as `sh -c` takes one, is read as the quotation it is to the first. A `case` arm's pattern and the bracket - and alternation of a `=~` operand are the shell's own syntax, so neither is a placeholder. + and alternation of a `[[ =~ ]]` operand are the shell's own syntax, so neither is a placeholder. """ out: list[Finding] = [] for line in unit.lines: diff --git a/tests/test_prose_lint.py b/tests/test_prose_lint.py index dcbd076c5..3fba0a45f 100755 --- a/tests/test_prose_lint.py +++ b/tests/test_prose_lint.py @@ -4517,10 +4517,14 @@ def test_a_regex_operand_is_no_placeholder_but_a_real_one_still_is(self) -> None self.assertEqual([3], self.lines(text)) self.assertEqual([2], self.lines("```sh\n[[ $x =~ ]] && echo ok\n```\n")) + def test_an_equals_tilde_outside_a_conditional_is_no_regex(self) -> None: + self.assertEqual([2], self.lines("```sh\necho x =~ [RepoName]\n```\n")) + def test_a_quoted_assignment_with_spaces_still_opens_a_command(self) -> None: for span in ( 'LABEL="two words" git fetch origin ', "A='x y' B= git fetch ", + "LABEL=two\\ words git fetch origin ", 'VERSION="$(./scripts/version.sh )"', ): with self.subTest(span=span):