mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 12:29:28 +00:00
Build CommentRanges
outside the parser (#11792)
## Summary This PR updates the parser to remove building the `CommentRanges` and instead it'll be built by the linter and the formatter when it's required. For the linter, it'll be built and owned by the `Indexer` while for the formatter it'll be built from the `Tokens` struct and passed as an argument. ## Test Plan `cargo insta test`
This commit is contained in:
parent
7509a48eab
commit
549cc1e437
28 changed files with 151 additions and 102 deletions
|
@ -1,6 +1,7 @@
|
|||
use std::path::Path;
|
||||
|
||||
use js_sys::Error;
|
||||
use ruff_python_trivia::CommentRanges;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
|
@ -172,8 +173,12 @@ impl Workspace {
|
|||
let indexer = Indexer::from_tokens(parsed.tokens(), &locator);
|
||||
|
||||
// Extract the `# noqa` and `# isort: skip` directives from the source.
|
||||
let directives =
|
||||
directives::extract_directives(&parsed, directives::Flags::empty(), &locator, &indexer);
|
||||
let directives = directives::extract_directives(
|
||||
parsed.tokens(),
|
||||
directives::Flags::empty(),
|
||||
&locator,
|
||||
&indexer,
|
||||
);
|
||||
|
||||
// Generate checks.
|
||||
let LinterResult {
|
||||
|
@ -241,11 +246,8 @@ impl Workspace {
|
|||
|
||||
pub fn comments(&self, contents: &str) -> Result<String, Error> {
|
||||
let parsed = ParsedModule::from_source(contents)?;
|
||||
let comments = pretty_comments(
|
||||
parsed.parsed.syntax(),
|
||||
parsed.parsed.comment_ranges(),
|
||||
contents,
|
||||
);
|
||||
let comment_ranges = CommentRanges::from(parsed.parsed.tokens());
|
||||
let comments = pretty_comments(parsed.parsed.syntax(), &comment_ranges, contents);
|
||||
Ok(comments)
|
||||
}
|
||||
|
||||
|
@ -270,13 +272,17 @@ pub(crate) fn into_error<E: std::fmt::Display>(err: E) -> Error {
|
|||
struct ParsedModule<'a> {
|
||||
source_code: &'a str,
|
||||
parsed: Parsed<Mod>,
|
||||
comment_ranges: CommentRanges,
|
||||
}
|
||||
|
||||
impl<'a> ParsedModule<'a> {
|
||||
fn from_source(source_code: &'a str) -> Result<Self, Error> {
|
||||
let parsed = parse(source_code, Mode::Module).map_err(into_error)?;
|
||||
let comment_ranges = CommentRanges::from(parsed.tokens());
|
||||
Ok(Self {
|
||||
source_code,
|
||||
parsed: parse(source_code, Mode::Module).map_err(into_error)?,
|
||||
parsed,
|
||||
comment_ranges,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -287,6 +293,11 @@ impl<'a> ParsedModule<'a> {
|
|||
.to_format_options(PySourceType::default(), self.source_code)
|
||||
.with_source_map_generation(SourceMapGeneration::Enabled);
|
||||
|
||||
format_module_ast(&self.parsed, self.source_code, options)
|
||||
format_module_ast(
|
||||
&self.parsed,
|
||||
&self.comment_ranges,
|
||||
self.source_code,
|
||||
options,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue