Remove parser dependency from ruff-python-ast (#6096)

This commit is contained in:
Micha Reiser 2023-07-26 17:47:22 +02:00 committed by GitHub
parent 99127243f4
commit 2cf00fee96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
658 changed files with 1714 additions and 1546 deletions

View file

@ -1,15 +1,15 @@
use std::cmp::Ordering;
use ruff_text_size::TextRange;
use rustpython_ast::{Expr, ExprIfExp, ExprSlice, Ranged};
use rustpython_parser::ast;
use rustpython_parser::ast::{Expr, ExprIfExp, ExprSlice, Ranged};
use ruff_python_ast::node::{AnyNodeRef, AstNode};
use ruff_python_ast::source_code::Locator;
use ruff_python_ast::whitespace;
use ruff_python_trivia::{
PythonWhitespace, SimpleToken, SimpleTokenKind, SimpleTokenizer, UniversalNewlines,
indentation_at_offset, PythonWhitespace, SimpleToken, SimpleTokenKind, SimpleTokenizer,
};
use ruff_source_file::{Locator, UniversalNewlines};
use crate::comments::visitor::{CommentPlacement, DecoratedComment};
use crate::expression::expr_slice::{assign_comment_in_slice, ExprSliceCommentSection};
@ -89,10 +89,9 @@ fn handle_match_comment<'a>(
let next_case = match_stmt.cases.get(current_case_index + 1);
let comment_indentation =
whitespace::indentation_at_offset(locator, comment.slice().range().start())
.unwrap_or_default()
.len();
let comment_indentation = indentation_at_offset(locator, comment.slice().range().start())
.unwrap_or_default()
.len();
let match_case_indentation = whitespace::indentation(locator, match_case).unwrap().len();
if let Some(next_case) = next_case {
@ -174,10 +173,9 @@ fn handle_in_between_except_handlers_or_except_handler_and_else_or_finally_comme
// it now depends on the indentation level of the comment if it is a leading comment for e.g.
// the following `finally` or indeed a trailing comment of the previous body's last statement.
let comment_indentation =
whitespace::indentation_at_offset(locator, comment.slice().range().start())
.unwrap_or_default()
.len();
let comment_indentation = indentation_at_offset(locator, comment.slice().range().start())
.unwrap_or_default()
.len();
let Some(except_indentation) =
whitespace::indentation(locator, preceding_except_handler).map(str::len)
@ -269,10 +267,9 @@ fn handle_in_between_bodies_own_line_comment<'a>(
// it now depends on the indentation level of the comment if it is a leading comment for e.g.
// the following `elif` or indeed a trailing comment of the previous body's last statement.
let comment_indentation =
whitespace::indentation_at_offset(locator, comment.slice().range().start())
.unwrap_or_default()
.len();
let comment_indentation = indentation_at_offset(locator, comment.slice().range().start())
.unwrap_or_default()
.len();
let Some(preceding_indentation) = whitespace::indentation(locator, &preceding).map(str::len)
else {
@ -445,10 +442,9 @@ fn own_line_comment_after_branch<'a>(
// We only care about the length because indentations with mixed spaces and tabs are only valid if
// the indent-level doesn't depend on the tab width (the indent level must be the same if the tab width is 1 or 8).
let comment_indentation =
whitespace::indentation_at_offset(locator, comment.slice().range().start())
.unwrap_or_default()
.len();
let comment_indentation = indentation_at_offset(locator, comment.slice().range().start())
.unwrap_or_default()
.len();
// Keep the comment on the entire statement in case it's a trailing comment
// ```python
@ -459,10 +455,9 @@ fn own_line_comment_after_branch<'a>(
// # Trailing if comment
// ```
// Here we keep the comment a trailing comment of the `if`
let preceding_node_indentation =
whitespace::indentation_at_offset(locator, preceding_node.start())
.unwrap_or_default()
.len();
let preceding_node_indentation = indentation_at_offset(locator, preceding_node.start())
.unwrap_or_default()
.len();
if comment_indentation == preceding_node_indentation {
return CommentPlacement::Default(comment);
}