From a6595b58924b3c29256313a6212d28946ffb83cc Mon Sep 17 00:00:00 2001 From: Ada Alakbarova Date: Mon, 28 Jul 2025 07:45:32 +0200 Subject: [PATCH 1/5] extract `source` into a variable --- src/ast.rs | 72 +++++++++++++++++++++--------------------------------- 1 file changed, 28 insertions(+), 44 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 6688b05..583ea0a 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -1406,10 +1406,8 @@ mod tests { #[test] fn preceding_indentation_shift_tabs() { let ctx = ctx(); - let tree = ctx.parse( - "a.java", - "class Outer {\n\tclass MyCls {\n\t\tint attr;\n\t}\n}\n", - ); + let source = "class Outer {\n\tclass MyCls {\n\t\tint attr;\n\t}\n}\n"; + let tree = ctx.parse("a.java", source); let class_decl = tree[0][2][1]; let class_body = class_decl[2]; let attr = class_body[1]; @@ -1420,10 +1418,8 @@ mod tests { #[test] fn preceding_indentation_shift_mixed_spaces_and_tabs() { let ctx = ctx(); - let tree = ctx.parse( - "a.java", - "class Outer {\n\tclass MyCls {\n int attr;\n\t}\n}\n", - ); + let source = "class Outer {\n\tclass MyCls {\n int attr;\n\t}\n}\n"; + let tree = ctx.parse("a.java", source); let class_decl = tree[0][2][1]; let class_body = class_decl[2]; let attr = class_body[1]; @@ -1434,10 +1430,8 @@ mod tests { #[test] fn preceding_indentation_shift_mixed_tabs_and_spaces() { let ctx = ctx(); - let tree = ctx.parse( - "a.java", - "class Outer {\n class MyCls {\n\t\tint attr;\n }\n}\n", - ); + let source = "class Outer {\n class MyCls {\n\t\tint attr;\n }\n}\n"; + let tree = ctx.parse("a.java", source); let class_decl = tree[0][2][1]; let class_body = class_decl[2]; let attr = class_body[1]; @@ -1479,9 +1473,7 @@ mod tests { fn removing_indentation() { let ctx = ctx(); - let tree = ctx.parse( - "a.json", - r#" + let source = r#" { "a": [ 1, @@ -1491,8 +1483,8 @@ mod tests { "c": "foo" } } -"#, - ); +"#; + let tree = ctx.parse("a.json", source); let root = &tree[0]; let entry_a = &root[1]; @@ -1518,14 +1510,10 @@ mod tests { fn multiline_comments_are_isomorphic() { let ctx = ctx(); - let comment_1 = ctx.parse( - "a.java", - "/**\n * This is a comment\n * spanning on many lines\n*/", - )[0]; - let comment_2 = ctx.parse( - "a.java", - " /**\n * This is a comment\n * spanning on many lines\n */", - )[0]; + let source_1 = "/**\n * This is a comment\n * spanning on many lines\n*/"; + let comment_1 = ctx.parse("a.java", source_1)[0]; + let source_2 = " /**\n * This is a comment\n * spanning on many lines\n */"; + let comment_2 = ctx.parse("a.java", source_2)[0]; assert!(comment_1.isomorphic_to(comment_2)); assert_eq!(comment_1.children.len(), 4); @@ -1705,10 +1693,8 @@ line 3";"#; #[test] fn parse_html_with_js() { let ctx = ctx(); - let html = ctx.parse( - "a.html", - "", - ); + let source = ""; + let html = ctx.parse("a.html", source); assert_eq!(html.grammar_name, "document"); assert_eq!(html.lang_profile.name, "HTML"); @@ -1725,10 +1711,8 @@ line 3";"#; #[test] fn parse_injection_with_syntax_error() { let ctx = ctx(); - let html = ctx.parse( - "a.html", - "", - ); + let source = ""; + let html = ctx.parse("a.html", source); assert_eq!(html.grammar_name, "document"); assert_eq!(html.lang_profile.name, "HTML"); @@ -1767,7 +1751,11 @@ A list: #[test] fn commutative_parent_via_query() { let ctx = ctx(); - let python = ctx.parse("a.py", "__all__ = [ 'foo', 'bar' ]\nother = [ 1, 2 ]\n"); + let source = "\ +__all__ = [ 'foo', 'bar' ] +other = [ 1, 2 ] +"; + let python = ctx.parse("a.py", source); let first_list = python[0][0][2]; let second_list = python[1][0][2]; @@ -1783,13 +1771,11 @@ A list: #[test] fn flatten_binary_operators() { let ctx = ctx(); - let ts = ctx.parse( - "a.ts", - r#"interface MyInterface { + let source = r#"interface MyInterface { level: 'debug' | 'info' | 'warn' | 'error'; } -"#, - ); +"#; + let ts = ctx.parse("a.ts", source); let union_type = ts[0][2][1][1][1]; assert_eq!(union_type.grammar_name, "union_type"); assert_eq!(union_type.children.len(), 7); @@ -1802,12 +1788,10 @@ A list: #[test] fn dont_flatten_different_operators_together() { let ctx = ctx(); - let ts = ctx.parse( - "a.ts", - r#"interface Foo { + let source = r#"interface Foo { field: 'first' | 'second' & 'third', -}"#, - ); +}"#; + let ts = ctx.parse("a.ts", source); let union_type = ts[0][2][1][1][1]; assert_eq!(union_type.grammar_name, "union_type"); assert_eq!(union_type.children.len(), 3); -- 2.47.3 From 71486e8afe29afcd12f40f2cccfe98589633f582 Mon Sep 17 00:00:00 2001 From: Ada Alakbarova Date: Mon, 28 Jul 2025 07:45:57 +0200 Subject: [PATCH 2/5] misc: rm needless raw string --- src/ast.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 583ea0a..ef3b445 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -1733,11 +1733,11 @@ line 3";"#; // the child ends up falling outside of the new computed range, // which violates the assumption that the ranges of all children // fall into the range of their parent. - let source = r#" + let source = "\ A list: - Hello -"#; +"; let markdown = ctx.parse("a.md", source); let paragraph = markdown[0][1][0][1]; @@ -1771,10 +1771,11 @@ other = [ 1, 2 ] #[test] fn flatten_binary_operators() { let ctx = ctx(); - let source = r#"interface MyInterface { + let source = "\ +interface MyInterface { level: 'debug' | 'info' | 'warn' | 'error'; } -"#; +"; let ts = ctx.parse("a.ts", source); let union_type = ts[0][2][1][1][1]; assert_eq!(union_type.grammar_name, "union_type"); @@ -1788,9 +1789,10 @@ other = [ 1, 2 ] #[test] fn dont_flatten_different_operators_together() { let ctx = ctx(); - let source = r#"interface Foo { + let source = "\ +interface Foo { field: 'first' | 'second' & 'third', -}"#; +}"; let ts = ctx.parse("a.ts", source); let union_type = ts[0][2][1][1][1]; assert_eq!(union_type.grammar_name, "union_type"); -- 2.47.3 From ee0e99b06f8a0436eaa69dccb7a46b58e151c4ba Mon Sep 17 00:00:00 2001 From: Ada Alakbarova Date: Mon, 28 Jul 2025 08:02:00 +0200 Subject: [PATCH 3/5] removing_indentation: make `expected`s multiline helps visualize the exact indentation we want --- src/ast.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 6 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index ef3b445..51811d0 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -1490,20 +1490,59 @@ mod tests { let entry_a = &root[1]; let array = &entry_a[2]; - assert_eq!(entry_a.source, "\"a\": [\n 1,\n 2,\n ]"); + assert_eq!( + entry_a.source, + "\ +\"a\": [ + 1, + 2, + ]" + ); assert_eq!(entry_a.indentation_shift(), Some(" ")); assert_eq!(entry_a.ancestor_indentation(), None); - assert_eq!(entry_a.unindented_source(), "\"a\": [\n 1,\n 2,\n]"); + assert_eq!( + entry_a.unindented_source(), + "\ +\"a\": [ + 1, + 2, +]" + ); assert_eq!( entry_a.reindented_source(" "), - "\"a\": [\n 1,\n 2,\n ]" + "\ +\"a\": [ + 1, + 2, + ]" ); - assert_eq!(array.source, "[\n 1,\n 2,\n ]"); + assert_eq!( + array.source, + "\ +[ + 1, + 2, + ]" + ); assert_eq!(array.indentation_shift(), None); assert_eq!(array.ancestor_indentation(), Some(" ")); - assert_eq!(array.unindented_source(), "[\n 1,\n 2,\n]"); - assert_eq!(array.reindented_source(" "), "[\n 1,\n 2,\n ]"); + assert_eq!( + array.unindented_source(), + "\ +[ + 1, + 2, +]" + ); + assert_eq!( + array.reindented_source(" "), + "\ +[ + 1, + 2, + ]" + ); } #[test] -- 2.47.3 From f1d7706e1698a2e97226980b6a54df916f0a9c90 Mon Sep 17 00:00:00 2001 From: Ada Alakbarova Date: Mon, 28 Jul 2025 08:11:43 +0200 Subject: [PATCH 4/5] multiline_comments_are_isomorphic: simplify `assert`s --- src/ast.rs | 37 +++++++++---------------------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 51811d0..ad47523 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -1556,32 +1556,14 @@ mod tests { assert!(comment_1.isomorphic_to(comment_2)); assert_eq!(comment_1.children.len(), 4); - assert_eq!( - comment_2 - .children - .iter() - .map(|child| child.source) - .collect_vec(), - vec![ - "/**", - "* This is a comment", - "* spanning on many lines", - "*/" - ] - ); - assert_eq!( - comment_2 - .children - .iter() - .map(|child| &child.byte_range) - .collect_vec(), - vec![ - &Range { start: 2, end: 5 }, - &Range { start: 9, end: 28 }, - &Range { start: 32, end: 56 }, - &Range { start: 59, end: 61 }, - ] - ); + assert_eq!(comment_2[0].source, "/**"); + assert_eq!(comment_2[1].source, "* This is a comment"); + assert_eq!(comment_2[2].source, "* spanning on many lines"); + assert_eq!(comment_2[3].source, "*/"); + assert_eq!(comment_2[0].byte_range, Range { start: 2, end: 5 },); + assert_eq!(comment_2[1].byte_range, Range { start: 9, end: 28 },); + assert_eq!(comment_2[2].byte_range, Range { start: 32, end: 56 },); + assert_eq!(comment_2[3].byte_range, Range { start: 59, end: 61 },); assert_eq!(comment_2[1].preceding_whitespace(), Some("\n ")); } @@ -1813,8 +1795,7 @@ other = [ 1, 2 ] let source = "\ interface MyInterface { level: 'debug' | 'info' | 'warn' | 'error'; -} -"; +}"; let ts = ctx.parse("a.ts", source); let union_type = ts[0][2][1][1][1]; assert_eq!(union_type.grammar_name, "union_type"); -- 2.47.3 From 06c4e9342d2232ec896e6213a53eb6acd977a1b0 Mon Sep 17 00:00:00 2001 From: Ada Alakbarova Date: Mon, 28 Jul 2025 08:13:18 +0200 Subject: [PATCH 5/5] multiline_comments_are_isomorphic: simplify `Range`s --- src/ast.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index ad47523..8fc7ef5 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -1560,10 +1560,10 @@ mod tests { assert_eq!(comment_2[1].source, "* This is a comment"); assert_eq!(comment_2[2].source, "* spanning on many lines"); assert_eq!(comment_2[3].source, "*/"); - assert_eq!(comment_2[0].byte_range, Range { start: 2, end: 5 },); - assert_eq!(comment_2[1].byte_range, Range { start: 9, end: 28 },); - assert_eq!(comment_2[2].byte_range, Range { start: 32, end: 56 },); - assert_eq!(comment_2[3].byte_range, Range { start: 59, end: 61 },); + assert_eq!(comment_2[0].byte_range, 2..5); + assert_eq!(comment_2[1].byte_range, 9..28); + assert_eq!(comment_2[2].byte_range, 32..56); + assert_eq!(comment_2[3].byte_range, 59..61); assert_eq!(comment_2[1].preceding_whitespace(), Some("\n ")); } -- 2.47.3