Skip to content

Commit aef403e

Browse files
committed
fix: #94 - More JSX space fixes.
1 parent 2a55b5f commit aef403e

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/parsing/node_helpers.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use swc_ast_view::*;
22

33
pub fn is_first_node_on_line(node: &dyn Spanned, module: &Module) -> bool {
44
let start = node.lo().0 as usize;
5-
let module_text = module.text().as_bytes();
5+
let source_file = module.source_file.as_ref().unwrap();
6+
let source_file_text = source_file.src.as_bytes();
67

78
for i in (0..start).rev() {
8-
let c = module_text[i];
9+
let c = source_file_text[i];
910
if c != ' ' as u8 && c != '\t' as u8 {
1011
return c == '\n' as u8;
1112
}
@@ -67,7 +68,8 @@ pub fn nodes_have_only_spaces_between(previous_node: &Node, next_node: &Node, mo
6768
crate::utils::has_no_new_lines_in_leading_whitespace(next_node_text)
6869
&& next_node_text.chars().next() == Some(' ')
6970
} else {
70-
crate::utils::is_not_empty_and_only_spaces(&module.text()[previous_node.hi().0 as usize..next_node.lo().0 as usize])
71+
let source_file = module.source_file.as_ref().unwrap();
72+
crate::utils::is_not_empty_and_only_spaces(&source_file.src[previous_node.hi().0 as usize..next_node.lo().0 as usize])
7173
}
7274
}
7375

tests/specs/issues/issue0094.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
-- fileName: test.jsx --
2+
~~ lineWidth: 80 ~~
23
== should format block comments in jsx expr container ==
34
const render = items => (
45
<div>{ /* eslint-disable */
@@ -24,3 +25,20 @@ const render = items => (
2425
[expect]
2526
<div {/* c */ ...a} />;
2627
<div {...a /* c */} />;
28+
29+
== should not panic and put space in correct spot ==
30+
// Test
31+
32+
// Test
33+
x = <div>
34+
<first>f</first> <first>f</first> <first>f</first> <first>f</first> <first>f</first> <first>f</first>
35+
</div>;
36+
37+
[expect]
38+
// Test
39+
40+
// Test
41+
x = <div>
42+
<first>f</first> <first>f</first> <first>f</first> <first>f</first>{" "}
43+
<first>f</first> <first>f</first>
44+
</div>;

0 commit comments

Comments
 (0)