@@ -2,6 +2,7 @@ use std::path::Path;
22
33use ast_grep_config:: { GlobalRules , RuleConfig , from_yaml_string} ;
44use ast_grep_language:: { LanguageExt , SupportLang } ;
5+ use json_strip_comments:: StripComments ;
56use serde_json:: Value ;
67use 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
0 commit comments