Repository navigation
fix(rules): case arms inside $(...), escaped operators before & #1143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
42e6416
ec9d330
94c7968
68c010d
f010a27
deeb493
3f7e944
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -271,8 +271,12 @@ defmodule Hypatia.Workflow do | |
| # the word, so a `#` after it opens a comment; the `)` of `$(…)` does not, | ||
| # so `$(name)#suffix` is one word (Codex and CodeRabbit on #1129). `parens` | ||
| # is the stack of open `(`: :subst for `$(`, :sub for a bare `(`. A `)` | ||
| # with nothing open (a `case` pattern) ends the word. | ||
| # with nothing open ends the word. The word `case` pushes :case_head, `in` makes it | ||
| # :case and `esac` pops it; while :case is on top, a `)` ends a pattern, not the enclosing | ||
| # `$(…)`, so it ends the word and pops nothing (Codex on #1129). | ||
| @word_breaks [?\s, ?\t, ?;, ?&, ?|, ?(, ?<, ?>] | ||
| # What may follow the words `case` and `esac`. | ||
| @word_ends [?\s, ?\t, ?\n, ?;, ?&, ?|, ?(, ?), ?<, ?>] | ||
|
|
||
| defp mask(<<>>, _q, _ws?, _cmt?, _qs?, _parens, acc), | ||
| do: acc |> Enum.reverse() |> IO.iodata_to_binary() | ||
|
|
@@ -297,6 +301,26 @@ defmodule Hypatia.Workflow do | |
| mask(r, q, false, false, qs?, ps, kept ++ acc) | ||
| end | ||
|
|
||
| # `case` opens a `case` only where a command starts. | ||
| defp mask(<<"case", c, r::binary>>, nil, true, _, qs?, ps, acc) when c in @word_ends do | ||
| if command_start?(acc), | ||
| do: mask(<<c, r::binary>>, nil, false, false, qs?, [:case_head | ps], ["case" | acc]), | ||
| else: mask(<<"ase", c, r::binary>>, nil, false, false, qs?, ps, [?c | acc]) | ||
| end | ||
|
|
||
| # Between `case` and `in` (the subject) no reserved word counts; `in` turns | ||
| # the head into the body, where `esac` closes it (Codex on #1143). | ||
| defp mask(<<"in", c, r::binary>>, nil, true, _, qs?, [:case_head | ps], acc) | ||
| when c in @word_ends, | ||
| do: mask(<<c, r::binary>>, nil, false, false, qs?, [:case | ps], ["in" | acc]) | ||
|
|
||
| defp mask(<<"esac", c, r::binary>>, nil, true, _, qs?, [:case | ps], acc) | ||
| when c in @word_ends, | ||
| do: mask(<<c, r::binary>>, nil, false, false, qs?, ps, ["esac" | acc]) | ||
|
hyperpolymath marked this conversation as resolved.
Resolved
|
||
|
|
||
| defp mask(<<"esac">>, nil, true, _, qs?, [:case | ps], acc), | ||
| do: mask(<<>>, nil, false, false, qs?, ps, ["esac" | acc]) | ||
|
|
||
| defp mask(<<?$, ?(, r::binary>>, nil, _, _, qs?, ps, acc), | ||
| do: mask(r, nil, true, false, qs?, [:subst | ps], [?(, ?$ | acc]) | ||
|
|
||
|
|
@@ -305,6 +329,7 @@ defmodule Hypatia.Workflow do | |
|
|
||
| defp mask(<<?), r::binary>>, nil, _, _, qs?, ps, acc) do | ||
| case ps do | ||
| [top | _] when top in [:case, :case_head] -> mask(r, nil, true, false, qs?, ps, [?) | acc]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When an outer arm starts a nested case on the same line, as in Useful? React with 👍 / 👎. |
||
| [:subst | rest] -> mask(r, nil, false, false, qs?, rest, [?) | acc]) | ||
| [_ | rest] -> mask(r, nil, true, false, qs?, rest, [?) | acc]) | ||
| [] -> mask(r, nil, true, false, qs?, [], [?) | acc]) | ||
|
|
@@ -317,6 +342,36 @@ defmodule Hypatia.Workflow do | |
| defp mask(<<c, r::binary>>, q, _, _, qs?, ps, acc), | ||
| do: mask(r, q, false, false, qs?, ps, [if(qs?, do: blank(c), else: c) | acc]) | ||
|
|
||
| # After only blanks since a newline, `;`, `&`, `|`, `(`, the start, or a | ||
| # reserved word that a command follows (`then`, `do`, `else`, ...; Codex | ||
| # on #1143). | ||
| @command_words ~w(then do else elif if while until ! { time coproc) | ||
|
|
||
| defp command_start?(acc) do | ||
| case Enum.drop_while(acc, &(&1 in [?\s, ?\t])) do | ||
| [] -> true | ||
| # A separator escaped with a backslash is a literal (Codex on #1143). | ||
| [c, ?\\ | _] when c in [?\n, ?;, ?&, ?|, ?(] -> false | ||
| [c | _] when c in [?\n, ?;, ?&, ?|, ?(] -> true | ||
|
hyperpolymath marked this conversation as resolved.
Resolved
hyperpolymath marked this conversation as resolved.
Resolved
|
||
| rest -> previous_word(rest) in @command_words and word_start_before?(rest) | ||
|
hyperpolymath marked this conversation as resolved.
Resolved
|
||
| end | ||
| end | ||
|
|
||
| # The word that ends the (reversed) accumulator, and whether a command | ||
| # could start before it. | ||
| defp previous_word(rest) do | ||
| rest | ||
| |> Enum.take_while(&(is_integer(&1) and &1 not in [?\s, ?\t, ?\n, ?;, ?&, ?|, ?(])) | ||
| |> Enum.reverse() | ||
| |> List.to_string() | ||
| end | ||
|
|
||
| defp word_start_before?(rest) do | ||
| rest | ||
| |> Enum.drop_while(&(is_integer(&1) and &1 not in [?\s, ?\t, ?\n, ?;, ?&, ?|, ?(])) | ||
| |> command_start?() | ||
| end | ||
|
|
||
| defp blank(?\n), do: ?\n | ||
| defp blank(_), do: ?\s | ||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.