forked from oxc-project/oxc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#![cfg(not(miri))] // Miri does not support custom allocators | ||
|
||
#[cfg(not(debug_assertions))] | ||
#[cfg(not(target_env = "msvc"))] | ||
#[global_allocator] | ||
static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc; | ||
|
||
#[cfg(not(debug_assertions))] | ||
#[cfg(target_os = "windows")] | ||
#[global_allocator] | ||
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; | ||
|
||
use oxc_cli::{CliRunResult, ParseRunner, Runner}; | ||
|
||
fn main() -> CliRunResult { | ||
init_miette(); | ||
|
||
let command = oxc_cli::format_command().fallback_to_usage().run(); | ||
command.handle_threads(); | ||
ParseRunner::new(command.format_options).run() | ||
} | ||
|
||
// Initialize the data which relies on `is_atty` system calls so they don't block subsequent threads. | ||
fn init_miette() { | ||
miette::set_hook(Box::new(|_| Box::new(miette::MietteHandlerOpts::new().build()))).unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use std::{env, path::Path}; | ||
|
||
use ignore::gitignore::Gitignore; | ||
use oxc_allocator::Allocator; | ||
use oxc_parser::Parser; | ||
use oxc_span::{SourceType, VALID_EXTENSIONS}; | ||
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator}; | ||
|
||
use crate::{ | ||
command::FormatOptions, | ||
result::{CliRunResult, ParseResult}, | ||
walk::{Extensions, Walk}, | ||
Runner, | ||
}; | ||
|
||
pub struct ParseRunner { | ||
options: FormatOptions, | ||
} | ||
|
||
impl Runner for ParseRunner { | ||
type Options = FormatOptions; | ||
|
||
fn new(options: Self::Options) -> Self { | ||
Self { options } | ||
} | ||
|
||
fn run(self) -> CliRunResult { | ||
let FormatOptions { paths, ignore_options, .. } = self.options; | ||
|
||
let mut paths = paths; | ||
|
||
// The ignore crate whitelists explicit paths, but priority | ||
// should be given to the ignore file. Many users lint | ||
// automatically and pass a list of changed files explicitly. | ||
// To accommodate this, unless `--no-ignore` is passed, | ||
// pre-filter the paths. | ||
if !paths.is_empty() && !ignore_options.no_ignore { | ||
let (ignore, _err) = Gitignore::new(&ignore_options.ignore_path); | ||
paths.retain(|p| if p.is_dir() { true } else { !ignore.matched(p, false).is_ignore() }); | ||
} | ||
|
||
if paths.is_empty() { | ||
if let Ok(cwd) = env::current_dir() { | ||
paths.push(cwd); | ||
} else { | ||
return CliRunResult::InvalidOptions { | ||
message: "Failed to get current working directory.".to_string(), | ||
}; | ||
} | ||
} | ||
|
||
let extensions = VALID_EXTENSIONS.to_vec(); | ||
|
||
let now = std::time::Instant::now(); | ||
|
||
let paths = | ||
Walk::new(&paths, &ignore_options).with_extensions(Extensions(extensions)).paths(); | ||
|
||
let total_duration = paths.par_iter().map(|path| Self::parse(path)).sum(); | ||
|
||
CliRunResult::ParseResult(ParseResult { | ||
parse_duration: total_duration, | ||
duration: now.elapsed(), | ||
number_of_files: paths.len(), | ||
}) | ||
} | ||
} | ||
|
||
impl ParseRunner { | ||
fn parse(path: &Path) -> std::time::Duration { | ||
let source_text = std::fs::read_to_string(path).unwrap(); | ||
let allocator = Allocator::default(); | ||
let source_type = SourceType::from_path(path).unwrap(); | ||
let now = std::time::Instant::now(); | ||
let _ = Parser::new(&allocator, &source_text, source_type).preserve_parens(false).parse(); | ||
now.elapsed() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters