Skip to content

Commit

Permalink
Respect isort:skip action comment (#1722)
Browse files Browse the repository at this point in the history
Resolves: #1718.
  • Loading branch information
charliermarsh committed Jan 7, 2023
1 parent a9cc56b commit 16d933f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
30 changes: 20 additions & 10 deletions resources/test/fixtures/isort/skip.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
# isort: off
import sys
import os
import collections
# isort: on

import sys
import os # isort: skip
import collections
import abc
def f():
# isort: off
import sys
import os
import collections
# isort: on


def f():
import sys
import os # isort: skip
import collections
import abc


def f():
import sys
import os # isort:skip
import collections
import abc
7 changes: 5 additions & 2 deletions src/directives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,13 @@ pub fn extract_isort_directives(lxr: &[LexResult]) -> IsortDirectives {
continue;
};

// `isort` allows for `# isort: skip` and `# isort: skip_file` to include or
// omit a space after the colon. The remaining action comments are
// required to include the space, and must appear on their own lines.
let comment_text = comment_text.trim_end();
if comment_text == "# isort: split" {
splits.push(start.row());
} else if comment_text == "# isort: skip_file" {
} else if comment_text == "# isort: skip_file" || comment_text == "# isort:skip_file" {
skip_file = true;
} else if off.is_some() {
if comment_text == "# isort: on" {
Expand All @@ -119,7 +122,7 @@ pub fn extract_isort_directives(lxr: &[LexResult]) -> IsortDirectives {
off = None;
}
} else {
if comment_text.contains("isort: skip") {
if comment_text.contains("isort: skip") || comment_text.contains("isort:skip") {
exclusions.insert(start.row());
} else if comment_text == "# isort: off" {
off = Some(start);
Expand Down
20 changes: 10 additions & 10 deletions src/isort/snapshots/ruff__isort__tests__skip.py.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@ expression: checks
- kind:
UnsortedImports: ~
location:
row: 7
row: 12
column: 0
end_location:
row: 8
row: 14
column: 0
fix:
content: "import sys\n\n"
content: " import abc\n import collections\n"
location:
row: 7
row: 12
column: 0
end_location:
row: 8
row: 14
column: 0
parent: ~
- kind:
UnsortedImports: ~
location:
row: 9
row: 19
column: 0
end_location:
row: 11
row: 21
column: 0
fix:
content: "import abc\nimport collections\n"
content: " import abc\n import collections\n"
location:
row: 9
row: 19
column: 0
end_location:
row: 11
row: 21
column: 0
parent: ~

0 comments on commit 16d933f

Please sign in to comment.