Skip to content

Commit

Permalink
cleanup; add some 'extreme' tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Feb 4, 2024
1 parent 3ab6494 commit fd85029
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
17 changes: 17 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF022.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,23 @@
,
)

__all__ = ( # comment about the opening paren
# multiline strange comment 0a
# multiline strange comment 0b
"foo" # inline comment about foo
# multiline strange comment 1a
# multiline strange comment 1b
, # comment about the comma??
# comment about bar part a
# comment about bar part b
"bar" # inline comment about bar
# strange multiline comment comment 2a
# strange multiline comment 2b
,
# strange multiline comment 3a
# strange multiline comment 3b
) # comment about the closing paren

###################################
# These should all not get flagged:
###################################
Expand Down
4 changes: 4 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF023.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ class BezierBuilder4:
,
)

__slots__ = {"foo", "bar",
"baz", "bingo"
}

###################################
# These should all not get flagged:
###################################
Expand Down
31 changes: 19 additions & 12 deletions crates/ruff_linter/src/rules/ruff/rules/sequence_sorting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,24 @@ fn multiline_string_sequence_postlude<'a>(
};
let postlude = locator.slice(TextRange::new(postlude_start, dunder_all_range_end));

// If the postlude consists solely of a closing parenthesis
// (not preceded by any whitespace/newlines),
// plus possibly a single trailing comma prior to the parenthesis,
// fixup the postlude so that the parenthesis appears on its own line,
// and so that the final item has a trailing comma.
// This produces formatting more similar
// to that which the formatter would produce.
let parenthesis_re = regex::Regex::new(r"^,?([\])}])$").unwrap();
if let Some(captures) = parenthesis_re.captures(postlude) {
let closing_paren = &captures[1];
return Cow::Owned(format!(",{newline}{leading_indent}{closing_paren}"));
}

let newline_chars = ['\r', '\n'];
if !postlude.starts_with(newline_chars) {
return Cow::Borrowed(postlude);
}

// The rest of this function uses heuristics to
// avoid very long indents for the closing paren
// that don't match the style for the rest of the
Expand All @@ -920,25 +938,14 @@ fn multiline_string_sequence_postlude<'a>(
// "y",
// ]
// ```
let newline_chars = ['\r', '\n'];
if !postlude.starts_with(newline_chars) {
// Special-case a few common situations so we can get formatting for the closing
// parenthesis that is similar to what the formatter would produce
let parenthesis_re = regex::Regex::new(r"^,?([\])}])$").unwrap();
if let Some(captures) = parenthesis_re.captures(postlude) {
let closing_paren = &captures[1];
return Cow::Owned(format!(",{newline}{leading_indent}{closing_paren}"));
}
return Cow::Borrowed(postlude);
}
if TextSize::of(leading_indentation(
postlude.trim_start_matches(newline_chars),
)) <= TextSize::of(item_indent)
{
return Cow::Borrowed(postlude);
}
let trimmed_postlude = postlude.trim_start();
if trimmed_postlude.starts_with([']', ')']) {
if trimmed_postlude.starts_with([']', ')', '}']) {
return Cow::Owned(format!("{newline}{leading_indent}{trimmed_postlude}"));
}
Cow::Borrowed(postlude)
Expand Down

0 comments on commit fd85029

Please sign in to comment.