Extract LineIndex independent methods from Locator (#13938)
Some checks are pending
CI / Fuzz the parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz (push) Blocked by required conditions
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions

This commit is contained in:
Micha Reiser 2024-10-28 08:53:41 +01:00 committed by GitHub
parent f8eb547fb4
commit 9f3a38d408
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
171 changed files with 1348 additions and 1284 deletions

View file

@ -14,7 +14,6 @@ license.workspace = true
[dev-dependencies]
ruff_python_parser = { workspace = true }
ruff_python_trivia = { workspace = true }
ruff_source_file = { workspace = true }
ruff_text_size = { workspace = true }
insta = { workspace = true }

View file

@ -1,6 +1,5 @@
use ruff_python_parser::{parse_unchecked, Mode};
use ruff_python_trivia::CommentRanges;
use ruff_source_file::Locator;
use ruff_text_size::TextSize;
#[test]
@ -8,11 +7,10 @@ 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 locator = Locator::new(source);
let comment_ranges = CommentRanges::from(parsed.tokens());
// act
let block_comments = comment_ranges.block_comments(&locator);
let block_comments = comment_ranges.block_comments(source);
// assert
assert_eq!(block_comments, vec![TextSize::new(0), TextSize::new(9)]);
@ -23,11 +21,10 @@ fn block_comments_indented_block() {
// arrange
let source = " # line 1\n # line 2\n";
let parsed = parse_unchecked(source, Mode::Module);
let locator = Locator::new(source);
let comment_ranges = CommentRanges::from(parsed.tokens());
// act
let block_comments = comment_ranges.block_comments(&locator);
let block_comments = comment_ranges.block_comments(source);
// assert
assert_eq!(block_comments, vec![TextSize::new(4), TextSize::new(17)]);
@ -38,11 +35,10 @@ fn block_comments_single_line_is_not_a_block() {
// arrange
let source = "\n";
let parsed = parse_unchecked(source, Mode::Module);
let locator = Locator::new(source);
let comment_ranges = CommentRanges::from(parsed.tokens());
// act
let block_comments = comment_ranges.block_comments(&locator);
let block_comments = comment_ranges.block_comments(source);
// assert
assert_eq!(block_comments, Vec::<TextSize>::new());
@ -53,11 +49,10 @@ 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 locator = Locator::new(source);
let comment_ranges = CommentRanges::from(parsed.tokens());
// act
let block_comments = comment_ranges.block_comments(&locator);
let block_comments = comment_ranges.block_comments(source);
// assert
assert_eq!(block_comments, Vec::<TextSize>::new());
@ -68,11 +63,10 @@ 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 locator = Locator::new(source);
let comment_ranges = CommentRanges::from(parsed.tokens());
// act
let block_comments = comment_ranges.block_comments(&locator);
let block_comments = comment_ranges.block_comments(source);
// assert
assert_eq!(block_comments, Vec::<TextSize>::new());
@ -88,11 +82,10 @@ fn block_comments_lines_in_triple_quotes_not_a_block() {
"""
"#;
let parsed = parse_unchecked(source, Mode::Module);
let locator = Locator::new(source);
let comment_ranges = CommentRanges::from(parsed.tokens());
// act
let block_comments = comment_ranges.block_comments(&locator);
let block_comments = comment_ranges.block_comments(source);
// assert
assert_eq!(block_comments, Vec::<TextSize>::new());
@ -125,11 +118,10 @@ y = 2 # do not form a block comment
"""
"#;
let parsed = parse_unchecked(source, Mode::Module);
let locator = Locator::new(source);
let comment_ranges = CommentRanges::from(parsed.tokens());
// act
let block_comments = comment_ranges.block_comments(&locator);
let block_comments = comment_ranges.block_comments(source);
// assert
assert_eq!(

View file

@ -1,6 +1,5 @@
use ruff_python_parser::{parse_module, ParseError};
use ruff_python_trivia::has_trailing_content;
use ruff_source_file::Locator;
use ruff_text_size::Ranged;
#[test]
@ -8,26 +7,22 @@ fn trailing_content() -> Result<(), ParseError> {
let contents = "x = 1";
let suite = parse_module(contents)?.into_suite();
let stmt = suite.first().unwrap();
let locator = Locator::new(contents);
assert!(!has_trailing_content(stmt.end(), &locator));
assert!(!has_trailing_content(stmt.end(), contents));
let contents = "x = 1; y = 2";
let suite = parse_module(contents)?.into_suite();
let stmt = suite.first().unwrap();
let locator = Locator::new(contents);
assert!(has_trailing_content(stmt.end(), &locator));
assert!(has_trailing_content(stmt.end(), contents));
let contents = "x = 1 ";
let suite = parse_module(contents)?.into_suite();
let stmt = suite.first().unwrap();
let locator = Locator::new(contents);
assert!(!has_trailing_content(stmt.end(), &locator));
assert!(!has_trailing_content(stmt.end(), contents));
let contents = "x = 1 # Comment";
let suite = parse_module(contents)?.into_suite();
let stmt = suite.first().unwrap();
let locator = Locator::new(contents);
assert!(!has_trailing_content(stmt.end(), &locator));
assert!(!has_trailing_content(stmt.end(), contents));
let contents = r"
x = 1
@ -36,8 +31,7 @@ y = 2
.trim();
let suite = parse_module(contents)?.into_suite();
let stmt = suite.first().unwrap();
let locator = Locator::new(contents);
assert!(!has_trailing_content(stmt.end(), &locator));
assert!(!has_trailing_content(stmt.end(), contents));
Ok(())
}