mirror of
https://github.com/zebrajr/node.git
synced 2025-12-06 00:20:08 +01:00
tools: refactor checkimports.py
- Use f-strings for formatting. - Use raw strings for regexes alongside f-strings. - Use a generator. - Remove unnecessary `else` clause. PR-URL: https://github.com/nodejs/node/pull/50011 Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
d68d0eacaa
commit
31bde06233
|
|
@ -8,9 +8,9 @@ import sys
|
||||||
import itertools
|
import itertools
|
||||||
|
|
||||||
def do_exist(file_name, lines, imported):
|
def do_exist(file_name, lines, imported):
|
||||||
if not any(not re.match('using \w+::{0};'.format(imported), line) and
|
if not any(not re.match(fr'using \w+::{imported};', line) and
|
||||||
re.search('\\b{0}\\b'.format(imported), line) for line in lines):
|
re.search(fr'\b{imported}\b', line) for line in lines):
|
||||||
print('File "{0}" does not use "{1}"'.format(file_name, imported))
|
print(f'File "{file_name}" does not use "{imported}"')
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
@ -27,18 +27,16 @@ def is_valid(file_name):
|
||||||
usings.append(matches.group(1))
|
usings.append(matches.group(1))
|
||||||
importeds.append(matches.group(2))
|
importeds.append(matches.group(2))
|
||||||
|
|
||||||
valid = all([do_exist(file_name, lines, imported) for imported in importeds])
|
valid = all(do_exist(file_name, lines, imported) for imported in importeds)
|
||||||
|
|
||||||
sorted_usings = sorted(usings, key=lambda x: x.lower())
|
sorted_usings = sorted(usings, key=lambda x: x.lower())
|
||||||
if sorted_usings != usings:
|
if sorted_usings != usings:
|
||||||
print("using statements aren't sorted in '{0}'.".format(file_name))
|
print(f"using statements aren't sorted in '{file_name}'.")
|
||||||
for num, actual, expected in zip(line_numbers, usings, sorted_usings):
|
for num, actual, expected in zip(line_numbers, usings, sorted_usings):
|
||||||
if actual != expected:
|
if actual != expected:
|
||||||
print('\tLine {0}: Actual: {1}, Expected: {2}'
|
print(f'\tLine {num}: Actual: {actual}, Expected: {expected}')
|
||||||
.format(num, actual, expected))
|
|
||||||
return False
|
return False
|
||||||
else:
|
return valid
|
||||||
return valid
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user