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

refactor: avoid UTF-8 validation of file extensions #3737

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions crates/biome_cli/src/execute/migrate/eslint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use biome_fs::{FileSystem, OpenOptions};
use biome_json_parser::JsonParserOptions;
use biome_service::DynRef;
use std::borrow::Cow;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};

use crate::diagnostics::MigrationDiagnostic;
Expand Down Expand Up @@ -154,8 +155,7 @@ fn load_legacy_config_data(
path: &Path,
console: &mut dyn Console,
) -> Result<eslint_eslint::LegacyConfigData, CliDiagnostic> {
let (deserialized, diagnostics) = match path.extension().and_then(|file_ext| file_ext.to_str())
{
let (deserialized, diagnostics) = match path.extension().and_then(OsStr::to_str) {
None | Some("json") => {
let mut file = fs.open_with_options(path, OpenOptions::default().read(true))?;
let mut content = String::new();
Expand Down
5 changes: 2 additions & 3 deletions crates/biome_cli/src/execute/migrate/prettier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use biome_fs::{FileSystem, OpenOptions};
use biome_js_formatter::context::{ArrowParentheses, QuoteProperties, Semicolons, TrailingCommas};
use biome_json_parser::JsonParserOptions;
use biome_service::DynRef;
use std::path::Path;
use std::{ffi::OsStr, path::Path};

use super::{eslint_eslint::ShorthandVec, node};

Expand Down Expand Up @@ -416,8 +416,7 @@ fn load_config(
path: &Path,
console: &mut dyn Console,
) -> Result<PrettierConfiguration, CliDiagnostic> {
let (deserialized, diagnostics) = match path.extension().and_then(|file_ext| file_ext.to_str())
{
let (deserialized, diagnostics) = match path.extension().and_then(OsStr::to_str) {
None | Some("json") => {
let mut file = fs.open_with_options(path, OpenOptions::default().read(true))?;
let mut content = String::new();
Expand Down
10 changes: 6 additions & 4 deletions crates/biome_cli/src/execute/process_file/assists.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ffi::OsStr;

use crate::execute::diagnostics::ResultExt;
use crate::execute::process_file::workspace_file::WorkspaceFile;
use crate::execute::process_file::{
Expand Down Expand Up @@ -38,14 +40,14 @@ pub(crate) fn assists_with_guard<'ctx>(

let mut output = fix_result.code;

match workspace_file.as_extension() {
Some("astro") => {
match workspace_file.as_extension().map(OsStr::as_encoded_bytes) {
Some(b"astro") => {
output = AstroFileHandler::output(input.as_str(), output.as_str());
}
Some("vue") => {
Some(b"vue") => {
output = VueFileHandler::output(input.as_str(), output.as_str());
}
Some("svelte") => {
Some(b"svelte") => {
output = SvelteFileHandler::output(input.as_str(), output.as_str());
}
_ => {}
Expand Down
9 changes: 5 additions & 4 deletions crates/biome_cli/src/execute/process_file/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::execute::TraversalMode;
use biome_analyze::RuleCategoriesBuilder;
use biome_diagnostics::{category, Diagnostic, DiagnosticExt, Error, Severity};
use biome_service::file_handlers::{AstroFileHandler, SvelteFileHandler, VueFileHandler};
use std::ffi::OsStr;
use std::path::Path;
use std::sync::atomic::Ordering;
use tracing::debug;
Expand Down Expand Up @@ -90,21 +91,21 @@ pub(crate) fn format_with_guard<'ctx>(
return Ok(FileStatus::Ignored);
}

match workspace_file.as_extension() {
Some("astro") => {
match workspace_file.as_extension().map(OsStr::as_encoded_bytes) {
Some(b"astro") => {
if output.is_empty() {
return Ok(FileStatus::Unchanged);
}
output = AstroFileHandler::output(input.as_str(), output.as_str());
}
Some("vue") => {
Some(b"vue") => {
if output.is_empty() {
return Ok(FileStatus::Unchanged);
}
output = VueFileHandler::output(input.as_str(), output.as_str());
}

Some("svelte") => {
Some(b"svelte") => {
if output.is_empty() {
return Ok(FileStatus::Unchanged);
}
Expand Down
17 changes: 9 additions & 8 deletions crates/biome_cli/src/execute/process_file/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use biome_analyze::RuleCategoriesBuilder;
use biome_diagnostics::{category, Error};
use biome_rowan::TextSize;
use biome_service::file_handlers::{AstroFileHandler, SvelteFileHandler, VueFileHandler};
use std::ffi::OsStr;
use std::path::Path;
use std::sync::atomic::Ordering;

Expand Down Expand Up @@ -53,14 +54,14 @@ pub(crate) fn lint_with_guard<'ctx>(

let mut output = fix_result.code;

match workspace_file.as_extension() {
Some("astro") => {
match workspace_file.as_extension().map(OsStr::as_encoded_bytes) {
Some(b"astro") => {
output = AstroFileHandler::output(input.as_str(), output.as_str());
}
Some("vue") => {
Some(b"vue") => {
output = VueFileHandler::output(input.as_str(), output.as_str());
}
Some("svelte") => {
Some(b"svelte") => {
output = SvelteFileHandler::output(input.as_str(), output.as_str());
}
_ => {}
Expand Down Expand Up @@ -93,10 +94,10 @@ pub(crate) fn lint_with_guard<'ctx>(
&& pull_diagnostics_result.skipped_diagnostics == 0;

if !no_diagnostics {
let offset = match workspace_file.as_extension() {
Some("vue") => VueFileHandler::start(input.as_str()),
Some("astro") => AstroFileHandler::start(input.as_str()),
Some("svelte") => SvelteFileHandler::start(input.as_str()),
let offset = match workspace_file.as_extension().map(OsStr::as_encoded_bytes) {
Some(b"vue") => VueFileHandler::start(input.as_str()),
Some(b"astro") => AstroFileHandler::start(input.as_str()),
Some(b"svelte") => SvelteFileHandler::start(input.as_str()),
_ => None,
};

Expand Down
10 changes: 6 additions & 4 deletions crates/biome_cli/src/execute/process_file/organize_imports.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ffi::OsStr;

use crate::execute::diagnostics::ResultExt;
use crate::execute::process_file::workspace_file::WorkspaceFile;
use crate::execute::process_file::{
Expand All @@ -24,21 +26,21 @@ pub(crate) fn organize_imports_with_guard<'ctx>(
let input = workspace_file.input()?;
let mut output = sorted.code;

match workspace_file.as_extension() {
Some("astro") => {
match workspace_file.as_extension().map(OsStr::as_encoded_bytes) {
Some(b"astro") => {
if output.is_empty() {
return Ok(FileStatus::Unchanged);
}
output = AstroFileHandler::output(input.as_str(), output.as_str());
}
Some("vue") => {
Some(b"vue") => {
if output.is_empty() {
return Ok(FileStatus::Unchanged);
}
output = VueFileHandler::output(input.as_str(), output.as_str());
}

Some("svelte") => {
Some(b"svelte") => {
if output.is_empty() {
return Ok(FileStatus::Unchanged);
}
Expand Down
5 changes: 3 additions & 2 deletions crates/biome_cli/src/execute/process_file/workspace_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use biome_diagnostics::{category, Error};
use biome_fs::{BiomePath, File, OpenOptions};
use biome_service::workspace::{FileGuard, OpenFileParams};
use biome_service::{Workspace, WorkspaceError};
use std::ffi::OsStr;
use std::path::{Path, PathBuf};

/// Small wrapper that holds information and operations around the current processed file
Expand Down Expand Up @@ -59,8 +60,8 @@ impl<'ctx, 'app> WorkspaceFile<'ctx, 'app> {
self.guard().get_file_content()
}

pub(crate) fn as_extension(&self) -> Option<&str> {
self.path.extension().and_then(|s| s.to_str())
pub(crate) fn as_extension(&self) -> Option<&OsStr> {
self.path.extension()
}

/// It updates the workspace file with `new_content`
Expand Down
32 changes: 16 additions & 16 deletions crates/biome_cli/src/execute/std_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ pub(crate) fn run<'a>(
})?;

let code = printed.into_code();
let output = match biome_path.extension_as_str() {
Some("astro") => AstroFileHandler::output(content, code.as_str()),
Some("vue") => VueFileHandler::output(content, code.as_str()),
Some("svelte") => SvelteFileHandler::output(content, code.as_str()),
let output = match biome_path.extension().map(|ext| ext.as_encoded_bytes()) {
Some(b"astro") => AstroFileHandler::output(content, code.as_str()),
Some(b"vue") => VueFileHandler::output(content, code.as_str()),
Some(b"svelte") => SvelteFileHandler::output(content, code.as_str()),
_ => code,
};
console.append(markup! {
Expand Down Expand Up @@ -127,10 +127,10 @@ pub(crate) fn run<'a>(
.build(),
})?;
let code = fix_file_result.code;
let output = match biome_path.extension_as_str() {
Some("astro") => AstroFileHandler::output(&new_content, code.as_str()),
Some("vue") => VueFileHandler::output(&new_content, code.as_str()),
Some("svelte") => SvelteFileHandler::output(&new_content, code.as_str()),
let output = match biome_path.extension().map(|ext| ext.as_encoded_bytes()) {
Some(b"astro") => AstroFileHandler::output(&new_content, code.as_str()),
Some(b"vue") => VueFileHandler::output(&new_content, code.as_str()),
Some(b"svelte") => SvelteFileHandler::output(&new_content, code.as_str()),
_ => code,
};
if output != new_content {
Expand All @@ -149,10 +149,10 @@ pub(crate) fn run<'a>(
path: biome_path.clone(),
})?;
let code = result.code;
let output = match biome_path.extension_as_str() {
Some("astro") => AstroFileHandler::output(&new_content, code.as_str()),
Some("vue") => VueFileHandler::output(&new_content, code.as_str()),
Some("svelte") => SvelteFileHandler::output(&new_content, code.as_str()),
let output = match biome_path.extension().map(|ext| ext.as_encoded_bytes()) {
Some(b"astro") => AstroFileHandler::output(&new_content, code.as_str()),
Some(b"vue") => VueFileHandler::output(&new_content, code.as_str()),
Some(b"svelte") => SvelteFileHandler::output(&new_content, code.as_str()),
_ => code,
};
if output != new_content {
Expand All @@ -172,10 +172,10 @@ pub(crate) fn run<'a>(
path: biome_path.clone(),
})?;
let code = printed.into_code();
let output = match biome_path.extension_as_str() {
Some("astro") => AstroFileHandler::output(&new_content, code.as_str()),
Some("vue") => VueFileHandler::output(&new_content, code.as_str()),
Some("svelte") => SvelteFileHandler::output(&new_content, code.as_str()),
let output = match biome_path.extension().map(|ext| ext.as_encoded_bytes()) {
Some(b"astro") => AstroFileHandler::output(&new_content, code.as_str()),
Some(b"vue") => VueFileHandler::output(&new_content, code.as_str()),
Some(b"svelte") => SvelteFileHandler::output(&new_content, code.as_str()),
_ => code,
};
if (mode.is_check_apply() || mode.is_check_apply_unsafe()) && output != new_content {
Expand Down
7 changes: 3 additions & 4 deletions crates/biome_fs/src/fs/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use crate::{
use biome_diagnostics::{adapters::IoError, DiagnosticExt, Error, Severity};
use oxc_resolver::{Resolution, ResolveError, ResolveOptions, Resolver};
use rayon::{scope, Scope};
use std::ffi::OsStr;
use std::fs::{DirEntry, FileType};
use std::panic::AssertUnwindSafe;
use std::process::Command;
Expand Down Expand Up @@ -220,7 +219,7 @@ impl<'scope> TraversalScope<'scope> for OsTraversalScope<'scope> {
// TODO: remove in Biome 2.0, and directly use `.gitignore`
/// Default list of ignored directories, in the future will be supplanted by
/// detecting and parsing .ignore files
const DEFAULT_IGNORE: &[&str; 5] = &[".git", ".svn", ".hg", ".yarn", "node_modules"];
const DEFAULT_IGNORE: &[&[u8]] = &[b".git", b".svn", b".hg", b".yarn", b"node_modules"];

/// Traverse a single directory
fn handle_dir<'scope>(
Expand All @@ -230,8 +229,8 @@ fn handle_dir<'scope>(
// The unresolved origin path in case the directory is behind a symbolic link
origin_path: Option<PathBuf>,
) {
if let Some(file_name) = path.file_name().and_then(OsStr::to_str) {
if DEFAULT_IGNORE.contains(&file_name) {
if let Some(file_name) = path.file_name() {
if DEFAULT_IGNORE.contains(&file_name.as_encoded_bytes()) {
return;
}
}
Expand Down
5 changes: 0 additions & 5 deletions crates/biome_fs/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,6 @@ impl BiomePath {
read_to_string(path)
}

/// Returns the extension of the path
pub fn extension_as_str(&self) -> Option<&str> {
self.extension().and_then(OsStr::to_str)
}

/// The priority of the file.
/// - `biome.json` and `biome.jsonc` have the highest priority
/// - `package.json` and `tsconfig.json`/`jsconfig.json` have the second-highest priority, and they are considered as manifest files
Expand Down
2 changes: 1 addition & 1 deletion crates/biome_grit_patterns/src/grit_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl GritQuery {
let name = path
.and_then(Path::file_stem)
.map(OsStr::to_string_lossy)
.map(|stem| stem.to_string());
.map(|stem| stem.into_owned());
let language = context.lang;
let variable_locations = VariableLocations::new(vars_array);

Expand Down
12 changes: 6 additions & 6 deletions crates/biome_js_analyze/tests/spec_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,12 @@ pub(crate) fn run_suppression_test(input: &'static str, _: &str, _: &str, _: &st

let input_file = Path::new(input);
let file_name = input_file.file_name().and_then(OsStr::to_str).unwrap();
let source_type = match input_file.extension().and_then(OsStr::to_str).unwrap() {
"js" | "mjs" | "jsx" => JsFileSource::jsx(),
"cjs" => JsFileSource::js_script(),
"ts" => JsFileSource::ts(),
"mts" | "cts" => JsFileSource::ts_restricted(),
"tsx" => JsFileSource::tsx(),
let source_type = match input_file.extension().map(OsStr::as_encoded_bytes) {
Some(b"js" | b"mjs" | b"jsx") => JsFileSource::jsx(),
Some(b"cjs") => JsFileSource::js_script(),
Some(b"ts") => JsFileSource::ts(),
Some(b"mts" | b"cts") => JsFileSource::ts_restricted(),
Some(b"tsx") => JsFileSource::tsx(),
_ => {
panic!("Unknown file extension: {:?}", input_file.extension());
}
Expand Down
9 changes: 5 additions & 4 deletions crates/biome_lsp/src/handlers/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use biome_service::workspace::{
use biome_service::WorkspaceError;
use std::borrow::Cow;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::ops::Sub;
use tower_lsp::lsp_types::{
self as lsp, CodeActionKind, CodeActionOrCommand, CodeActionParams, CodeActionResponse,
Expand Down Expand Up @@ -84,10 +85,10 @@ pub(crate) fn code_actions(
let content = session.workspace.get_file_content(GetFileContentParams {
path: biome_path.clone(),
})?;
let offset = match biome_path.extension().and_then(|s| s.to_str()) {
Some("vue") => VueFileHandler::start(content.as_str()),
Some("astro") => AstroFileHandler::start(content.as_str()),
Some("svelte") => SvelteFileHandler::start(content.as_str()),
let offset = match biome_path.extension().map(OsStr::as_encoded_bytes) {
Some(b"vue") => VueFileHandler::start(content.as_str()),
Some(b"astro") => AstroFileHandler::start(content.as_str()),
Some(b"svelte") => SvelteFileHandler::start(content.as_str()),
_ => None,
};
let cursor_range = from_proto::text_range(&doc.line_index, params.range, position_encoding)
Expand Down
18 changes: 9 additions & 9 deletions crates/biome_lsp/src/handlers/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use biome_service::workspace::{
GetFileContentParams, SupportsFeatureParams,
};
use biome_service::{extension_error, WorkspaceError};
use std::ffi::OsStr;
use std::ops::{Add, Sub};
use tower_lsp::lsp_types::*;
use tracing::debug;
Expand All @@ -36,21 +37,20 @@ pub(crate) fn format(
})?;

let mut output = printed.into_code();
let file_extension = biome_path.extension().and_then(|s| s.to_str());
let input = session.workspace.get_file_content(GetFileContentParams {
path: biome_path.clone(),
})?;
if output.is_empty() {
return Ok(None);
}
match file_extension {
Some("astro") => {
match biome_path.extension().map(OsStr::as_encoded_bytes) {
Some(b"astro") => {
output = AstroFileHandler::output(input.as_str(), output.as_str());
}
Some("vue") => {
Some(b"vue") => {
output = VueFileHandler::output(input.as_str(), output.as_str());
}
Some("svelte") => {
Some(b"svelte") => {
output = SvelteFileHandler::output(input.as_str(), output.as_str());
}
_ => {}
Expand Down Expand Up @@ -104,10 +104,10 @@ pub(crate) fn format_range(
let content = session.workspace.get_file_content(GetFileContentParams {
path: biome_path.clone(),
})?;
let offset = match biome_path.extension().and_then(|s| s.to_str()) {
Some("vue") => VueFileHandler::start(content.as_str()),
Some("astro") => AstroFileHandler::start(content.as_str()),
Some("svelte") => SvelteFileHandler::start(content.as_str()),
let offset = match biome_path.extension().map(OsStr::as_encoded_bytes) {
Some(b"vue") => VueFileHandler::start(content.as_str()),
Some(b"astro") => AstroFileHandler::start(content.as_str()),
Some(b"svelte") => SvelteFileHandler::start(content.as_str()),
_ => None,
};
let format_range = if let Some(offset) = offset {
Expand Down
Loading
Loading