From 11ca5c2badc3197c20493f4e0c996f4d83226f6a Mon Sep 17 00:00:00 2001 From: Boshen Date: Thu, 4 Jan 2024 21:24:19 +0800 Subject: [PATCH] feat(linter): do not lint when vue file has no js section (#1891) --- .../fixtures/{linter => vue}/debugger.vue | 0 crates/oxc_cli/fixtures/vue/empty.vue | 2 ++ crates/oxc_cli/src/lint/mod.rs | 25 +++++++++++------ crates/oxc_linter/src/partial_loader/mod.rs | 1 - crates/oxc_linter/src/service.rs | 28 ++++++++++--------- 5 files changed, 34 insertions(+), 22 deletions(-) rename crates/oxc_cli/fixtures/{linter => vue}/debugger.vue (100%) create mode 100644 crates/oxc_cli/fixtures/vue/empty.vue diff --git a/crates/oxc_cli/fixtures/linter/debugger.vue b/crates/oxc_cli/fixtures/vue/debugger.vue similarity index 100% rename from crates/oxc_cli/fixtures/linter/debugger.vue rename to crates/oxc_cli/fixtures/vue/debugger.vue diff --git a/crates/oxc_cli/fixtures/vue/empty.vue b/crates/oxc_cli/fixtures/vue/empty.vue new file mode 100644 index 000000000..6beff5199 --- /dev/null +++ b/crates/oxc_cli/fixtures/vue/empty.vue @@ -0,0 +1,2 @@ + diff --git a/crates/oxc_cli/src/lint/mod.rs b/crates/oxc_cli/src/lint/mod.rs index 2e8271993..34ba6853e 100644 --- a/crates/oxc_cli/src/lint/mod.rs +++ b/crates/oxc_cli/src/lint/mod.rs @@ -252,8 +252,7 @@ mod test { let args = &[]; let result = test(args); assert!(result.number_of_rules > 0); - assert_eq!(result.number_of_files, 5); - assert_eq!(result.number_of_warnings, 3); + assert!(result.number_of_warnings > 0); assert_eq!(result.number_of_errors, 0); } @@ -262,8 +261,8 @@ mod test { let args = &["fixtures/linter"]; let result = test(args); assert!(result.number_of_rules > 0); - assert_eq!(result.number_of_files, 3); - assert_eq!(result.number_of_warnings, 3); + assert_eq!(result.number_of_files, 2); + assert_eq!(result.number_of_warnings, 2); assert_eq!(result.number_of_errors, 0); } @@ -296,7 +295,8 @@ mod test { #[test] fn ignore_pattern() { - let args = &["--ignore-pattern", "**/*.js", "--ignore-pattern", "**/*.vue", "fixtures"]; + let args = + &["--ignore-pattern", "**/*.js", "--ignore-pattern", "**/*.vue", "fixtures/linter"]; let result = test(args); assert_eq!(result.number_of_files, 0); assert_eq!(result.number_of_warnings, 0); @@ -331,7 +331,7 @@ mod test { #[test] fn filter_allow_all() { - let args = &["-A", "all", "fixtures"]; + let args = &["-A", "all", "fixtures/linter"]; let result = test(args); assert!(result.number_of_files > 0); assert_eq!(result.number_of_warnings, 0); @@ -348,11 +348,20 @@ mod test { } #[test] - fn test_lint_vue_file() { - let args = &["fixtures/linter/debugger.vue"]; + fn lint_vue_file() { + let args = &["fixtures/vue/debugger.vue"]; 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); } + + #[test] + fn lint_empty_vue_file() { + let args = &["fixtures/vue/empty.vue"]; + let result = test(args); + assert_eq!(result.number_of_files, 1); + assert_eq!(result.number_of_warnings, 0); + assert_eq!(result.number_of_errors, 0); + } } diff --git a/crates/oxc_linter/src/partial_loader/mod.rs b/crates/oxc_linter/src/partial_loader/mod.rs index 417d62ba2..76574a151 100644 --- a/crates/oxc_linter/src/partial_loader/mod.rs +++ b/crates/oxc_linter/src/partial_loader/mod.rs @@ -18,7 +18,6 @@ pub struct PartialLoaderValue<'a> { impl<'a> PartialLoaderValue<'a> { pub fn from(source_text: &'a str, is_ts: bool, is_jsx: bool) -> Self { - // `module_kind` should be `ModuleKind::Module` for allow `import` let source_type = SourceType::default().with_typescript(is_ts).with_module(true).with_jsx(is_jsx); Self { source_text, source_type } diff --git a/crates/oxc_linter/src/service.rs b/crates/oxc_linter/src/service.rs index c95db06a1..846dad459 100644 --- a/crates/oxc_linter/src/service.rs +++ b/crates/oxc_linter/src/service.rs @@ -17,10 +17,7 @@ use oxc_resolver::{ResolveOptions, Resolver}; use oxc_semantic::{ModuleRecord, SemanticBuilder}; use oxc_span::{SourceType, VALID_EXTENSIONS}; -use crate::{ - partial_loader::{PartialLoader, PartialLoaderValue}, - Fixer, LintContext, Linter, Message, -}; +use crate::{partial_loader::PartialLoader, Fixer, LintContext, Linter, Message}; #[derive(Clone)] pub struct LintService { @@ -160,18 +157,22 @@ impl Runtime { Some(Ok((source_type, source_text))) } - fn may_need_extract_js_content<'a>( + /// Extract js section of specifial files. + /// Returns `None` if the specifial file does not have a js section. + fn extract_js<'a>( &self, source_text: &'a str, + source_type: SourceType, ext: &str, ) -> Option<(&'a str, SourceType)> { if ext == "vue" { - let PartialLoaderValue { source_text, source_type } = - self.partial_vue_loader.parse(source_text); - Some((source_text, source_type)) - } else { - None + let result = self.partial_vue_loader.parse(source_text); + if result.source_text.is_empty() { + return None; + } + return Some((result.source_text, result.source_type)); } + Some((source_text, source_type)) } fn process_path(&self, path: &Path, tx_error: &DiagnosticSender) { @@ -189,9 +190,10 @@ impl Runtime { return; } }; - let (source_text, source_type) = self - .may_need_extract_js_content(&source_text, ext) - .unwrap_or((&source_text, source_type)); + let Some((source_text, source_type)) = self.extract_js(&source_text, source_type, ext) + else { + return; + }; let allocator = Allocator::default(); let mut messages =