From dc912fa58e661707c2ef4caad1298f8fdd040a40 Mon Sep 17 00:00:00 2001 From: Tapan Prakash Date: Wed, 22 Jan 2025 06:39:39 +0530 Subject: [PATCH] fix(linter): Added missing $schema property to default config (#8625) The $schema property was not added when the --init command was used to create the configuration. Now, $schema is added based on the availability of configuration_schema.json in the current working directory. --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- apps/oxlint/src/lint.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/apps/oxlint/src/lint.rs b/apps/oxlint/src/lint.rs index 62d6fbd96..8b1c29621 100644 --- a/apps/oxlint/src/lint.rs +++ b/apps/oxlint/src/lint.rs @@ -12,6 +12,7 @@ use oxc_linter::{ LintFilter, LintOptions, LintService, LintServiceOptions, Linter, Oxlintrc, }; use oxc_span::VALID_EXTENSIONS; +use serde_json::Value; use crate::{ cli::{CliRunResult, LintCommand, LintResult, MiscOptions, Runner, WarningOptions}, @@ -132,7 +133,23 @@ impl Runner for LintRunner { if misc_options.print_config { return CliRunResult::PrintConfigResult { config_file }; } else if basic_options.init { - match fs::write(Self::DEFAULT_OXLINTRC, config_file) { + let schema_relative_path = "node_modules/oxlint/configuration_schema.json"; + let configuration = if self.cwd.join(schema_relative_path).is_file() { + let mut config_json: Value = serde_json::from_str(&config_file).unwrap(); + if let Value::Object(ref mut obj) = config_json { + let mut json_object = serde_json::Map::new(); + json_object.insert( + "$schema".to_string(), + format!("./{schema_relative_path}").into(), + ); + json_object.extend(obj.clone()); + *obj = json_object; + } + serde_json::to_string_pretty(&config_json).unwrap() + } else { + config_file + }; + match fs::write(Self::DEFAULT_OXLINTRC, configuration) { Ok(()) => { return CliRunResult::ConfigFileInitResult { message: "Configuration file created".to_string(),