mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:10 +00:00
Pass ParserOptions
to the parser (#16220)
## Summary This is part of the preparation for detecting syntax errors in the parser from https://github.com/astral-sh/ruff/pull/16090/. As suggested in [this comment](https://github.com/astral-sh/ruff/pull/16090/#discussion_r1953084509), I started working on a `ParseOptions` struct that could be stored in the parser. For this initial refactor, I only made it hold the existing `Mode` option, but for syntax errors, we will also need it to have a `PythonVersion`. For that use case, I'm picturing something like a `ParseOptions::with_python_version` method, so you can extend the current calls to something like ```rust ParseOptions::from(mode).with_python_version(settings.target_version) ``` But I thought it was worth adding `ParseOptions` alone without changing any other behavior first. Most of the diff is just updating call sites taking `Mode` to take `ParseOptions::from(Mode)` or those taking `PySourceType`s to take `ParseOptions::from(PySourceType)`. The interesting changes are in the new `parser/options.rs` file and smaller parts of `parser/mod.rs` and `ruff_python_parser/src/lib.rs`. ## Test Plan Existing tests, this should not change any behavior.
This commit is contained in:
parent
cfc6941d5c
commit
97d0659ce3
25 changed files with 148 additions and 93 deletions
|
@ -1,4 +1,4 @@
|
|||
use ruff_python_parser::{parse_unchecked, Mode};
|
||||
use ruff_python_parser::{parse_unchecked, Mode, ParseOptions};
|
||||
use ruff_python_trivia::CommentRanges;
|
||||
use ruff_text_size::TextSize;
|
||||
|
||||
|
@ -6,7 +6,7 @@ use ruff_text_size::TextSize;
|
|||
fn block_comments_two_line_block_at_start() {
|
||||
// arrange
|
||||
let source = "# line 1\n# line 2\n";
|
||||
let parsed = parse_unchecked(source, Mode::Module);
|
||||
let parsed = parse_unchecked(source, ParseOptions::from(Mode::Module));
|
||||
let comment_ranges = CommentRanges::from(parsed.tokens());
|
||||
|
||||
// act
|
||||
|
@ -20,7 +20,7 @@ fn block_comments_two_line_block_at_start() {
|
|||
fn block_comments_indented_block() {
|
||||
// arrange
|
||||
let source = " # line 1\n # line 2\n";
|
||||
let parsed = parse_unchecked(source, Mode::Module);
|
||||
let parsed = parse_unchecked(source, ParseOptions::from(Mode::Module));
|
||||
let comment_ranges = CommentRanges::from(parsed.tokens());
|
||||
|
||||
// act
|
||||
|
@ -34,7 +34,7 @@ fn block_comments_indented_block() {
|
|||
fn block_comments_single_line_is_not_a_block() {
|
||||
// arrange
|
||||
let source = "\n";
|
||||
let parsed = parse_unchecked(source, Mode::Module);
|
||||
let parsed = parse_unchecked(source, ParseOptions::from(Mode::Module));
|
||||
let comment_ranges = CommentRanges::from(parsed.tokens());
|
||||
|
||||
// act
|
||||
|
@ -48,7 +48,7 @@ fn block_comments_single_line_is_not_a_block() {
|
|||
fn block_comments_lines_with_code_not_a_block() {
|
||||
// arrange
|
||||
let source = "x = 1 # line 1\ny = 2 # line 2\n";
|
||||
let parsed = parse_unchecked(source, Mode::Module);
|
||||
let parsed = parse_unchecked(source, ParseOptions::from(Mode::Module));
|
||||
let comment_ranges = CommentRanges::from(parsed.tokens());
|
||||
|
||||
// act
|
||||
|
@ -62,7 +62,7 @@ fn block_comments_lines_with_code_not_a_block() {
|
|||
fn block_comments_sequential_lines_not_in_block() {
|
||||
// arrange
|
||||
let source = " # line 1\n # line 2\n";
|
||||
let parsed = parse_unchecked(source, Mode::Module);
|
||||
let parsed = parse_unchecked(source, ParseOptions::from(Mode::Module));
|
||||
let comment_ranges = CommentRanges::from(parsed.tokens());
|
||||
|
||||
// act
|
||||
|
@ -81,7 +81,7 @@ fn block_comments_lines_in_triple_quotes_not_a_block() {
|
|||
# line 2
|
||||
"""
|
||||
"#;
|
||||
let parsed = parse_unchecked(source, Mode::Module);
|
||||
let parsed = parse_unchecked(source, ParseOptions::from(Mode::Module));
|
||||
let comment_ranges = CommentRanges::from(parsed.tokens());
|
||||
|
||||
// act
|
||||
|
@ -117,7 +117,7 @@ y = 2 # do not form a block comment
|
|||
# therefore do not form a block comment
|
||||
"""
|
||||
"#;
|
||||
let parsed = parse_unchecked(source, Mode::Module);
|
||||
let parsed = parse_unchecked(source, ParseOptions::from(Mode::Module));
|
||||
let comment_ranges = CommentRanges::from(parsed.tokens());
|
||||
|
||||
// act
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use insta::assert_debug_snapshot;
|
||||
|
||||
use ruff_python_parser::{parse_unchecked, Mode};
|
||||
use ruff_python_parser::{parse_unchecked, Mode, ParseOptions};
|
||||
use ruff_python_trivia::{lines_after, lines_before, CommentRanges, SimpleToken, SimpleTokenizer};
|
||||
use ruff_python_trivia::{BackwardsTokenizer, SimpleTokenKind};
|
||||
use ruff_text_size::{TextLen, TextRange, TextSize};
|
||||
|
@ -22,7 +22,7 @@ impl TokenizationTestCase {
|
|||
}
|
||||
|
||||
fn tokenize_reverse(&self) -> Vec<SimpleToken> {
|
||||
let parsed = parse_unchecked(self.source, Mode::Module);
|
||||
let parsed = parse_unchecked(self.source, ParseOptions::from(Mode::Module));
|
||||
let comment_ranges = CommentRanges::from(parsed.tokens());
|
||||
BackwardsTokenizer::new(self.source, self.range, &comment_ranges).collect()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue