From 1cd5e7511ba8175ed07ed07859d78fb74e69e4f8 Mon Sep 17 00:00:00 2001
From: Kalven Schraut <30308012+kalvenschraut@users.noreply.github.com>
Date: Sun, 7 Apr 2024 00:00:55 -0500
Subject: [PATCH] fix(linter): svelte partial loader handle generics (#2875)
(#2906)
While looking into this, I knew vue would have similar problems. Which
then led me to realize the generic case was handled already so I tried
abstract out the function so both the vue and svelte partial loaders
could reuse the code. Finally added some svelte test cases to prove this
resolved the issue.
I am a bit new to rust so if there was a better way to reuse the
find_script_closing_angle function open to suggestions.
---
crates/oxc_linter/src/partial_loader/mod.rs | 21 ++++++++++
.../oxc_linter/src/partial_loader/svelte.rs | 40 ++++++++++++++++++-
crates/oxc_linter/src/partial_loader/vue.rs | 25 +-----------
3 files changed, 61 insertions(+), 25 deletions(-)
diff --git a/crates/oxc_linter/src/partial_loader/mod.rs b/crates/oxc_linter/src/partial_loader/mod.rs
index fdd7b9e5a..63d2debf7 100644
--- a/crates/oxc_linter/src/partial_loader/mod.rs
+++ b/crates/oxc_linter/src/partial_loader/mod.rs
@@ -40,3 +40,24 @@ impl PartialLoader {
}
}
}
+
+/// Find closing angle for situations where there is another `>` in between.
+/// e.g. `
+
Hello World
+ "#;
+
+ let result = parse_svelte(source_text);
+ assert_eq!(result.source_text.trim(), r#"console.log("hi");"#);
+ }
+
+ #[test]
+ fn test_parse_svelte_ts_with_generic() {
+ let source_text = r#"
+
+ Hello World
+ "#;
+
+ let result = parse_svelte(source_text);
+ assert_eq!(result.source_text.trim(), r#"console.log("hi");"#);
+ }
+}
diff --git a/crates/oxc_linter/src/partial_loader/vue.rs b/crates/oxc_linter/src/partial_loader/vue.rs
index 2fb593b22..d34361598 100644
--- a/crates/oxc_linter/src/partial_loader/vue.rs
+++ b/crates/oxc_linter/src/partial_loader/vue.rs
@@ -2,7 +2,7 @@ use memchr::memmem::Finder;
use oxc_span::SourceType;
-use super::{JavaScriptSource, SCRIPT_END, SCRIPT_START};
+use super::{find_script_closing_angle, JavaScriptSource, SCRIPT_END, SCRIPT_START};
pub struct VuePartialLoader<'a> {
source_text: &'a str,
@@ -37,7 +37,7 @@ impl<'a> VuePartialLoader<'a> {
*pointer += offset + SCRIPT_START.len();
// find closing ">"
- let offset = self.find_script_closing_angle(*pointer)?;
+ let offset = find_script_closing_angle(self.source_text, *pointer)?;
// get ts and jsx attribute
let content = &self.source_text[*pointer..*pointer + offset];
@@ -57,27 +57,6 @@ impl<'a> VuePartialLoader<'a> {
SourceType::default().with_module(true).with_typescript(is_ts).with_jsx(is_jsx);
Some(JavaScriptSource::new(source_text, source_type, js_start))
}
-
- /// Find closing angle for situations where there is another `>` in between.
- /// e.g. `