Skip to content

Commit 8aec218

Browse files
committed
feat(cli): support .ts config file for oxlint
1 parent 3cb78c3 commit 8aec218

1 file changed

Lines changed: 59 additions & 8 deletions

File tree

  • packages/cli/binding/src

packages/cli/binding/src/cli.rs

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,60 @@ impl SubcommandResolver {
231231
}
232232
}
233233

234-
/// Write a temporary config file and prepend `-c <path>` to args.
234+
/// Write a temporary TS config file that re-exports a field from vite.config.
235+
/// The temp file imports the vite config and re-exports the specified field,
236+
/// so the tool (e.g. oxlint) picks it up via `-c <path>`.
237+
/// The `config_file_path` must be an absolute path.
238+
async fn write_temp_ts_config_import(
239+
&mut self,
240+
config_file_path: &str,
241+
temp_filename: &str,
242+
field_name: &str,
243+
args: &mut Vec<String>,
244+
) -> anyhow::Result<()> {
245+
let path = PathBuf::from(config_file_path);
246+
if !path.is_absolute() {
247+
anyhow::bail!("config_file_path must be an absolute path, got: {config_file_path}");
248+
}
249+
250+
let config_basename = path
251+
.file_name()
252+
.and_then(|n| n.to_str())
253+
.ok_or_else(|| {
254+
anyhow::anyhow!("Failed to get file name of config file: {config_file_path}")
255+
})?
256+
.to_string();
257+
258+
let config_dir = AbsolutePathBuf::new(path)
259+
.and_then(|p| p.parent().map(|p| p.to_absolute_path_buf()))
260+
.ok_or_else(|| {
261+
anyhow::anyhow!("Failed to get parent directory of config file: {config_file_path}")
262+
})?;
263+
264+
let config_path = config_dir.join(temp_filename);
265+
let content = format!(
266+
"import {{ defineConfig }} from 'oxlint';\nimport viteConfig from './{config_basename}';\nexport default defineConfig(viteConfig.{field_name} as any);\n"
267+
);
268+
write(&config_path, content).await?;
269+
270+
self.temp_config_files.push(config_path.clone());
271+
272+
let config_path_str = config_path
273+
.as_path()
274+
.to_str()
275+
.ok_or_else(|| anyhow::anyhow!("config path is not valid UTF-8"))?;
276+
args.insert(0, config_path_str.to_string());
277+
args.insert(0, "-c".to_string());
278+
// Prevent oxlint from linting the temp config file itself
279+
args.push("--ignore-pattern".to_string());
280+
args.push(temp_filename.to_string());
281+
Ok(())
282+
}
283+
284+
/// Write a temporary JSON config file and prepend `-c <path>` to args.
235285
/// The file will be tracked for cleanup after command execution.
236286
/// The `config_file_path` must be an absolute path.
237-
async fn write_temp_config_file(
287+
async fn write_temp_json_config_file(
238288
&mut self,
239289
config: &serde_json::Value,
240290
config_file_path: &str,
@@ -311,21 +361,22 @@ impl SubcommandResolver {
311361
tracing::error!("Failed to parse vite config: {vite_config_json}");
312362
})?;
313363

314-
if let (Some(lint_config), Some(config_file)) =
364+
if let (Some(_), Some(config_file)) =
315365
(&resolved_vite_config.lint, &resolved_vite_config.config_file)
316366
{
317-
self.write_temp_config_file(
318-
lint_config,
367+
self.write_temp_ts_config_import(
319368
config_file,
320-
".vite-plus-lint.tmp.json",
369+
".vite-plus-lint.tmp.mts",
370+
"lint",
321371
&mut args,
322372
)
323373
.await?;
324374
}
325375

326376
Ok(ResolvedSubcommand {
327377
program: Arc::from(OsStr::new("node")),
328-
args: iter::once(Str::from(js_path_str))
378+
args: iter::once(Str::from("--disable-warning=MODULE_TYPELESS_PACKAGE_JSON"))
379+
.chain(iter::once(Str::from(js_path_str)))
329380
.chain(args.into_iter().map(Str::from))
330381
.collect(),
331382
cache_config: UserCacheConfig::with_config(EnabledCacheConfig {
@@ -362,7 +413,7 @@ impl SubcommandResolver {
362413
if let (Some(fmt_config), Some(config_file)) =
363414
(&resolved_vite_config.fmt, &resolved_vite_config.config_file)
364415
{
365-
self.write_temp_config_file(
416+
self.write_temp_json_config_file(
366417
fmt_config,
367418
config_file,
368419
".vite-plus-fmt.tmp.json",

0 commit comments

Comments
 (0)