Skip to content

Commit

Permalink
fix(linter): get correct source offsets for astro files (#6196)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
camchenry committed Oct 1, 2024
1 parent f280066 commit f6a3450
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions crates/oxc_linter/src/loader/partial_loader/astro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ impl<'a> AstroPartialLoader<'a> {
return None;
};

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

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

#[test]
Expand All @@ -140,7 +142,9 @@ mod test {
sources[0].source_text.trim(),
"const { message = 'Welcome, world!' } = Astro.props;"
);
assert_eq!(sources[0].start, 12);
assert_eq!(sources[1].source_text.trim(), r#"console.log("Hi");"#);
assert_eq!(sources[1].start, 141);
}

#[test]
Expand All @@ -158,7 +162,9 @@ mod test {
let sources = parse_astro(source_text);
assert_eq!(sources.len(), 2);
assert!(sources[0].source_text.is_empty());
assert_eq!(sources[0].start, 102);
assert_eq!(sources[1].source_text.trim(), r#"console.log("Hi");"#);
assert_eq!(sources[1].start, 129);
}

#[test]
Expand All @@ -176,6 +182,8 @@ mod test {
let sources = parse_astro(source_text);
assert_eq!(sources.len(), 2);
assert!(sources[0].source_text.is_empty());
assert_eq!(sources[0].start, 104);
assert_eq!(sources[1].source_text.trim(), r#"console.log("Hi");"#);
assert_eq!(sources[1].start, 122);
}
}

0 comments on commit f6a3450

Please sign in to comment.