Skip to content

Commit f874e35

Browse files
fengmk2claude
andcommitted
feat(migration): add JSONC format support to merge_json_config
Use json-strip-comments crate to strip comments before parsing, enabling support for .jsonc config files with // and /* */ comments. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent b462d4d commit f874e35

5 files changed

Lines changed: 120 additions & 3 deletions

File tree

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ hex = "0.4.3"
5050
httpmock = "0.7"
5151
ignore = "0.4"
5252
indoc = "2.0.5"
53+
json-strip-comments = "3.1"
5354
napi = { version = "3.0.0", default-features = false, features = ["async", "error_anyhow"] }
5455
napi-build = "2"
5556
napi-derive = { version = "3.0.0", default-features = false, features = ["type-def", "strict"] }

crates/vite_migration/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ast-grep-config = { workspace = true }
1111
ast-grep-core = { workspace = true }
1212
ast-grep-language = { workspace = true }
1313
ignore = { workspace = true }
14+
json-strip-comments = { workspace = true }
1415
serde_json = { workspace = true, features = ["preserve_order"] }
1516
vite_error = { workspace = true }
1617

crates/vite_migration/src/vite_config.rs

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::path::Path;
22

33
use ast_grep_config::{GlobalRules, RuleConfig, from_yaml_string};
44
use ast_grep_language::{LanguageExt, SupportLang};
5+
use json_strip_comments::StripComments;
56
use serde_json::Value;
67
use vite_error::Error;
78

@@ -71,9 +72,10 @@ pub fn merge_json_config(
7172
// Read the vite config file
7273
let vite_config_content = std::fs::read_to_string(vite_config_path)?;
7374

74-
// Read and parse the JSON config file
75+
// Read and parse the JSON/JSONC config file (supports comments)
7576
let json_config_content = std::fs::read_to_string(json_config_path)?;
76-
let json_config: Value = serde_json::from_str(&json_config_content)?;
77+
let json_without_comments = StripComments::new(json_config_content.as_bytes());
78+
let json_config: Value = serde_json::from_reader(json_without_comments)?;
7779

7880
// Convert JSON to TypeScript object literal
7981
let ts_config = json_to_js_object_literal(&json_config, 0);
@@ -993,6 +995,110 @@ export default defineConfig({
993995
);
994996
}
995997

998+
#[test]
999+
fn test_merge_json_config_with_jsonc_file() {
1000+
// Test JSONC support with single-line and block comments
1001+
let temp_dir = tempdir().unwrap();
1002+
1003+
let vite_config_path = temp_dir.path().join("vite.config.ts");
1004+
let jsonc_config_path = temp_dir.path().join(".oxfmtrc.jsonc");
1005+
1006+
// Write test vite config
1007+
let mut vite_file = std::fs::File::create(&vite_config_path).unwrap();
1008+
write!(
1009+
vite_file,
1010+
r#"import {{ defineConfig }} from 'vite';
1011+
1012+
export default defineConfig({{
1013+
plugins: [],
1014+
}});"#
1015+
)
1016+
.unwrap();
1017+
1018+
// Write test JSONC config with comments
1019+
let mut jsonc_file = std::fs::File::create(&jsonc_config_path).unwrap();
1020+
write!(
1021+
jsonc_file,
1022+
r#"{{
1023+
// Formatting options
1024+
"indentWidth": 2,
1025+
/*
1026+
* Line width configuration
1027+
*/
1028+
"lineWidth": 100
1029+
}}"#
1030+
)
1031+
.unwrap();
1032+
1033+
// Run the merge
1034+
let result = merge_json_config(&vite_config_path, &jsonc_config_path, "fmt").unwrap();
1035+
1036+
// Verify the result - comments should be stripped
1037+
assert!(result.updated);
1038+
assert_eq!(
1039+
result.content,
1040+
r#"import { defineConfig } from 'vite';
1041+
1042+
export default defineConfig({
1043+
fmt: {
1044+
indentWidth: 2,
1045+
lineWidth: 100,
1046+
},
1047+
plugins: [],
1048+
});"#
1049+
);
1050+
}
1051+
1052+
#[test]
1053+
fn test_merge_json_config_with_inline_comments() {
1054+
// Test JSONC with inline comments
1055+
let temp_dir = tempdir().unwrap();
1056+
1057+
let vite_config_path = temp_dir.path().join("vite.config.ts");
1058+
let jsonc_config_path = temp_dir.path().join(".oxlintrc.jsonc");
1059+
1060+
let mut vite_file = std::fs::File::create(&vite_config_path).unwrap();
1061+
write!(
1062+
vite_file,
1063+
r#"import {{ defineConfig }} from 'vite';
1064+
1065+
export default defineConfig({{
1066+
plugins: [],
1067+
}});"#
1068+
)
1069+
.unwrap();
1070+
1071+
// JSONC with inline comments
1072+
let mut jsonc_file = std::fs::File::create(&jsonc_config_path).unwrap();
1073+
write!(
1074+
jsonc_file,
1075+
r#"{{
1076+
"rules": {{
1077+
"no-console": "warn" // warn about console.log usage
1078+
}}
1079+
}}"#
1080+
)
1081+
.unwrap();
1082+
1083+
let result = merge_json_config(&vite_config_path, &jsonc_config_path, "lint").unwrap();
1084+
1085+
// Verify the result - inline comments should be stripped
1086+
assert!(result.updated);
1087+
assert_eq!(
1088+
result.content,
1089+
r#"import { defineConfig } from 'vite';
1090+
1091+
export default defineConfig({
1092+
lint: {
1093+
rules: {
1094+
'no-console': 'warn',
1095+
},
1096+
},
1097+
plugins: [],
1098+
});"#
1099+
);
1100+
}
1101+
9961102
#[test]
9971103
fn test_full_json_to_js_object_literal_conversion() {
9981104
// Test a realistic .oxlintrc config

packages/global/src/migration/migrator.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,6 @@ function mergeViteConfigFiles(projectPath: string): void {
648648
mergeAndRemoveJsonConfig(projectPath, viteConfig, configs.oxlintConfig, 'lint');
649649
}
650650
if (configs.oxfmtConfig) {
651-
// TODO: handle jsonc file
652651
// merge oxfmt config into vite.config.ts
653652
mergeAndRemoveJsonConfig(projectPath, viteConfig, configs.oxfmtConfig, 'fmt');
654653
}

0 commit comments

Comments
 (0)