mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-19 01:50:38 +00:00
Use Jupyter
mode while parsing Notebook files (#5552)
## Summary Enable using the new `Mode::Jupyter` for the tokenizer/parser to parse Jupyter line magic tokens. The individual call to the lexer i.e., `lex_starts_at` done by various rules should consider the context of the source code (is this content from a Jupyter Notebook?). Thus, a new field `source_type` (of type `PySourceType`) is added to `Checker` which is being passed around as an argument to the relevant functions. This is then used to determine the `Mode` for the lexer. ## Test Plan Add new test cases to make sure that the magic statement is considered while generating the diagnostic and autofix: * For `I001`, if there's a magic statement in between two import blocks, they should be sorted independently fixes: #6090
This commit is contained in:
parent
d788957ec4
commit
32fa05765a
52 changed files with 652 additions and 196 deletions
|
@ -25,6 +25,7 @@ use ruff_python_ast::PySourceType;
|
|||
use ruff_python_codegen::Stylist;
|
||||
use ruff_python_formatter::{format_module, format_node, PyFormatOptions};
|
||||
use ruff_python_index::{CommentRangesBuilder, Indexer};
|
||||
use ruff_python_parser::AsMode;
|
||||
use ruff_source_file::{Locator, SourceLocation};
|
||||
|
||||
#[wasm_bindgen(typescript_custom_section)]
|
||||
|
@ -197,8 +198,10 @@ impl Workspace {
|
|||
}
|
||||
|
||||
pub fn check(&self, contents: &str) -> Result<JsValue, Error> {
|
||||
let source_type = PySourceType::default();
|
||||
|
||||
// Tokenize once.
|
||||
let tokens: Vec<LexResult> = ruff_python_parser::tokenize(contents);
|
||||
let tokens: Vec<LexResult> = ruff_python_parser::tokenize(contents, source_type.as_mode());
|
||||
|
||||
// Map row and column locations to byte slices (lazily).
|
||||
let locator = Locator::new(contents);
|
||||
|
@ -228,6 +231,7 @@ impl Workspace {
|
|||
&self.settings,
|
||||
flags::Noqa::Enabled,
|
||||
None,
|
||||
source_type,
|
||||
);
|
||||
|
||||
let source_code = locator.to_source_code();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue