Skip to content

Commit

Permalink
feat(cli): add tsconfig file validation in LintRunner (#2850)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Mar 29, 2024
1 parent 76cc906 commit 2365198
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions crates/oxc_cli/fixtures/tsconfig/tsconfig.json
@@ -0,0 +1 @@
{}
31 changes: 31 additions & 0 deletions crates/oxc_cli/src/lint/mod.rs
Expand Up @@ -113,6 +113,15 @@ impl Runner for LintRunner {
}
};

if let Some(path) = tsconfig.as_ref() {
if !path.is_file() {
let path = if path.is_relative() { cwd.join(path) } else { path.clone() };
return CliRunResult::InvalidOptions {
message: format!("The tsconfig file {path:?} does not exist, Please provide a valid tsconfig file.", ),
};
}
}

let options = LintServiceOptions { cwd, paths, tsconfig };
let lint_service = LintService::new(linter, options);
let mut diagnostic_service =
Expand Down Expand Up @@ -173,6 +182,18 @@ mod test {
}
}

fn test_invalid_options(args: &[&str]) -> String {
let mut new_args = vec!["--quiet"];
new_args.extend(args);
let options = lint_command().run_inner(new_args.as_slice()).unwrap().lint_options;
match LintRunner::new(options).run() {
CliRunResult::InvalidOptions { message } => message,
other => {
panic!("Expected InvalidOptions, got {other:?}");
}
}
}

#[test]
fn no_arg() {
let args = &[];
Expand Down Expand Up @@ -402,4 +423,14 @@ mod test {
assert_eq!(result.number_of_warnings, 1);
assert_eq!(result.number_of_errors, 0);
}

#[test]
fn test_tsconfig_option() {
// passed
test(&["--tsconfig", "fixtures/tsconfig/tsconfig.json"]);

// failed
assert!(test_invalid_options(&["--tsconfig", "oxc/tsconfig.json"])
.contains("oxc/tsconfig.json\" does not exist, Please provide a valid tsconfig file."));
}
}
9 changes: 3 additions & 6 deletions crates/oxc_linter/src/service.rs
Expand Up @@ -150,17 +150,14 @@ impl Runtime {

fn get_resolver(tsconfig: Option<PathBuf>) -> Resolver {
use oxc_resolver::{ResolveOptions, TsconfigOptions, TsconfigReferences};
let tsconfig = if let Some(path) = tsconfig {
let tsconfig = tsconfig.and_then(|path| {
if path.is_file() {
Some(TsconfigOptions { config_file: path, references: TsconfigReferences::Auto })
} else {
// TODO: crates/oxc_cli/src/lint/mod.rs
eprintln!("Tsconfig {path:?} is not a file");
None
}
} else {
None
};
});

Resolver::new(ResolveOptions {
extensions: VALID_EXTENSIONS.iter().map(|ext| format!(".{ext}")).collect(),
condition_names: vec!["module".into(), "require".into()],
Expand Down

0 comments on commit 2365198

Please sign in to comment.