-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
linter: only lint on updated lines in the new file (#4409)
- Loading branch information
Showing
6 changed files
with
571 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import difflib | ||
|
||
import whatthepatch | ||
|
||
|
||
def get_diff(old_contents: str, new_contents: str, filepath: str = 'file') -> str: | ||
diff = list( | ||
difflib.unified_diff( | ||
old_contents.split('\n'), | ||
new_contents.split('\n'), | ||
fromfile=filepath, | ||
tofile=filepath, | ||
# do not output unchange lines | ||
# because they can cause `parse_diff` to fail | ||
n=0, | ||
) | ||
) | ||
return '\n'.join(map(lambda x: x.rstrip(), diff)) | ||
|
||
|
||
def parse_diff(diff_patch: str) -> list[whatthepatch.patch.Change]: | ||
# handle empty patch | ||
if diff_patch.strip() == '': | ||
return [] | ||
|
||
patch = whatthepatch.parse_patch(diff_patch) | ||
patch_list = list(patch) | ||
assert len(patch_list) == 1, ( | ||
'parse_diff only supports single file diff. But got:\nPATCH:\n' | ||
+ diff_patch | ||
+ '\nPATCH LIST:\n' | ||
+ str(patch_list) | ||
) | ||
changes = patch_list[0].changes | ||
|
||
# ignore changes that are the same (i.e., old_lineno == new_lineno) | ||
output_changes = [] | ||
for change in changes: | ||
if change.old != change.new: | ||
output_changes.append(change) | ||
return output_changes |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.