-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable token-based rules on source with syntax errors #11950
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
crates/ruff_linter/resources/test/fixtures/flake8_commas/COM81_syntax_error.py
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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
# Check for `flake8-commas` violation for a file containing syntax errors. | ||
( | ||
*args | ||
) | ||
|
||
def foo[(param1='test', param2='test',): | ||
pass | ||
|
29 changes: 29 additions & 0 deletions
29
crates/ruff_linter/resources/test/fixtures/flake8_implicit_str_concat/ISC_syntax_error.py
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,29 @@ | ||
# The lexer doesn't emit a string token if it's unterminated | ||
"a" "b | ||
"a" "b" "c | ||
"a" """b | ||
c""" "d | ||
|
||
# For f-strings, the `FStringRanges` won't contain the range for | ||
# unterminated f-strings. | ||
f"a" f"b | ||
f"a" f"b" f"c | ||
f"a" f"""b | ||
c""" f"d {e | ||
|
||
( | ||
"a" | ||
"b | ||
"c" | ||
"d" | ||
) | ||
|
||
|
||
# Triple-quoted strings, if unterminated, consume everything that comes after | ||
# the opening quote. So, no test code should raise the violation after this. | ||
( | ||
"""abc""" | ||
f"""def | ||
"g" "h" | ||
"i" "j" | ||
) |
26 changes: 26 additions & 0 deletions
26
crates/ruff_linter/resources/test/fixtures/pycodestyle/E30_syntax_error.py
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,26 @@ | ||
# Check for E30 errors in a file containing syntax errors with unclosed | ||
# parenthesis. | ||
|
||
def foo[T1, T2(): | ||
pass | ||
|
||
def bar(): | ||
pass | ||
|
||
|
||
|
||
class Foo: | ||
def __init__( | ||
pass | ||
def method(): | ||
pass | ||
|
||
foo = Foo( | ||
|
||
|
||
def top( | ||
def nested1(): | ||
pass | ||
def nested2(): | ||
pass | ||
|
13 changes: 13 additions & 0 deletions
13
crates/ruff_linter/resources/test/fixtures/pylint/invalid_characters_syntax_error.py
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,13 @@ | ||
# These test cases contain syntax errors. The characters within the unterminated | ||
# strings shouldn't be highlighted. | ||
|
||
# Before any syntax error | ||
b = '' | ||
# Unterminated string | ||
b = ' | ||
b = '' | ||
# Unterminated f-string | ||
b = f' | ||
b = f'' | ||
# Implicitly concatenated | ||
b = '' f'' ' |
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 |
---|---|---|
|
@@ -107,14 +107,9 @@ where | |
fn extract_noqa_line_for(tokens: &Tokens, locator: &Locator, indexer: &Indexer) -> NoqaMapping { | ||
let mut string_mappings = Vec::new(); | ||
|
||
for token in tokens.up_to_first_unknown() { | ||
for token in tokens { | ||
match token.kind() { | ||
TokenKind::EndOfFile => { | ||
break; | ||
} | ||
|
||
Comment on lines
+110
to
-115
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The token stream doesn't contain the |
||
// For multi-line strings, we expect `noqa` directives on the last line of the | ||
// string. | ||
// For multi-line strings, we expect `noqa` directives on the last line of the string. | ||
TokenKind::String if token.is_triple_quoted_string() => { | ||
if locator.contains_line_break(token.range()) { | ||
string_mappings.push(TextRange::new( | ||
|
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 |
---|---|---|
|
@@ -24,7 +24,7 @@ pub(crate) struct DocLines<'a> { | |
impl<'a> DocLines<'a> { | ||
fn new(tokens: &'a Tokens) -> Self { | ||
Self { | ||
inner: tokens.up_to_first_unknown().iter(), | ||
inner: tokens.iter(), | ||
prev: TextSize::default(), | ||
} | ||
Comment on lines
24
to
29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This extracts a specific set of comments so it doesn't require any specific update. |
||
} | ||
|
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
28 changes: 24 additions & 4 deletions
28
...ke8_commas/snapshots/ruff_linter__rules__flake8_commas__tests__COM81_syntax_error.py.snap
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 |
---|---|---|
@@ -1,10 +1,30 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_commas/mod.rs | ||
--- | ||
COM81_syntax_error.py:2:5: SyntaxError: Starred expression cannot be used here | ||
COM81_syntax_error.py:3:5: SyntaxError: Starred expression cannot be used here | ||
| | ||
1 | ( | ||
2 | *args | ||
1 | # Check for `flake8-commas` violation for a file containing syntax errors. | ||
2 | ( | ||
3 | *args | ||
| ^ | ||
3 | ) | ||
4 | ) | ||
| | ||
|
||
COM81_syntax_error.py:6:9: SyntaxError: Type parameter list cannot be empty | ||
| | ||
4 | ) | ||
5 | | ||
6 | def foo[(param1='test', param2='test',): | ||
| ^ | ||
7 | pass | ||
| | ||
|
||
COM81_syntax_error.py:6:38: COM819 Trailing comma prohibited | ||
| | ||
4 | ) | ||
5 | | ||
6 | def foo[(param1='test', param2='test',): | ||
| ^ COM819 | ||
7 | pass | ||
| | ||
= help: Remove trailing comma |
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
181 changes: 181 additions & 0 deletions
181
...ts/ruff_linter__rules__flake8_implicit_str_concat__tests__ISC001_ISC_syntax_error.py.snap
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,181 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/flake8_implicit_str_concat/mod.rs | ||
--- | ||
ISC_syntax_error.py:2:5: SyntaxError: missing closing quote in string literal | ||
| | ||
1 | # The lexer doesn't emit a string token if it's unterminated | ||
2 | "a" "b | ||
| ^ | ||
3 | "a" "b" "c | ||
4 | "a" """b | ||
| | ||
|
||
ISC_syntax_error.py:2:7: SyntaxError: Expected a statement | ||
| | ||
1 | # The lexer doesn't emit a string token if it's unterminated | ||
2 | "a" "b | ||
| ^ | ||
3 | "a" "b" "c | ||
4 | "a" """b | ||
5 | c""" "d | ||
| | ||
|
||
ISC_syntax_error.py:3:1: ISC001 Implicitly concatenated string literals on one line | ||
| | ||
1 | # The lexer doesn't emit a string token if it's unterminated | ||
2 | "a" "b | ||
3 | "a" "b" "c | ||
| ^^^^^^^ ISC001 | ||
4 | "a" """b | ||
5 | c""" "d | ||
| | ||
= help: Combine string literals | ||
|
||
ISC_syntax_error.py:3:9: SyntaxError: missing closing quote in string literal | ||
| | ||
1 | # The lexer doesn't emit a string token if it's unterminated | ||
2 | "a" "b | ||
3 | "a" "b" "c | ||
| ^ | ||
4 | "a" """b | ||
5 | c""" "d | ||
| | ||
|
||
ISC_syntax_error.py:3:11: SyntaxError: Expected a statement | ||
| | ||
1 | # The lexer doesn't emit a string token if it's unterminated | ||
2 | "a" "b | ||
3 | "a" "b" "c | ||
| ^ | ||
4 | "a" """b | ||
5 | c""" "d | ||
| | ||
|
||
ISC_syntax_error.py:4:1: ISC001 Implicitly concatenated string literals on one line | ||
| | ||
2 | "a" "b | ||
3 | "a" "b" "c | ||
4 | / "a" """b | ||
5 | | c""" "d | ||
| |____^ ISC001 | ||
6 | | ||
7 | # For f-strings, the `FStringRanges` won't contain the range for | ||
| | ||
= help: Combine string literals | ||
|
||
ISC_syntax_error.py:5:6: SyntaxError: missing closing quote in string literal | ||
| | ||
3 | "a" "b" "c | ||
4 | "a" """b | ||
5 | c""" "d | ||
| ^ | ||
6 | | ||
7 | # For f-strings, the `FStringRanges` won't contain the range for | ||
| | ||
|
||
ISC_syntax_error.py:5:8: SyntaxError: Expected a statement | ||
| | ||
3 | "a" "b" "c | ||
4 | "a" """b | ||
5 | c""" "d | ||
| ^ | ||
6 | | ||
7 | # For f-strings, the `FStringRanges` won't contain the range for | ||
8 | # unterminated f-strings. | ||
| | ||
|
||
ISC_syntax_error.py:9:8: SyntaxError: f-string: unterminated string | ||
| | ||
7 | # For f-strings, the `FStringRanges` won't contain the range for | ||
8 | # unterminated f-strings. | ||
9 | f"a" f"b | ||
| ^ | ||
10 | f"a" f"b" f"c | ||
11 | f"a" f"""b | ||
| | ||
|
||
ISC_syntax_error.py:9:9: SyntaxError: Expected FStringEnd, found newline | ||
| | ||
7 | # For f-strings, the `FStringRanges` won't contain the range for | ||
8 | # unterminated f-strings. | ||
9 | f"a" f"b | ||
| ^ | ||
10 | f"a" f"b" f"c | ||
11 | f"a" f"""b | ||
12 | c""" f"d {e | ||
| | ||
|
||
ISC_syntax_error.py:10:1: ISC001 Implicitly concatenated string literals on one line | ||
| | ||
8 | # unterminated f-strings. | ||
9 | f"a" f"b | ||
10 | f"a" f"b" f"c | ||
| ^^^^^^^^^ ISC001 | ||
11 | f"a" f"""b | ||
12 | c""" f"d {e | ||
| | ||
= help: Combine string literals | ||
|
||
ISC_syntax_error.py:10:13: SyntaxError: f-string: unterminated string | ||
| | ||
8 | # unterminated f-strings. | ||
9 | f"a" f"b | ||
10 | f"a" f"b" f"c | ||
| ^ | ||
11 | f"a" f"""b | ||
12 | c""" f"d {e | ||
| | ||
|
||
ISC_syntax_error.py:10:14: SyntaxError: Expected FStringEnd, found newline | ||
| | ||
8 | # unterminated f-strings. | ||
9 | f"a" f"b | ||
10 | f"a" f"b" f"c | ||
| ^ | ||
11 | f"a" f"""b | ||
12 | c""" f"d {e | ||
| | ||
|
||
ISC_syntax_error.py:11:1: ISC001 Implicitly concatenated string literals on one line | ||
| | ||
9 | f"a" f"b | ||
10 | f"a" f"b" f"c | ||
11 | / f"a" f"""b | ||
12 | | c""" f"d {e | ||
| |____^ ISC001 | ||
13 | | ||
14 | ( | ||
| | ||
= help: Combine string literals | ||
|
||
ISC_syntax_error.py:16:5: SyntaxError: missing closing quote in string literal | ||
| | ||
14 | ( | ||
15 | "a" | ||
16 | "b | ||
| ^ | ||
17 | "c" | ||
18 | "d" | ||
| | ||
|
||
ISC_syntax_error.py:26:9: SyntaxError: f-string: unterminated triple-quoted string | ||
| | ||
24 | ( | ||
25 | """abc""" | ||
26 | f"""def | ||
| ^ | ||
27 | "g" "h" | ||
28 | "i" "j" | ||
| | ||
|
||
ISC_syntax_error.py:30:1: SyntaxError: unexpected EOF while parsing | ||
| | ||
28 | "i" "j" | ||
29 | ) | ||
| | ||
|
||
ISC_syntax_error.py:30:1: SyntaxError: f-string: unterminated string | ||
| | ||
28 | "i" "j" | ||
29 | ) | ||
| |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking at string tokens and the lexer doesn't emit them if it's unterminated. So, we might get away with not doing anything in this case for now.