Skip to content

Check E004 once per file, not once per invocation - #9

Draft
taowang487 wants to merge 1 commit into
openstack:masterfrom
taowang487:fix-e004-per-file
Draft

Check E004 once per file, not once per invocation#9
taowang487 wants to merge 1 commit into
openstack:masterfrom
taowang487:fix-e004-per-file

Conversation

@taowang487

Copy link
Copy Markdown

The last line should always end with a newline check sits at the same indent level as the for fname in files loop, so it runs a single time after every file has been processed, against the line variable left over from whichever file was scanned last. Files earlier in the list are never checked.

bashate a.sh b.sh   # a.sh missing its newline -> not reported
bashate b.sh a.sh   # same a.sh                -> reported

This inverts a 2016 regression. Before
I54963cd25efc9f9603fbcc60b8e25bb11ea2d7db the check lived in the isfirstline() block and, as that commit noted, could not fire unless you passed two files; the fix moved it out of the loop but dedented one level too far, so now it cannot fire unless the file is passed last. The test added at the same time is single-file, so it passes under both behaviours.

Move the check inside the per-file loop and track the file's last raw line separately -- line is unusable here because it is rewritten by inline-comment stripping and rebound by the for line in logical_line loop. filename/filelineno are passed explicitly since fileinput is exhausted by that point.

Callers that batch files into one invocation, such as pre-commit, are most affected: the same offending file passes or fails depending only on how the file list happens to be ordered.

The "last line should always end with a newline" check sits at the
same indent level as the `for fname in files` loop, so it runs a
single time after every file has been processed, against the `line`
variable left over from whichever file was scanned last.  Files
earlier in the list are never checked.

    bashate a.sh b.sh   # a.sh missing its newline -> not reported
    bashate b.sh a.sh   # same a.sh                -> reported

This inverts a 2016 regression.  Before
I54963cd25efc9f9603fbcc60b8e25bb11ea2d7db the check lived in the
isfirstline() block and, as that commit noted, could not fire unless
you passed two files; the fix moved it out of the loop but dedented
one level too far, so now it cannot fire unless the file is passed
last.  The test added at the same time is single-file, so it passes
under both behaviours.

Move the check inside the per-file loop and track the file's last raw
line separately -- `line` is unusable here because it is rewritten by
inline-comment stripping and rebound by the `for line in logical_line`
loop.  filename/filelineno are passed explicitly since fileinput is
exhausted by that point.

Callers that batch files into one invocation, such as pre-commit, are
most affected: the same offending file passes or fails depending only
on how the file list happens to be ordered.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant