diff --git a/crates/oxc_language_server/src/linter/isolated_lint_handler.rs b/crates/oxc_language_server/src/linter/isolated_lint_handler.rs index a91f0653c6300..9935015e761cf 100644 --- a/crates/oxc_language_server/src/linter/isolated_lint_handler.rs +++ b/crates/oxc_language_server/src/linter/isolated_lint_handler.rs @@ -1,5 +1,4 @@ use std::{ - fs, path::{Path, PathBuf}, sync::{Arc, OnceLock}, }; @@ -14,7 +13,7 @@ use tower_lsp_server::{ use oxc_allocator::Allocator; use oxc_linter::{ LINTABLE_EXTENSIONS, LintService, LintServiceOptions, Linter, MessageWithPosition, - loader::Loader, + loader::Loader, read_to_string, }; use super::error_with_position::{ @@ -103,7 +102,7 @@ impl IsolatedLintHandler { debug!("extension not supported yet."); return None; } - let source_text = source_text.or_else(|| fs::read_to_string(path).ok())?; + let source_text = source_text.or_else(|| read_to_string(path).ok())?; debug!("lint {path:?}"); diff --git a/crates/oxc_linter/src/lib.rs b/crates/oxc_linter/src/lib.rs index 66703ea0bea03..cff4769f4e6d0 100644 --- a/crates/oxc_linter/src/lib.rs +++ b/crates/oxc_linter/src/lib.rs @@ -44,6 +44,7 @@ pub use crate::{ options::{AllowWarnDeny, InvalidFilterKind, LintFilter, LintFilterKind}, rule::{RuleCategory, RuleFixMeta, RuleMeta, RuleWithSeverity}, service::{LintService, LintServiceOptions}, + utils::read_to_string, }; use crate::{ config::{LintConfig, OxlintEnv, OxlintGlobals, OxlintSettings, ResolvedLinterState}, diff --git a/crates/oxc_linter/src/utils/mod.rs b/crates/oxc_linter/src/utils/mod.rs index de8d28274e405..af496fb23a5b3 100644 --- a/crates/oxc_linter/src/utils/mod.rs +++ b/crates/oxc_linter/src/utils/mod.rs @@ -113,6 +113,11 @@ pub fn is_eslint_rule_adapted_to_typescript(rule_name: &str) -> bool { TYPESCRIPT_COMPATIBLE_ESLINT_RULES.binary_search(&rule_name).is_ok() } +/// Reads the content of a path and returns it. +/// This function is faster than native `fs:read_to_string`. +/// +/// # Errors +/// When the content of the path is not a valid UTF-8 bytes pub fn read_to_string(path: &Path) -> io::Result { // `simdutf8` is faster than `std::str::from_utf8` which `fs::read_to_string` uses internally let bytes = std::fs::read(path)?;