Skip to content

Commit f6a3450

Browse files
committedOct 1, 2024·
fix(linter): get correct source offsets for astro files (#6196)
Previously, we reported the `source_text` of an Astro file correctly, but the `start` offset was incorrectly set to before the `---` of the frontmatter. The start value now correctly points to the end of the split.
1 parent f280066 commit f6a3450

File tree

1 file changed

+10
-2
lines changed
  • crates/oxc_linter/src/loader/partial_loader

1 file changed

+10
-2
lines changed
 

‎crates/oxc_linter/src/loader/partial_loader/astro.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ impl<'a> AstroPartialLoader<'a> {
4242
return None;
4343
};
4444

45-
let js_code =
46-
Span::new(start + ASTRO_SPLIT.len() as u32, end).source_text(self.source_text);
45+
// move start to the end of the ASTRO_SPLIT
46+
let start = start + ASTRO_SPLIT.len() as u32;
47+
let js_code = Span::new(start, end).source_text(self.source_text);
4748
Some(JavaScriptSource::partial(js_code, SourceType::ts(), start))
4849
}
4950

@@ -118,6 +119,7 @@ mod test {
118119
let sources = parse_astro(source_text);
119120
assert_eq!(sources.len(), 1);
120121
assert_eq!(sources[0].source_text.trim(), r#"console.log("Hi");"#);
122+
assert_eq!(sources[0].start, 51);
121123
}
122124

123125
#[test]
@@ -140,7 +142,9 @@ mod test {
140142
sources[0].source_text.trim(),
141143
"const { message = 'Welcome, world!' } = Astro.props;"
142144
);
145+
assert_eq!(sources[0].start, 12);
143146
assert_eq!(sources[1].source_text.trim(), r#"console.log("Hi");"#);
147+
assert_eq!(sources[1].start, 141);
144148
}
145149

146150
#[test]
@@ -158,7 +162,9 @@ mod test {
158162
let sources = parse_astro(source_text);
159163
assert_eq!(sources.len(), 2);
160164
assert!(sources[0].source_text.is_empty());
165+
assert_eq!(sources[0].start, 102);
161166
assert_eq!(sources[1].source_text.trim(), r#"console.log("Hi");"#);
167+
assert_eq!(sources[1].start, 129);
162168
}
163169

164170
#[test]
@@ -176,6 +182,8 @@ mod test {
176182
let sources = parse_astro(source_text);
177183
assert_eq!(sources.len(), 2);
178184
assert!(sources[0].source_text.is_empty());
185+
assert_eq!(sources[0].start, 104);
179186
assert_eq!(sources[1].source_text.trim(), r#"console.log("Hi");"#);
187+
assert_eq!(sources[1].start, 122);
180188
}
181189
}

0 commit comments

Comments
 (0)
Please sign in to comment.