feat(linter): do not lint when vue file has no js section (#1891)

This commit is contained in:
Boshen 2024-01-04 21:24:19 +08:00 committed by GitHub
parent d0c4100ca8
commit 11ca5c2bad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 22 deletions

View file

@ -0,0 +1,2 @@
<template>
</template>

View file

@ -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);
}
}

View file

@ -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 }

View file

@ -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 =