Replace dynamic implicit concatenation detection with parser flag (#6513)

## Summary

In https://github.com/astral-sh/ruff/pull/6512, we added a flag to the
AST to mark implicitly-concatenated string expressions. This PR makes
use of that flag to remove the `is_implicit_concatenation` method.

## Test Plan

`cargo test`
This commit is contained in:
Charlie Marsh 2023-08-14 10:27:17 -04:00 committed by GitHub
parent 40407dcce5
commit a7cf8f0b77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 52 additions and 150 deletions

View file

@ -1,10 +1,8 @@
use ruff_formatter::{write, FormatOwnedWithRule, FormatRefWithRule, FormatRuleWithOptions};
use ruff_python_ast::helpers::is_compound_statement;
use ruff_python_ast::str::is_implicit_concatenation;
use ruff_python_ast::{self as ast, Expr, Ranged, Stmt, Suite};
use ruff_python_ast::{self as ast, Ranged, Stmt, Suite};
use ruff_python_ast::{Constant, ExprConstant};
use ruff_python_trivia::{lines_after_ignoring_trivia, lines_before};
use ruff_source_file::Locator;
use crate::comments::{leading_comments, trailing_comments};
use crate::context::{NodeLevel, WithNodeLevel};
@ -78,7 +76,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
write!(f, [first.format()])?;
}
SuiteKind::Function => {
if let Some(constant) = get_docstring(first, &f.context().locator()) {
if let Some(constant) = get_docstring(first) {
write!(
f,
[
@ -95,7 +93,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> for FormatSuite {
}
}
SuiteKind::Class => {
if let Some(constant) = get_docstring(first, &f.context().locator()) {
if let Some(constant) = get_docstring(first) {
if !comments.has_leading_comments(first)
&& lines_before(first.start(), source) > 1
{
@ -257,26 +255,20 @@ const fn is_import_definition(stmt: &Stmt) -> bool {
}
/// Checks if the statement is a simple string that can be formatted as a docstring
fn get_docstring<'a>(stmt: &'a Stmt, locator: &Locator) -> Option<&'a ExprConstant> {
let Stmt::Expr(ast::StmtExpr { value, .. }) = stmt else {
return None;
};
let Expr::Constant(constant) = value.as_ref() else {
return None;
};
if let ExprConstant {
value: Constant::Str(..),
range,
..
} = constant
{
if is_implicit_concatenation(locator.slice(*range)) {
return None;
}
return Some(constant);
fn get_docstring(stmt: &Stmt) -> Option<&ExprConstant> {
let stmt_expr = stmt.as_expr_stmt()?;
let expr_constant = stmt_expr.value.as_constant_expr()?;
if matches!(
expr_constant.value,
Constant::Str(ast::StringConstant {
implicit_concatenated: false,
..
})
) {
Some(expr_constant)
} else {
None
}
None
}
impl FormatRuleWithOptions<Suite, PyFormatContext<'_>> for FormatSuite {