diff --git a/crates/ruff_linter/src/noqa.rs b/crates/ruff_linter/src/noqa.rs index c2392b0e6bf60..ec352191e6c73 100644 --- a/crates/ruff_linter/src/noqa.rs +++ b/crates/ruff_linter/src/noqa.rs @@ -1274,6 +1274,30 @@ mod tests { assert_lexed_ranges_match_slices(directive, source); } + #[test] + fn malformed_code_1() { + let source = "# noqa: F"; + let directive = lex_inline_noqa(TextRange::up_to(source.text_len()), source); + assert_debug_snapshot!(directive); + assert_lexed_ranges_match_slices(directive, source); + } + + #[test] + fn malformed_code_2() { + let source = "# noqa: RUF001, F"; + let directive = lex_inline_noqa(TextRange::up_to(source.text_len()), source); + assert_debug_snapshot!(directive); + assert_lexed_ranges_match_slices(directive, source); + } + + #[test] + fn malformed_code_3() { + let source = "# noqa: RUF001F"; + let directive = lex_inline_noqa(TextRange::up_to(source.text_len()), source); + assert_debug_snapshot!(directive); + assert_lexed_ranges_match_slices(directive, source); + } + #[test] fn noqa_code() { let source = "# noqa: F401"; @@ -1530,6 +1554,30 @@ mod tests { assert_lexed_ranges_match_slices(exemption, source); } + #[test] + fn flake8_malformed_code_1() { + let source = "# flake8: noqa: F"; + let directive = lex_file_exemption(TextRange::up_to(source.text_len()), source); + assert_debug_snapshot!(directive); + assert_lexed_ranges_match_slices(directive, source); + } + + #[test] + fn flake8_malformed_code_2() { + let source = "# flake8: noqa: RUF001, F"; + let directive = lex_file_exemption(TextRange::up_to(source.text_len()), source); + assert_debug_snapshot!(directive); + assert_lexed_ranges_match_slices(directive, source); + } + + #[test] + fn flake8_malformed_code_3() { + let source = "# flake8: noqa: RUF001F"; + let directive = lex_file_exemption(TextRange::up_to(source.text_len()), source); + assert_debug_snapshot!(directive); + assert_lexed_ranges_match_slices(directive, source); + } + #[test] fn ruff_exemption_all() { let source = "# ruff: noqa"; @@ -1562,6 +1610,30 @@ mod tests { assert_lexed_ranges_match_slices(exemption, source); } + #[test] + fn ruff_malformed_code_1() { + let source = "# ruff: noqa: F"; + let directive = lex_file_exemption(TextRange::up_to(source.text_len()), source); + assert_debug_snapshot!(directive); + assert_lexed_ranges_match_slices(directive, source); + } + + #[test] + fn ruff_malformed_code_2() { + let source = "# ruff: noqa: RUF001, F"; + let directive = lex_file_exemption(TextRange::up_to(source.text_len()), source); + assert_debug_snapshot!(directive); + assert_lexed_ranges_match_slices(directive, source); + } + + #[test] + fn ruff_malformed_code_3() { + let source = "# ruff: noqa: RUF001F"; + let directive = lex_file_exemption(TextRange::up_to(source.text_len()), source); + assert_debug_snapshot!(directive); + assert_lexed_ranges_match_slices(directive, source); + } + #[test] fn flake8_exemption_all_no_space() { let source = "#flake8:noqa"; diff --git a/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__flake8_malformed_code_1.snap b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__flake8_malformed_code_1.snap new file mode 100644 index 0000000000000..196dcb58f8968 --- /dev/null +++ b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__flake8_malformed_code_1.snap @@ -0,0 +1,7 @@ +--- +source: crates/ruff_linter/src/noqa.rs +expression: directive +--- +Err( + MissingCodes, +) diff --git a/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__flake8_malformed_code_2.snap b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__flake8_malformed_code_2.snap new file mode 100644 index 0000000000000..915b3393f2831 --- /dev/null +++ b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__flake8_malformed_code_2.snap @@ -0,0 +1,22 @@ +--- +source: crates/ruff_linter/src/noqa.rs +expression: directive +--- +Ok( + Some( + NoqaLexerOutput { + warnings: [], + directive: Codes( + Codes { + range: 0..22, + codes: [ + Code { + code: "RUF001", + range: 16..22, + }, + ], + }, + ), + }, + ), +) diff --git a/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__flake8_malformed_code_3.snap b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__flake8_malformed_code_3.snap new file mode 100644 index 0000000000000..44d5fcaa2262c --- /dev/null +++ b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__flake8_malformed_code_3.snap @@ -0,0 +1,7 @@ +--- +source: crates/ruff_linter/src/noqa.rs +expression: directive +--- +Err( + InvalidCodeSuffix, +) diff --git a/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__malformed_code_1.snap b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__malformed_code_1.snap new file mode 100644 index 0000000000000..196dcb58f8968 --- /dev/null +++ b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__malformed_code_1.snap @@ -0,0 +1,7 @@ +--- +source: crates/ruff_linter/src/noqa.rs +expression: directive +--- +Err( + MissingCodes, +) diff --git a/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__malformed_code_2.snap b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__malformed_code_2.snap new file mode 100644 index 0000000000000..e7a3632209800 --- /dev/null +++ b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__malformed_code_2.snap @@ -0,0 +1,22 @@ +--- +source: crates/ruff_linter/src/noqa.rs +expression: directive +--- +Ok( + Some( + NoqaLexerOutput { + warnings: [], + directive: Codes( + Codes { + range: 0..14, + codes: [ + Code { + code: "RUF001", + range: 8..14, + }, + ], + }, + ), + }, + ), +) diff --git a/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__malformed_code_3.snap b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__malformed_code_3.snap new file mode 100644 index 0000000000000..44d5fcaa2262c --- /dev/null +++ b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__malformed_code_3.snap @@ -0,0 +1,7 @@ +--- +source: crates/ruff_linter/src/noqa.rs +expression: directive +--- +Err( + InvalidCodeSuffix, +) diff --git a/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__ruff_malformed_code_1.snap b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__ruff_malformed_code_1.snap new file mode 100644 index 0000000000000..196dcb58f8968 --- /dev/null +++ b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__ruff_malformed_code_1.snap @@ -0,0 +1,7 @@ +--- +source: crates/ruff_linter/src/noqa.rs +expression: directive +--- +Err( + MissingCodes, +) diff --git a/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__ruff_malformed_code_2.snap b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__ruff_malformed_code_2.snap new file mode 100644 index 0000000000000..c59da01dbcdba --- /dev/null +++ b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__ruff_malformed_code_2.snap @@ -0,0 +1,22 @@ +--- +source: crates/ruff_linter/src/noqa.rs +expression: directive +--- +Ok( + Some( + NoqaLexerOutput { + warnings: [], + directive: Codes( + Codes { + range: 0..20, + codes: [ + Code { + code: "RUF001", + range: 14..20, + }, + ], + }, + ), + }, + ), +) diff --git a/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__ruff_malformed_code_3.snap b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__ruff_malformed_code_3.snap new file mode 100644 index 0000000000000..44d5fcaa2262c --- /dev/null +++ b/crates/ruff_linter/src/snapshots/ruff_linter__noqa__tests__ruff_malformed_code_3.snap @@ -0,0 +1,7 @@ +--- +source: crates/ruff_linter/src/noqa.rs +expression: directive +--- +Err( + InvalidCodeSuffix, +)