Skip to content

Commit

Permalink
feat(lint): add partial loader register
Browse files Browse the repository at this point in the history
  • Loading branch information
mysteryven committed Dec 21, 2023
1 parent 9e02ef1 commit 471b48e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 14 deletions.
28 changes: 28 additions & 0 deletions crates/oxc_linter/src/partial_loader/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
use oxc_diagnostics::Error;
use oxc_span::SourceType;

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

pub enum PartialLoader {
Vue,
Astro,
}

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

pub type PartialLoaderResult = Result<PartialLoaderValue, Error>;

impl PartialLoader {
// TODO: add parser
pub fn parse(&self, _source_text: &str) -> PartialLoaderValue {
PartialLoaderValue { source_text: String::new(), source_type: SourceType::default() }
}
pub fn filter_extension(&self, extension: &str) -> bool {
match self {
Self::Vue => matches!(extension, "vue"),
Self::Astro => matches!(extension, "astro"),
}
}
}
55 changes: 41 additions & 14 deletions crates/oxc_linter/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{

use dashmap::DashMap;
use rayon::{iter::ParallelBridge, prelude::ParallelIterator};
use rustc_hash::FxHashSet;
use rustc_hash::{FxHashMap, FxHashSet};

use oxc_allocator::Allocator;
use oxc_diagnostics::{DiagnosticSender, DiagnosticService, Error, FailedToOpenFileError};
Expand All @@ -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 {
Expand Down Expand Up @@ -110,6 +110,7 @@ pub struct Runtime {
resolver: Resolver,
module_map: ModuleMap,
cache_state: CacheState,
partial_loaders: FxHashMap<String, PartialLoader>,
}

impl Runtime {
Expand All @@ -121,37 +122,63 @@ impl Runtime {
resolver: Self::resolver(),
module_map: ModuleMap::default(),
cache_state: CacheState::default(),
partial_loaders: FxHashMap::default(),
}
}

#[allow(dead_code)]
fn register_partial_loader(&mut self, name: &str, partial_loader: PartialLoader) {
self.partial_loaders.insert(name.to_string(), partial_loader);
}

fn resolver() -> Resolver {
Resolver::new(ResolveOptions {
extensions: VALID_EXTENSIONS.iter().map(|ext| format!(".{ext}")).collect(),
..ResolveOptions::default()
})
}

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 = self.partial_loaders.get(ext)?;
if !partial_loader.filter_extension(ext) {
return None;
}

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

Expand Down

0 comments on commit 471b48e

Please sign in to comment.