diff --git a/crates/ruff/resources/test/fixtures/isort/bom_sorted.py b/crates/ruff/resources/test/fixtures/isort/bom_sorted.py new file mode 100644 index 0000000000..f13e4fddea --- /dev/null +++ b/crates/ruff/resources/test/fixtures/isort/bom_sorted.py @@ -0,0 +1,2 @@ +import bar +import foo diff --git a/crates/ruff/resources/test/fixtures/isort/bom_unsorted.py b/crates/ruff/resources/test/fixtures/isort/bom_unsorted.py new file mode 100644 index 0000000000..ba03995229 --- /dev/null +++ b/crates/ruff/resources/test/fixtures/isort/bom_unsorted.py @@ -0,0 +1,2 @@ +import foo +import bar diff --git a/crates/ruff/src/rules/isort/mod.rs b/crates/ruff/src/rules/isort/mod.rs index 55412202d8..854b26728b 100644 --- a/crates/ruff/src/rules/isort/mod.rs +++ b/crates/ruff/src/rules/isort/mod.rs @@ -314,6 +314,8 @@ mod tests { #[test_case(Path::new("add_newline_before_comments.py"))] #[test_case(Path::new("as_imports_comments.py"))] + #[test_case(Path::new("bom_sorted.py"))] + #[test_case(Path::new("bom_unsorted.py"))] #[test_case(Path::new("combine_as_imports.py"))] #[test_case(Path::new("combine_import_from.py"))] #[test_case(Path::new("comments.py"))] diff --git a/crates/ruff/src/rules/isort/snapshots/ruff__rules__isort__tests__bom_sorted.py.snap b/crates/ruff/src/rules/isort/snapshots/ruff__rules__isort__tests__bom_sorted.py.snap new file mode 100644 index 0000000000..94aa1559b8 --- /dev/null +++ b/crates/ruff/src/rules/isort/snapshots/ruff__rules__isort__tests__bom_sorted.py.snap @@ -0,0 +1,4 @@ +--- +source: crates/ruff/src/rules/isort/mod.rs +--- + diff --git a/crates/ruff/src/rules/isort/snapshots/ruff__rules__isort__tests__bom_unsorted.py.snap b/crates/ruff/src/rules/isort/snapshots/ruff__rules__isort__tests__bom_unsorted.py.snap new file mode 100644 index 0000000000..ac86dda07b --- /dev/null +++ b/crates/ruff/src/rules/isort/snapshots/ruff__rules__isort__tests__bom_unsorted.py.snap @@ -0,0 +1,18 @@ +--- +source: crates/ruff/src/rules/isort/mod.rs +--- +bom_unsorted.py:1:1: I001 [*] Import block is un-sorted or un-formatted + | +1 | import foo + | _^ +2 | | import bar + | + = help: Organize imports + +ℹ Fix +1 |-import foo +2 |-import bar + 1 |+import bar + 2 |+import foo + + diff --git a/crates/ruff_source_file/src/locator.rs b/crates/ruff_source_file/src/locator.rs index f2b1a836d0..92b01bfc1d 100644 --- a/crates/ruff_source_file/src/locator.rs +++ b/crates/ruff_source_file/src/locator.rs @@ -76,7 +76,11 @@ impl<'a> Locator<'a> { if let Some(index) = memrchr2(b'\n', b'\r', bytes) { // SAFETY: Safe because `index < offset` TextSize::try_from(index).unwrap().add(TextSize::from(1)) + } else if self.contents.starts_with('\u{feff}') { + // Skip the BOM. + '\u{feff}'.text_len() } else { + // Start of file. TextSize::default() } }