Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lint): add partial loader register #1760

Merged
merged 1 commit into from
Dec 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat(lint): add partial loader register
  • Loading branch information
mysteryven authored and Boshen committed Dec 31, 2023
commit 164fefc5fdcef1ed539fdf19b0fc2d24d15f6b06
4 changes: 3 additions & 1 deletion crates/oxc_cli/fixtures/walk_dir/bar.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<template>
<div>Hello World</div>
</template>
<script></script>
<script>
console.log('foo')
</script>
21 changes: 21 additions & 0 deletions crates/oxc_linter/src/partial_loader/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
use oxc_span::SourceType;

pub const LINT_PARTIAL_LOADER_EXT: &[&str] = &["vue"];

pub enum PartialLoader {
Vue,
}

pub struct PartialLoaderValue {
pub source_text: String,
pub source_type: SourceType,
}

impl PartialLoader {
// TODO: add parser
pub fn parse(&self, _source_text: &str) -> PartialLoaderValue {
PartialLoaderValue {
source_text: String::from("const a = 1"), // for not report "empty file error"
source_type: SourceType::default(),
}
}
}
46 changes: 33 additions & 13 deletions crates/oxc_linter/src/service.rs
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ use oxc_resolver::{ResolveOptions, Resolver};
use oxc_semantic::{ModuleRecord, SemanticBuilder};
use oxc_span::{SourceType, VALID_EXTENSIONS};

use crate::{Fixer, LintContext, Linter, Message};
use crate::{partial_loader::PartialLoader, Fixer, LintContext, Linter, Message};

#[derive(Clone)]
pub struct LintService {
@@ -110,6 +110,7 @@ pub struct Runtime {
resolver: Resolver,
module_map: ModuleMap,
cache_state: CacheState,
partial_vue_loader: PartialLoader,
}

impl Runtime {
@@ -121,6 +122,7 @@ impl Runtime {
resolver: Self::resolver(),
module_map: ModuleMap::default(),
cache_state: CacheState::default(),
partial_vue_loader: PartialLoader::Vue,
}
}

@@ -131,27 +133,45 @@ impl Runtime {
})
}

fn process_path(&self, path: &Path, tx_error: &DiagnosticSender) {
let Ok(source_type) = SourceType::from_path(path) else { return };
fn get_source_type_and_text(&self, path: &Path) -> Option<Result<(SourceType, String), Error>> {
let read_file = |path: &Path| -> Result<String, Error> {
fs::read_to_string(path)
.map_err(|e| Error::new(FailedToOpenFileError(path.to_path_buf(), e)))
};

if let Ok(source_type) = SourceType::from_path(path) {
match read_file(path) {
Ok(source_text) => Some(Ok((source_type, source_text))),
Err(e) => Some(Err(e)),
}
} else {
let ext = path.extension().and_then(std::ffi::OsStr::to_str)?;
let partial_loader = if ext == "vue" { Some(&self.partial_vue_loader) } else { None };
let partial_loader = partial_loader?;

let source_text = match read_file(path) {
Ok(source_text) => source_text,
Err(e) => return Some(Err(e)),
};

let ret = partial_loader.parse(&source_text);
Some(Ok((ret.source_type, ret.source_text)))
}
}

fn process_path(&self, path: &Path, tx_error: &DiagnosticSender) {
if self.init_cache_state(path) {
return;
}

let allocator = Allocator::default();
let source_text = match fs::read_to_string(path) {
let Some(source_type_and_text) = self.get_source_type_and_text(path) else { return };
let (source_type, source_text) = match source_type_and_text {
Ok(source_text) => source_text,
Err(e) => {
tx_error
.send(Some((
path.to_path_buf(),
vec![Error::new(FailedToOpenFileError(path.to_path_buf(), e))],
)))
.unwrap();
tx_error.send(Some((path.to_path_buf(), vec![e]))).unwrap();
return;
}
};

let allocator = Allocator::default();
let mut messages =
self.process_source(path, &allocator, &source_text, source_type, true, tx_error);