mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-25 03:22:52 +00:00
Improve API exposed on ExprStringLiteral
nodes (#16192)
## Summary This PR makes the following changes: - It adjusts various callsites to use the new `ast::StringLiteral::contents_range()` method that was introduced in https://github.com/astral-sh/ruff/pull/16183. This is less verbose and more type-safe than using the `ast::str::raw_contents()` helper function. - It adds a new `ast::ExprStringLiteral::as_unconcatenated_literal()` helper method, and adjusts various callsites to use it. This addresses @MichaReiser's review comment at https://github.com/astral-sh/ruff/pull/16183#discussion_r1957334365. There is no functional change here, but it helps readability to make it clearer that we're differentiating between implicitly concatenated strings and unconcatenated strings at various points. - It renames the `StringLiteralValue::flags()` method to `StringLiteralFlags::first_literal_flags()`. If you're dealing with an implicitly concatenated string `string_node`, `string_node.value.flags().closer_len()` could give an incorrect result; this renaming makes it clearer that the `StringLiteralFlags` instance returned by the method is only guaranteed to give accurate information for the first `StringLiteral` contained in the `ExprStringLiteral` node. - It deletes the unused `BytesLiteralValue::flags()` method. This seems prone to misuse in the same way as `StringLiteralValue::flags()`: if it's an implicitly concatenated bytestring, the `BytesLiteralFlags` instance returned by the method would only give accurate information for the first `BytesLiteral` in the bytestring. ## Test Plan `cargo test`
This commit is contained in:
parent
21999b3be7
commit
b6b1947010
8 changed files with 37 additions and 43 deletions
|
@ -28,9 +28,7 @@ impl FormatRuleWithOptions<ExprStringLiteral, PyFormatContext<'_>> for FormatExp
|
|||
|
||||
impl FormatNodeRule<ExprStringLiteral> for FormatExprStringLiteral {
|
||||
fn fmt_fields(&self, item: &ExprStringLiteral, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let ExprStringLiteral { value, .. } = item;
|
||||
|
||||
if let [string_literal] = value.as_slice() {
|
||||
if let Some(string_literal) = item.as_unconcatenated_literal() {
|
||||
string_literal.format().with_options(self.kind).fmt(f)
|
||||
} else {
|
||||
// Always join strings that aren't parenthesized and thus, always on a single line.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue