diff --git a/crates/oxc_cli/fixtures/svelte/debugger.svelte b/crates/oxc_cli/fixtures/svelte/debugger.svelte
new file mode 100644
index 000000000..44bf6421e
--- /dev/null
+++ b/crates/oxc_cli/fixtures/svelte/debugger.svelte
@@ -0,0 +1,5 @@
+
+
+
Hello {name}!
diff --git a/crates/oxc_cli/src/lint/mod.rs b/crates/oxc_cli/src/lint/mod.rs
index 4a75fb6ae..7e449478f 100644
--- a/crates/oxc_cli/src/lint/mod.rs
+++ b/crates/oxc_cli/src/lint/mod.rs
@@ -379,4 +379,13 @@ mod test {
assert_eq!(result.number_of_warnings, 4);
assert_eq!(result.number_of_errors, 0);
}
+
+ #[test]
+ fn lint_svelte_file() {
+ let args = &["fixtures/svelte/debugger.svelte"];
+ let result = test(args);
+ assert_eq!(result.number_of_files, 1);
+ assert_eq!(result.number_of_warnings, 1);
+ assert_eq!(result.number_of_errors, 0);
+ }
}
diff --git a/crates/oxc_linter/src/partial_loader/astro.rs b/crates/oxc_linter/src/partial_loader/astro.rs
index bed6b1030..8bc28c537 100644
--- a/crates/oxc_linter/src/partial_loader/astro.rs
+++ b/crates/oxc_linter/src/partial_loader/astro.rs
@@ -2,11 +2,9 @@ use memchr::memmem::Finder;
use oxc_span::{SourceType, Span};
-use super::JavaScriptSource;
+use super::{JavaScriptSource, SCRIPT_END, SCRIPT_START};
const ASTRO_SPLIT: &str = "---";
-const SCRIPT_START: &str = "";
-pub enum PartialLoader {
- Vue,
- Astro,
-}
+pub const LINT_PARTIAL_LOADER_EXT: &[&str] = &["vue", "astro", "svelte"];
#[derive(Debug, Clone, Copy)]
pub struct JavaScriptSource<'a> {
@@ -24,11 +23,17 @@ impl<'a> JavaScriptSource<'a> {
}
}
+pub struct PartialLoader;
+
impl PartialLoader {
- pub fn build<'a>(&self, source_text: &'a str) -> Vec> {
- match self {
- Self::Vue => VuePartialLoader::new(source_text).parse(),
- Self::Astro => AstroPartialLoader::new(source_text).parse(),
+ /// Extract js section of specifial files.
+ /// Returns `None` if the specifial file does not have a js section.
+ pub fn parse<'a>(ext: &str, source_text: &'a str) -> Option>> {
+ match ext {
+ "vue" => Some(VuePartialLoader::new(source_text).parse()),
+ "astro" => Some(AstroPartialLoader::new(source_text).parse()),
+ "svelte" => Some(SveltePartialLoader::new(source_text).parse()),
+ _ => None,
}
}
}
diff --git a/crates/oxc_linter/src/partial_loader/svelte.rs b/crates/oxc_linter/src/partial_loader/svelte.rs
new file mode 100644
index 000000000..efb866f90
--- /dev/null
+++ b/crates/oxc_linter/src/partial_loader/svelte.rs
@@ -0,0 +1,48 @@
+use memchr::memmem::Finder;
+
+use oxc_span::SourceType;
+
+use super::{JavaScriptSource, SCRIPT_END, SCRIPT_START};
+
+pub struct SveltePartialLoader<'a> {
+ source_text: &'a str,
+}
+
+impl<'a> SveltePartialLoader<'a> {
+ pub fn new(source_text: &'a str) -> Self {
+ Self { source_text }
+ }
+
+ pub fn parse(self) -> Vec> {
+ self.parse_script().map_or_else(Vec::new, |source| vec![source])
+ }
+
+ fn parse_script(&self) -> Option> {
+ let script_start_finder = Finder::new(SCRIPT_START);
+ let script_end_finder = Finder::new(SCRIPT_END);
+
+ let mut pointer = 0;
+
+ // find opening ""
+ let offset = script_end_finder.find(self.source_text[pointer..].as_bytes())?;
+ let js_end = pointer + offset;
+
+ let source_text = &self.source_text[js_start..js_end];
+ let source_type = SourceType::default().with_module(true).with_typescript(is_ts);
+ Some(JavaScriptSource::new(source_text, source_type))
+ }
+}
diff --git a/crates/oxc_linter/src/partial_loader/vue.rs b/crates/oxc_linter/src/partial_loader/vue.rs
index 8b21a2001..7aff48eb9 100644
--- a/crates/oxc_linter/src/partial_loader/vue.rs
+++ b/crates/oxc_linter/src/partial_loader/vue.rs
@@ -2,10 +2,7 @@ use memchr::memmem::Finder;
use oxc_span::SourceType;
-use super::JavaScriptSource;
-
-const SCRIPT_START: &str = "