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
27 changes: 16 additions & 11 deletions .github/actions/prose-gate/prose_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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+)")


Expand All @@ -1130,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 = text.split()
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]
Expand Down Expand Up @@ -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"\[\[[^\n]*?=~[ \t]+(\S+)")


def blanked(value: str) -> str:
Expand Down Expand Up @@ -1714,17 +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 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 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)]
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")
Expand Down
18 changes: 18 additions & 0 deletions tests/test_prose_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4512,6 +4512,24 @@ 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 <ref>\n```\n"
self.assertEqual([3], self.lines(text))
self.assertEqual([2], self.lines("```sh\n[[ $x =~ <pattern> ]] && 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 <ref>',
"A='x y' B= git fetch <ref>",
"LABEL=two\\ words git fetch origin <ref>",
'VERSION="$(./scripts/version.sh <ref>)"',
):
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):
Expand Down