forked from semgrep/semgrep
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
osemgrep: Port autofix application for
fix
(semgrep#6398)
This isn't an exact port of the relevant code in `autofix.py`. I already had some autofix application code in `Autofix.ml` for testing purposes. In semgrep#6391 I refactored it to make it usable in this context. Several differences: - Instead of tracking details about applied fixes so that we can correctly apply multiple fixes to the same file, we just apply them from bottom to top so that they can't interfere with each other. - We discard overlapping fixes and apply the ones that we can apply without conflicts. I think the Python CLI just applies them anyway, possibly incorrectly. However, the Python CLI also dedups matches that are the same except for metavariable bindings, which eliminates a major source of overlapping fixes. This logic is not ported yet. - Here, we apply fixes using the byte offset into the file, instead of reading the file, splitting into lines, and then updating using the line and column like the CLI does. This is a lot simpler. - When the `--dryrun` flag is passed, the Python CLI reports a list of fixed lines in its JSON output. This hasn't been ported yet, and in fact using the byte offset (see above) will make this a little bit more complicated. Test plan: Manual testing with the example given in semgrep#4964: `osemgrep scan -c autofix.yaml autofix.py --autofix` Note that this is an example where there are multiple matches reported for the same range, due to different possible metavariable bindings. For overlapping ranges, osemgrep applies the first one in the list that semgrep-core returns, just like the Python CLI. So, in this example it applies the same fix as the CLI. Also, ran the e2e tests: `PYTEST_USE_OSEMGREP=true pipenv run python -m pytest tests/e2e` Before: 77 passed After: 83 passed
- Loading branch information
Showing
10 changed files
with
120 additions
and
59 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
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
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
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,5 @@ | ||
open Common | ||
|
||
let unit_str ?(pad = false) count str = | ||
let str = if count <> 1 then str ^ "s" else if pad then str ^ " " else str in | ||
spf "%d %s" count str |