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>
This commit is contained in:
Tapan Prakash 2025-01-22 06:39:39 +05:30 committed by GitHub
parent 3e19e4e004
commit dc912fa58e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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(),