Skip to content

Commit 74fb6c9

Browse files
committed
refactor(linter): reduce repeated code (#13070)
Refactor. Many branches of `process_path` return a default `ProcessedModule`. All branches return a `ModuleProcessOutput` with `path` field containing a clone of `path` param. Move the duplicated logic into a separate function, to allow shortening and simplifying the logic. There may be a small perf benefit of not repeating the same code in many places, but mainly the motivation is to shorten the code, which I find makes it a bit easier to understand.
1 parent c211d32 commit 74fb6c9

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

crates/oxc_linter/src/service/runtime.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -867,20 +867,24 @@ impl Runtime {
867867
check_syntax_errors: bool,
868868
tx_error: &DiagnosticSender,
869869
) -> ModuleProcessOutput<'_> {
870-
let default_output = || ModuleProcessOutput {
871-
path: Arc::clone(path),
872-
processed_module: ProcessedModule::default(),
873-
};
870+
let processed_module =
871+
self.process_path_to_module(path, check_syntax_errors, tx_error).unwrap_or_default();
872+
ModuleProcessOutput { path: Arc::clone(path), processed_module }
873+
}
874874

875-
let Some(ext) = Path::new(path).extension().and_then(OsStr::to_str) else {
876-
return default_output();
877-
};
875+
fn process_path_to_module(
876+
&self,
877+
path: &Arc<OsStr>,
878+
check_syntax_errors: bool,
879+
tx_error: &DiagnosticSender,
880+
) -> Option<ProcessedModule<'_>> {
881+
let ext = Path::new(path).extension().and_then(OsStr::to_str)?;
878882

879883
if SourceType::from_path(Path::new(path))
880884
.as_ref()
881885
.is_err_and(|_| !LINT_PARTIAL_LOADER_EXTENSIONS.contains(&ext))
882886
{
883-
return default_output();
887+
return None;
884888
}
885889

886890
let mut records = SmallVec::<[Result<ResolvedModuleRecord, Vec<OxcDiagnostic>>; 1]>::new();
@@ -889,7 +893,7 @@ impl Runtime {
889893
if self.paths.contains(path) {
890894
let allocator_guard = self.allocator_pool.get();
891895

892-
let build = ModuleContent::try_new(allocator_guard, |allocator| {
896+
let mc = ModuleContent::try_new(allocator_guard, |allocator| {
893897
let Some(stt) = self.get_source_type_and_text(Path::new(path), ext, allocator)
894898
else {
895899
return Err(());
@@ -916,24 +920,20 @@ impl Runtime {
916920

917921
Ok(ModuleContentDependent { source_text, section_contents })
918922
});
923+
let mc = mc.ok()?;
919924

920-
module_content = match build {
921-
Ok(mc) => Some(mc),
922-
Err(()) => return default_output(),
923-
};
925+
module_content = Some(mc);
924926
} else {
925927
let allocator_guard = self.allocator_pool.get();
926928
let allocator = &*allocator_guard;
927929

928-
let Some(stt) = self.get_source_type_and_text(Path::new(path), ext, allocator) else {
929-
return default_output();
930-
};
930+
let stt = self.get_source_type_and_text(Path::new(path), ext, allocator)?;
931931

932932
let (source_type, source_text) = match stt {
933933
Ok(v) => v,
934934
Err(e) => {
935935
tx_error.send((Path::new(path).to_path_buf(), vec![e])).unwrap();
936-
return default_output();
936+
return None;
937937
}
938938
};
939939

@@ -948,13 +948,7 @@ impl Runtime {
948948
);
949949
}
950950

951-
ModuleProcessOutput {
952-
path: Arc::clone(path),
953-
processed_module: ProcessedModule {
954-
section_module_records: records,
955-
content: module_content,
956-
},
957-
}
951+
Some(ProcessedModule { section_module_records: records, content: module_content })
958952
}
959953

960954
#[expect(clippy::too_many_arguments)]

0 commit comments

Comments
 (0)