Experiment with Located trait

This commit is contained in:
Micha Reiser
2024-10-26 12:26:18 +02:00
parent 6aaf1d9446
commit ca4c006f7d
84 changed files with 926 additions and 599 deletions

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(())
}