fix(scripts): unused import regex and misc refactoring #362
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I noticed the regex for detecting unused imports was changed in a recent PR. This broke the detection of unused imports. The change made it so you would only detect unused lines that explicitly say
@import("...")
. But those are not the only lines we want to check. We also want to check for things likeconst Thing = sig.utils.Thing;
. The recent change also broke detection of identifiers with numbers and underscores. So I fixed these issues.I also made the regex slightly better at detecting file paths within
@import
lines by including/
as a valid character. This identified few more unused imports which I also removed in this PR.I consolidated the code to a main function so it's clearer what's actually running when you run the script.
I used return values to indicate failed checks, instead of directly calling
exit(1)
within the check function. That way it always runs all the checks, instead of exiting early.I renamed the file to
style.py
.check_style.py
is misleading because it's not just a check. By default this script will automatically edit the code to adjust its style. Only if you provide the--check
flag does it limit itself to checks. At that point, the name is redundant with the flag. I see "style" as analogous to zig's "fmt" subcommand. By defaultzig fmt
will autoformat the code, and then it limits itself to checks if you use--check
.