Re: [PATCH 1/3] chainlint.pl: fix line number reporting
- From
Eric Sunshine <sunshine@sunshineco.com>
- Date
- Jul 8, 2024, 05:08 UTC
- Message-ID
- <CAPig+cRxnpG9Yfix09EnJAbnzwN=yoUtSeYxt7S-Od+xBgfaYQ@mail.gmail.com>
- In-Reply-To
- <20240706060515.GA700151@coredump.intra.peff.net>
On 7/6/24 2:05 AM, Jeff King wrote:
Show 31 quoted lines
> The previous commit taught chainlint.pl to handle test bodies in
> heredocs, but there are two small bugs related to line numbers:
>
> 2. For an invocation like the one above, if the test_expect_success
> line is X, then "test body" would correctly start at X+1, since the
> hanging newline at the start of the single-quoted test body
> increments the count. But for a here-doc, there is an implicit
> newline at the end of the token stream before the here-doc starts.
> We have to increment "lineno" to account for this.
>
> Actually, this is not _quite_ correct, as there could be multiple
> here-docs, like:
>
> test_expect_success "$(cat <<END_OF_TITLE)" - <<END_OF_TEST
> this is the title
> END_OF_TITLE
> this is the test
> END_OF_TEST
>
> in which case we'd need to skip past END_OF_TITLE. Given how
> unlikely it is for anybody to do this, and since it would only
> affect line numbers, it's probably not worth caring about too much.
> The solution would probably be to record the starting line number
> of each here-doc section in the lexer/shellparser stage.
>
> Signed-off-by: Jeff King <peff@peff.net>
> ---
> I actually suspect the "record the heredoc line number" thing would not
> be too hard. I.e., turn ShellParser's "heredoc" hash to point to
> hashrefs like: "{ content => ..., lineno => ... }". And that would give
> us a good spot to stick an "interpolate" boolean later if we want.It turned out to be quite easy. See below for an implementation atop your patch [1/3] (modulo Gmail whitespace damage). Given how simple this ended up being, it probably makes sense to squash this change in, as well.
--- >8 ---
diff --git a/t/chainlint.pl b/t/chainlint.pl index c9ab79b6b0..b31cb263f8 100755 --- a/t/chainlint.pl +++ b/t/chainlint.pl @@ -174,8 +174,10 @@ sub swallow_heredocs { $$b =~ /(?:\G|\n)$indent\Q$$tag[0]\E(?:\n|\z)/gc; if (pos($$b) > $start) { my $body = substr($$b, $start, pos($$b) - $start); - $self->{parser}->{heredocs}->{$$tag[0]} = - substr($body, 0, length($body) - length($&)); + $self->{parser}->{heredocs}->{$$tag[0]} = { + content => substr($body, 0, length($body) - length($&)), + start_line => $self->{lineno}, + }; $self->{lineno} += () = $body =~ /\n/sg; next; } @@ -624,8 +626,9 @@ sub check_test { my $lineno = $body->[3]; $body = unwrap($body); if ($body eq '-') { - $body = shift @_; - $lineno++; + my $herebody = shift @_; + $body = $herebody->{content}; + $lineno = $herebody->{start_line}; } $self->{ntests}++; my $parser = TestParser->new(\$body); --