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: 19 additions & 9 deletions .github/actions/prose-gate/prose_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -1691,23 +1691,34 @@ def blanked(value: str) -> str:
return re.sub(r"[^\n]", " ", value)


def heredoc_rest(value: str) -> tuple[int, int] | None:
"""Where a heredoc token's opening line resumes past its label and where that line ends.

None where `value` opens no heredoc. The text between the two is ordinary shell code. A match
always holds a line break, since `HEREDOC` requires one after the opening line.
"""
if not (m := re.match(HEREDOC, value)):
return None
return m.end(4) + len(m[2]), value.find("\n")


def shell_literals(text: str, lexer: str) -> str:
"""The command with what its shell takes literally blanked, each line break kept.

That is a quotation, an escape, a comment, a `${...}` expansion, and a heredoc's label and body,
as the shell's lexer reads them. So a `$(...)` inside a double quotation stays code. A heredoc's
opening line past its label is code too. A line break is added for the lexer, since its
comment rule ends on one and a code span has none.
opening line past its label is read as any other command is, its own literals blanked. A line
break is added for the lexer, since its comment rule ends on one and a code span has none.
"""
out: list[str] = []
depth = 0
for kind, value in lexer_for(lexer).get_tokens(text + "\n"):
opens = kind in String.Interpol and value == "${"
depth += opens - (depth > 0 and kind in String.Interpol and value == "}")
if kind in String.Heredoc and (m := re.match(HEREDOC, value)):
cut, eol = m.end(4) + len(m[2]), value.find("\n")
eol = len(value) if eol < 0 else eol
out.append(blanked(value[:cut]) + value[cut:eol] + blanked(value[eol:]))
if kind in String.Heredoc and (split := heredoc_rest(value)):
cut, eol = split
rest = shell_literals(value[cut:eol], lexer)
out.append(blanked(value[:cut]) + rest + blanked(value[eol:]))
elif opens or depth or kind in Comment or (kind in String and kind not in String.Backtick):
out.append(blanked(value))
else:
Expand Down Expand Up @@ -1772,9 +1783,8 @@ def shell_char_kinds(text: str, lexer: str) -> str:
out.append("#" * len(value))
elif kind in String.Escape and value == "\\\n":
out.append("ww")
elif kind in String.Heredoc and (m := re.match(HEREDOC, value)):
cut, eol = m.end(4) + len(m[2]), value.find("\n")
eol = len(value) if eol < 0 else eol
elif kind in String.Heredoc and (split := heredoc_rest(value)):
cut, eol = split
rest = shell_char_kinds(value[cut:eol], lexer)
out.append("h" * cut + rest + "H" * (len(value) - eol))
elif kind in String.Heredoc:
Expand Down
18 changes: 16 additions & 2 deletions tests/test_prose_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4686,10 +4686,24 @@ def test_a_launcher_name_alone_opens_no_command_span(self) -> None:
self.assertEqual([2], self.lines("```sh\nnice -n 5 dash -c 'ls <dir>'\n```\n"))

def test_a_second_shell_after_a_heredoc_label_is_read(self) -> None:
"""The first shell takes the quoted string literally, so only the second shell reports it."""
text = "```sh\ncat <<'END' | sudo sh -c 'cat > <file>'\nbody\nEND\n```\n"
found = self.hits(text)
self.assertIn(2, [n for n, _ in found])
self.assertTrue(any("the string 'sh' hands" in message for _, message in found), found)
self.assertEqual([2], [n for n, _ in found])
self.assertIn("the string 'sh' hands", found[0][1])

def test_a_quotation_after_a_heredoc_label_is_literal(self) -> None:
"""The rest of a heredoc's opening line is read as any command is, so its twin still fails."""
for rest, expected in (
('| tee "<file>"', []),
("| tee '<file>'", []),
("| tee <file> # <note>", [2]),
):
with self.subTest(rest=rest):
text = f"```sh\ncat <<'END' {rest}\nbody\nEND\n```\n"
self.assertEqual(expected, self.lines(text))
Comment thread
ptr727 marked this conversation as resolved.
Resolved
text = "```sh\ncat <<'END' - <<'TWO' | tee \"<file>\"\none\nEND\ntwo\nTWO\n```\n"
self.assertEqual([], self.lines(text))

def test_a_second_shell_after_a_reserved_word_is_read(self) -> None:
text = "```sh\nif ssh '<host>' 'test -d <dir>'; then echo ok; fi\n```\n"
Expand Down
Loading