Skip to content

Commit

Permalink
feat(linter): import plugin and new lint runner
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jul 9, 2023
1 parent 5d67094 commit 1182477
Show file tree
Hide file tree
Showing 336 changed files with 2,528 additions and 730 deletions.
1 change: 1 addition & 0 deletions .typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ extend-exclude = [
"tasks/coverage/test262",
"tasks/coverage/babel",
"tasks/coverage/typescript",
"crates/oxc_linter/tests",
"**/*.snap",
"pnpm-lock.yaml",
]
Expand Down
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ clap = { version = "4.3.4" }
compact_str = { version = "0.7.0" }
convert_case = { version = "0.6.0" }
criterion = { version = "0.5.1", default-features = false }
crossbeam-channel = { version = "0.5.8" }
dashmap = { version = "5.4.0" }
flate2 = { version = "1.0.26" }
glob = { version = "0.3.1" }
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ oxc_resolver = { workspace = true }
codespan-reporting = "0.11.1"

clap = { workspace = true }
crossbeam-channel = { workspace = true }
dashmap = { workspace = true }
rayon = { workspace = true }
miette = { workspace = true, features = ["fancy-no-backtrace"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod walk;
use clap::{Arg, Command};

pub use crate::{
lint::{lint_command, LintOptions, LintRunner, LintRunnerWithModuleTree},
lint::{lint_command, matches_to_lint_options, LintRunner},
result::CliRunResult,
type_check::{type_check_command, TypeCheckOptions, TypeCheckRunner},
walk::Walk,
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_cli/src/lint/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc;
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;

use clap::{Arg, Command};
use oxc_cli::{lint_command, CliRunResult, LintOptions, LintRunner};
use oxc_cli::{lint_command, matches_to_lint_options, CliRunResult, LintRunner};

pub fn command() -> Command {
lint_command(
Expand All @@ -35,12 +35,12 @@ fn main() -> CliRunResult {
rayon::ThreadPoolBuilder::new().num_threads(*threads).build_global().unwrap();
}

let options = LintOptions::from(&matches);
let options = matches_to_lint_options(&matches);

if options.list_rules {
LintRunner::print_rules();
return CliRunResult::None;
}

LintRunner::new(options).run()
LintRunner::run(options)
}
126 changes: 43 additions & 83 deletions crates/oxc_cli/src/lint/mod.rs
Original file line number Diff line number Diff line change
@@ -1,96 +1,55 @@
mod command;
mod error;
mod runner;
mod runner_with_module_tree;

use std::{collections::BTreeMap, path::PathBuf};

use clap::ArgMatches;

pub use self::{
command::lint_command, error::Error, runner::LintRunner,
runner_with_module_tree::LintRunnerWithModuleTree,
};

#[derive(Debug)]
#[allow(clippy::struct_excessive_bools)]
pub struct LintOptions {
pub paths: Vec<PathBuf>,
/// Allow / Deny rules in order. [("allow" / "deny", rule name)]
/// Defaults to [("deny", "correctness")]
pub rules: Vec<(AllowWarnDeny, String)>,
pub list_rules: bool,
pub fix: bool,
pub quiet: bool,
pub ignore_path: PathBuf,
pub no_ignore: bool,
pub ignore_pattern: Vec<String>,
pub max_warnings: Option<usize>,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum AllowWarnDeny {
Allow,
// Warn,
Deny,
}

impl From<&'static str> for AllowWarnDeny {
fn from(s: &'static str) -> Self {
match s {
"allow" => Self::Allow,
"deny" => Self::Deny,
_ => unreachable!(),
}
use oxc_linter::{AllowWarnDeny, LintOptions};

pub use self::{command::lint_command, error::Error, runner::LintRunner};

pub fn matches_to_lint_options(matches: &ArgMatches) -> LintOptions {
let list_rules = matches.get_flag("rules");
LintOptions {
paths: matches.get_many("path").map_or_else(
|| if list_rules { vec![] } else { vec![PathBuf::from(".")] },
|paths| paths.into_iter().cloned().collect(),
),
rules: get_rules(matches),
fix: matches.get_flag("fix"),
quiet: matches.get_flag("quiet"),
ignore_path: matches
.get_one::<PathBuf>("ignore-path")
.map_or_else(|| PathBuf::from(".eslintignore"), Clone::clone),
no_ignore: matches.get_flag("no-ignore"),
ignore_pattern: matches
.get_many::<String>("ignore-pattern")
.map(|patterns| patterns.into_iter().cloned().collect())
.unwrap_or_default(),
max_warnings: matches.get_one("max-warnings").copied(),
list_rules,
}
}

impl<'a> From<&'a ArgMatches> for LintOptions {
fn from(matches: &'a ArgMatches) -> Self {
let list_rules = matches.get_flag("rules");

Self {
paths: matches.get_many("path").map_or_else(
|| if list_rules { vec![] } else { vec![PathBuf::from(".")] },
|paths| paths.into_iter().cloned().collect(),
),
rules: Self::get_rules(matches),
fix: matches.get_flag("fix"),
quiet: matches.get_flag("quiet"),
ignore_path: matches
.get_one::<PathBuf>("ignore-path")
.map_or_else(|| PathBuf::from(".eslintignore"), Clone::clone),
no_ignore: matches.get_flag("no-ignore"),
ignore_pattern: matches
.get_many::<String>("ignore-pattern")
.map(|patterns| patterns.into_iter().cloned().collect())
.unwrap_or_default(),
max_warnings: matches.get_one("max-warnings").copied(),
list_rules,
/// Get all rules in order, e.g.
/// `-A all -D no-var -D -eqeqeq` => [("allow", "all"), ("deny", "no-var"), ("deny", "eqeqeq")]
/// Defaults to [("deny", "correctness")];
fn get_rules(matches: &ArgMatches) -> Vec<(AllowWarnDeny, String)> {
let mut map: BTreeMap<usize, (AllowWarnDeny, String)> = BTreeMap::new();
for key in ["allow", "deny"] {
let allow_warn_deny = AllowWarnDeny::from(key);
if let Some(values) = matches.get_many::<String>(key) {
let indices = matches.indices_of(key).unwrap();
let zipped =
values.zip(indices).map(|(value, i)| (i, (allow_warn_deny, value.clone())));
map.extend(zipped);
}
}
}

impl LintOptions {
/// Get all rules in order, e.g.
/// `-A all -D no-var -D -eqeqeq` => [("allow", "all"), ("deny", "no-var"), ("deny", "eqeqeq")]
/// Defaults to [("deny", "correctness")];
fn get_rules(matches: &ArgMatches) -> Vec<(AllowWarnDeny, String)> {
let mut map: BTreeMap<usize, (AllowWarnDeny, String)> = BTreeMap::new();
for key in ["allow", "deny"] {
let allow_warn_deny = AllowWarnDeny::from(key);
if let Some(values) = matches.get_many::<String>(key) {
let indices = matches.indices_of(key).unwrap();
let zipped =
values.zip(indices).map(|(value, i)| (i, (allow_warn_deny, value.clone())));
map.extend(zipped);
}
}
if map.is_empty() {
vec![(AllowWarnDeny::Deny, "correctness".into())]
} else {
map.into_values().collect()
}
if map.is_empty() {
vec![(AllowWarnDeny::Deny, "correctness".into())]
} else {
map.into_values().collect()
}
}

Expand All @@ -99,8 +58,9 @@ mod test {
use std::path::PathBuf;

use clap::Command;
use oxc_linter::LintOptions;

use super::{lint_command, AllowWarnDeny, LintOptions};
use super::{lint_command, matches_to_lint_options, AllowWarnDeny};

#[test]
fn verify_command() {
Expand All @@ -110,7 +70,7 @@ mod test {
fn get_lint_options(arg: &str) -> LintOptions {
let matches =
lint_command(Command::new("oxc")).try_get_matches_from(arg.split(' ')).unwrap();
LintOptions::from(&matches)
matches_to_lint_options(&matches)
}

#[test]
Expand Down
Loading

0 comments on commit 1182477

Please sign in to comment.