chainlint.sed: swallow comments consistently

When checking for broken a &&-chain, chainlint.sed knows that the final statement in a subshell should not end with `&&`, so it takes care to make a distinction between the final line which is an actual statement and any lines which may be mere comments preceding the closing ')'. As such, it swallows comment lines so that they do not interfere with the &&-chain check. However, since `sed` does not provide any sort of real recursion, chainlint.sed only checks &&-chains in subshells one level deep; it doesn't do any checking in deeper subshells or in `{...}` blocks within subshells. Furthermore, on account of potential implementation complexity, it doesn't check &&-chains within `case` arms. Due to an oversight, it also doesn't swallow comments inside deep subshells, `{...}` blocks, or `case` statements, which makes its output inconsistent (swallowing comments in some cases but not others). Unfortunately, this inconsistency seeps into the chainlint self-test "expect" files, which potentially makes it difficult to reuse the self-tests should a more capable chainlint ever be developed. Therefore, teach chainlint.sed to consistently swallow comments in all cases. Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Dec 13, 2021 at 01:30 UTC 31da22d1fd46e80c592f6b04c6cafdb983c8a903
6 files changed +52 -4
t/chainlint.sed
+19 -2
@@ -294,6 +294,12 @@ bfolded
294 x
295 s/?!HERE?!/<</g
296 n
297 +:cascom
298 +/^[ ]*#/{
299 + N
300 + s/.*\n//
301 + bcascom
302 +}
303 /^[ ]*esac/bslurp
304 bcase
305
@@ -322,10 +328,15 @@ x
328 :nstslrp
329 s/?!HERE?!/<</g
330 n
331 +:nstcom
332 +# comment -- not closing ")" if in comment
333 +/^[ ]*#/{
334 + N
335 + s/.*\n//
336 + bnstcom
337 +}
338 # closing ")" on own line -- stop nested slurp
339 /^[ ]*)/bnstcl
327 -# comment -- not closing ")" if in comment
328 -/^[ ]*#/bnstcnt
340 # "$((...))" -- arithmetic expansion; not closing ")"
341 /\$(([^)][^)]*))[^)]*$/bnstcnt
342 # "$(...)" -- command substitution; not closing ")"
@@ -345,6 +356,12 @@ bchkchn
356 x
357 s/?!HERE?!/<</g
358 n
359 +:blkcom
360 +/^[ ]*#/{
361 + N
362 + s/.*\n//
363 + bblkcom
364 +}
365 # closing "}" -- stop block slurp
366 /}/bchkchn
367 bblock
t/chainlint/block-comment.expect new
+6
@@ -0,0 +1,6 @@
1 +(
2 + {
3 + echo a &&
4 + echo b
5 + }
6 +)
t/chainlint/block-comment.test new
+8
@@ -0,0 +1,8 @@
1 +(
2 + {
3 + # show a
4 + echo a &&
5 + # show b
6 + echo b
7 + }
8 +)
t/chainlint/case-comment.expect new
+8
@@ -0,0 +1,8 @@
1 +(
2 + case "$x" in
3 + x) foo ;;
4 + *)
5 + bar
6 + ;;
7 + esac
8 +)
t/chainlint/case-comment.test new
+11
@@ -0,0 +1,11 @@
1 +(
2 + case "$x" in
3 + # found foo
4 + x) foo ;;
5 + # found other
6 + *)
7 + # treat it as bar
8 + bar
9 + ;;
10 + esac
11 +)
t/chainlint/nested-subshell-comment.expect
-2
@@ -2,8 +2,6 @@
2 foo &&
3 (
4 bar &&
5 - # bottles wobble while fiddles gobble
6 - # minor numbers of cows (or do they?)
5 baz &&
6 snaff
7 ) ?!AMP?!