diff --git a/crates/oxc_cli/fixtures/tsconfig/tsconfig.json b/crates/oxc_cli/fixtures/tsconfig/tsconfig.json new file mode 100644 index 000000000000..9e26dfeeb6e6 --- /dev/null +++ b/crates/oxc_cli/fixtures/tsconfig/tsconfig.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/crates/oxc_cli/src/lint/mod.rs b/crates/oxc_cli/src/lint/mod.rs index 11795cde9a16..8b63afe94c2c 100644 --- a/crates/oxc_cli/src/lint/mod.rs +++ b/crates/oxc_cli/src/lint/mod.rs @@ -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 = @@ -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 = &[]; @@ -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.")); + } } diff --git a/crates/oxc_linter/src/service.rs b/crates/oxc_linter/src/service.rs index 3aef6d03e83e..8203ec2c7de7 100644 --- a/crates/oxc_linter/src/service.rs +++ b/crates/oxc_linter/src/service.rs @@ -150,17 +150,14 @@ impl Runtime { fn get_resolver(tsconfig: Option) -> 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()],