Make Locator available in AST-to-CST conversion pass (#3194)

This commit is contained in:
Charlie Marsh 2023-02-23 19:43:03 -05:00 committed by GitHub
parent 198b301baf
commit eb15371453
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 316 additions and 136 deletions

View file

@ -1,7 +1,8 @@
use anyhow::Result;
use ruff_formatter::{format, Formatted, IndentStyle, SimpleFormatOptions};
use rustpython_parser::lexer::LexResult;
use ruff_formatter::{format, Formatted, IndentStyle, SimpleFormatOptions};
use crate::attachment::attach;
use crate::context::ASTFormatContext;
use crate::core::locator::Locator;
@ -35,7 +36,10 @@ pub fn fmt(contents: &str) -> Result<Formatted<ASTFormatContext>> {
let python_ast = ruff_rustpython::parse_program_tokens(tokens, "<filename>")?;
// Convert to a CST.
let mut python_cst: Vec<Stmt> = python_ast.into_iter().map(Into::into).collect();
let mut python_cst: Vec<Stmt> = python_ast
.into_iter()
.map(|stmt| (stmt, &locator).into())
.collect();
// Attach trivia.
attach(&mut python_cst, trivia);
@ -57,15 +61,16 @@ pub fn fmt(contents: &str) -> Result<Formatted<ASTFormatContext>> {
#[cfg(test)]
mod tests {
use std::fmt::{Formatter, Write};
use std::fs;
use std::path::Path;
use anyhow::Result;
use similar::TextDiff;
use ruff_testing_macros::fixture;
use crate::fmt;
use ruff_testing_macros::fixture;
use similar::TextDiff;
use std::fmt::{Formatter, Write};
#[fixture(
pattern = "resources/test/fixtures/black/**/*.py",