Skip to content

Commit

Permalink
refactor(linter): decouple module resolution from import plugin (#5829)
Browse files Browse the repository at this point in the history
closes #5815
  • Loading branch information
shulaoda authored Sep 17, 2024
1 parent 85ac3f7 commit 026ee6a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
3 changes: 2 additions & 1 deletion apps/oxlint/src/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ impl Runner for LintRunner {
let number_of_files = paths.len();

let cwd = std::env::current_dir().unwrap();
let mut options = LintServiceOptions::new(cwd, paths);
let mut options =
LintServiceOptions::new(cwd, paths).with_cross_module(enable_plugins.import_plugin);
let lint_options = OxlintOptions::default()
.with_filter(filter)
.with_config_path(basic_options.config)
Expand Down
19 changes: 14 additions & 5 deletions crates/oxc_linter/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub struct LintServiceOptions {

/// TypeScript `tsconfig.json` path for reading path alias and project references
tsconfig: Option<PathBuf>,

cross_module: bool,
}

impl LintServiceOptions {
Expand All @@ -40,7 +42,7 @@ impl LintServiceOptions {
where
T: Into<Box<Path>>,
{
Self { cwd: cwd.into(), paths, tsconfig: None }
Self { cwd: cwd.into(), paths, tsconfig: None, cross_module: false }
}

#[inline]
Expand All @@ -58,6 +60,13 @@ impl LintServiceOptions {
self
}

#[inline]
#[must_use]
pub fn with_cross_module(mut self, cross_module: bool) -> Self {
self.cross_module = cross_module;
self
}

#[inline]
pub fn cwd(&self) -> &Path {
&self.cwd
Expand Down Expand Up @@ -165,7 +174,7 @@ pub struct Runtime {

impl Runtime {
fn new(linter: Linter, options: LintServiceOptions) -> Self {
let resolver = linter.options().plugins.has_import().then(|| {
let resolver = options.cross_module.then(|| {
Self::get_resolver(options.tsconfig.or_else(|| Some(options.cwd.join("tsconfig.json"))))
});
Self {
Expand Down Expand Up @@ -310,7 +319,7 @@ impl Runtime {
.build_module_record(path, program);
let module_record = semantic_builder.module_record();

if self.linter.options().plugins.has_import() {
if self.resolver.is_some() {
self.module_map.insert(
path.to_path_buf().into_boxed_path(),
ModuleState::Resolved(Arc::clone(&module_record)),
Expand Down Expand Up @@ -392,7 +401,7 @@ impl Runtime {
}

fn init_cache_state(&self, path: &Path) -> bool {
if !self.linter.options().plugins.has_import() {
if self.resolver.is_none() {
return false;
}

Expand Down Expand Up @@ -447,7 +456,7 @@ impl Runtime {
}

fn ignore_path(&self, path: &Path) {
if self.linter.options().plugins.has_import() {
if self.resolver.is_some() {
self.module_map.insert(path.to_path_buf().into_boxed_path(), ModuleState::Ignored);
self.update_cache_state(path);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ impl Tester {

let cwd = self.current_working_directory.clone();
let paths = vec![path_to_lint.into_boxed_path()];
let options = LintServiceOptions::new(cwd, paths);
let options = LintServiceOptions::new(cwd, paths).with_cross_module(self.plugins.import);
let lint_service = LintService::from_linter(linter, options);
let diagnostic_service = DiagnosticService::default();
let tx_error = diagnostic_service.sender();
Expand Down

0 comments on commit 026ee6a

Please sign in to comment.