Skip to content

Commit

Permalink
Only track noqa directives for multi-line strings
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Nov 5, 2022
1 parent 95073b1 commit f0dfce9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
17 changes: 17 additions & 0 deletions resources/test/fixtures/E501.py
Expand Up @@ -32,3 +32,20 @@ def caller(string: str) -> None:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
""", # noqa: E501
)

multiple_strings_per_line = {
# OK
"Lorem ipsum dolor": "sit amet",
# E501 Line too long
"Lorem ipsum dolor": "sit amet consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
# E501 Line too long
"Lorem ipsum dolor": """
sit amet consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
""",
# OK
"Lorem ipsum dolor": "sit amet consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.", # noqa: E501
# OK
"Lorem ipsum dolor": """
sit amet consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
""", # noqa: E501
}
5 changes: 4 additions & 1 deletion src/noqa.rs
Expand Up @@ -49,7 +49,10 @@ pub fn extract_noqa_line_for(lxr: &[LexResult]) -> Vec<usize> {
if matches!(tok, Tok::EndOfFile) {
break;
}
if matches!(tok, Tok::String { .. }) {
// For multi-line strings, we expect `noqa` directives on the last line of the string.
// By definition, we can't have multiple multi-line strings on the same line, so we don't
// need to verify that we haven't already traversed past the current line.
if matches!(tok, Tok::String { .. }) && end.row() > start.row() {
for i in (noqa_line_for.len())..(start.row() - 1) {
noqa_line_for.push(i + 1);
}
Expand Down
22 changes: 22 additions & 0 deletions src/snapshots/ruff__linter__tests__E501_E501.py.snap
Expand Up @@ -24,4 +24,26 @@ expression: checks
row: 25
column: 127
fix: ~
- kind:
LineTooLong:
- 132
- 88
location:
row: 40
column: 0
end_location:
row: 40
column: 132
fix: ~
- kind:
LineTooLong:
- 105
- 88
location:
row: 43
column: 0
end_location:
row: 43
column: 105
fix: ~

0 comments on commit f0dfce9

Please sign in to comment.