mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-11 06:08:34 +00:00

## 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.
142 lines
3.6 KiB
Rust
142 lines
3.6 KiB
Rust
use ruff_python_parser::{parse_unchecked, Mode, ParseOptions};
|
|
use ruff_python_trivia::CommentRanges;
|
|
use ruff_text_size::TextSize;
|
|
|
|
#[test]
|
|
fn block_comments_two_line_block_at_start() {
|
|
// arrange
|
|
let source = "# line 1\n# line 2\n";
|
|
let parsed = parse_unchecked(source, ParseOptions::from(Mode::Module));
|
|
let comment_ranges = CommentRanges::from(parsed.tokens());
|
|
|
|
// act
|
|
let block_comments = comment_ranges.block_comments(source);
|
|
|
|
// assert
|
|
assert_eq!(block_comments, vec![TextSize::new(0), TextSize::new(9)]);
|
|
}
|
|
|
|
#[test]
|
|
fn block_comments_indented_block() {
|
|
// arrange
|
|
let source = " # line 1\n # line 2\n";
|
|
let parsed = parse_unchecked(source, ParseOptions::from(Mode::Module));
|
|
let comment_ranges = CommentRanges::from(parsed.tokens());
|
|
|
|
// act
|
|
let block_comments = comment_ranges.block_comments(source);
|
|
|
|
// assert
|
|
assert_eq!(block_comments, vec![TextSize::new(4), TextSize::new(17)]);
|
|
}
|
|
|
|
#[test]
|
|
fn block_comments_single_line_is_not_a_block() {
|
|
// arrange
|
|
let source = "\n";
|
|
let parsed = parse_unchecked(source, ParseOptions::from(Mode::Module));
|
|
let comment_ranges = CommentRanges::from(parsed.tokens());
|
|
|
|
// act
|
|
let block_comments = comment_ranges.block_comments(source);
|
|
|
|
// assert
|
|
assert_eq!(block_comments, Vec::<TextSize>::new());
|
|
}
|
|
|
|
#[test]
|
|
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, ParseOptions::from(Mode::Module));
|
|
let comment_ranges = CommentRanges::from(parsed.tokens());
|
|
|
|
// act
|
|
let block_comments = comment_ranges.block_comments(source);
|
|
|
|
// assert
|
|
assert_eq!(block_comments, Vec::<TextSize>::new());
|
|
}
|
|
|
|
#[test]
|
|
fn block_comments_sequential_lines_not_in_block() {
|
|
// arrange
|
|
let source = " # line 1\n # line 2\n";
|
|
let parsed = parse_unchecked(source, ParseOptions::from(Mode::Module));
|
|
let comment_ranges = CommentRanges::from(parsed.tokens());
|
|
|
|
// act
|
|
let block_comments = comment_ranges.block_comments(source);
|
|
|
|
// assert
|
|
assert_eq!(block_comments, Vec::<TextSize>::new());
|
|
}
|
|
|
|
#[test]
|
|
fn block_comments_lines_in_triple_quotes_not_a_block() {
|
|
// arrange
|
|
let source = r#"
|
|
"""
|
|
# line 1
|
|
# line 2
|
|
"""
|
|
"#;
|
|
let parsed = parse_unchecked(source, ParseOptions::from(Mode::Module));
|
|
let comment_ranges = CommentRanges::from(parsed.tokens());
|
|
|
|
// act
|
|
let block_comments = comment_ranges.block_comments(source);
|
|
|
|
// assert
|
|
assert_eq!(block_comments, Vec::<TextSize>::new());
|
|
}
|
|
|
|
#[test]
|
|
fn block_comments_stress_test() {
|
|
// arrange
|
|
let source = r#"
|
|
# block comment 1 line 1
|
|
# block comment 2 line 2
|
|
|
|
# these lines
|
|
# do not form
|
|
# a block comment
|
|
|
|
x = 1 # these lines also do not
|
|
y = 2 # do not form a block comment
|
|
|
|
# these lines do form a block comment
|
|
#
|
|
|
|
#
|
|
# and so do these
|
|
#
|
|
|
|
"""
|
|
# these lines are in triple quotes and
|
|
# therefore do not form a block comment
|
|
"""
|
|
"#;
|
|
let parsed = parse_unchecked(source, ParseOptions::from(Mode::Module));
|
|
let comment_ranges = CommentRanges::from(parsed.tokens());
|
|
|
|
// act
|
|
let block_comments = comment_ranges.block_comments(source);
|
|
|
|
// assert
|
|
assert_eq!(
|
|
block_comments,
|
|
vec![
|
|
// Block #1
|
|
TextSize::new(1),
|
|
TextSize::new(26),
|
|
// Block #2
|
|
TextSize::new(174),
|
|
TextSize::new(212),
|
|
// Block #3
|
|
TextSize::new(219),
|
|
TextSize::new(225),
|
|
TextSize::new(247)
|
|
]
|
|
);
|
|
}
|