Skip to content

Commit 5ee24d5

Browse files
committed
feat(oxfmt): Provide JSON schema for .oxfmtrc.json
1 parent f0c19eb commit 5ee24d5

File tree

7 files changed

+310
-2
lines changed

7 files changed

+310
-2
lines changed

Cargo.lock

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

crates/oxc_formatter/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ oxc_syntax = { workspace = true }
2929
cow-utils = { workspace = true }
3030
json-strip-comments = { workspace = true }
3131
rustc-hash = { workspace = true }
32+
schemars = { workspace = true }
3233
serde = { workspace = true, features = ["derive"] }
3334
serde_json = { workspace = true }
3435
unicode-width = "0.2"
3536

3637
[dev-dependencies]
38+
insta = { workspace = true }
3739
oxc_parser = { workspace = true }
3840
pico-args = { workspace = true }
41+
project-root = { workspace = true }

crates/oxc_formatter/src/service/oxfmtrc.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::path::Path;
22

3+
use schemars::JsonSchema;
34
use serde::{Deserialize, Serialize};
45

56
use crate::{
@@ -8,7 +9,7 @@ use crate::{
89
Semicolons, SortImports, SortOrder, TrailingCommas,
910
};
1011

11-
#[derive(Debug, Default, Clone, Deserialize, Serialize)]
12+
#[derive(Debug, Default, Clone, Deserialize, Serialize, JsonSchema)]
1213
#[serde(rename_all = "camelCase", default)]
1314
pub struct Oxfmtrc {
1415
#[serde(skip_serializing_if = "Option::is_none")]
@@ -45,7 +46,7 @@ pub struct Oxfmtrc {
4546
pub experimental_sort_imports: Option<SortImportsConfig>,
4647
}
4748

48-
#[derive(Debug, Clone, Deserialize, Serialize)]
49+
#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
4950
#[serde(rename_all = "camelCase", default)]
5051
pub struct SortImportsConfig {
5152
#[serde(default)]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::fs;
2+
3+
use oxc_formatter::Oxfmtrc;
4+
use project_root::get_project_root;
5+
6+
// NOTE: This test generates the JSON schema for the `.oxfmtrc.json` configuration file
7+
8+
#[test]
9+
fn test_schema_json() {
10+
let path = get_project_root().unwrap().join("npm/oxfmt/configuration_schema.json");
11+
let schema = schemars::schema_for!(Oxfmtrc);
12+
let json = serde_json::to_string_pretty(&schema).unwrap();
13+
let existing_json = fs::read_to_string(&path).unwrap_or_default();
14+
if existing_json.trim() != json.trim() {
15+
std::fs::write(&path, &json).unwrap();
16+
}
17+
insta::with_settings!({ prepend_module_to_snapshot => false }, {
18+
insta::assert_snapshot!(json);
19+
});
20+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
---
2+
source: crates/oxc_formatter/tests/schema.rs
3+
expression: json
4+
---
5+
{
6+
"$schema": "http://json-schema.org/draft-07/schema#",
7+
"title": "Oxfmtrc",
8+
"type": "object",
9+
"properties": {
10+
"arrowParentheses": {
11+
"type": [
12+
"string",
13+
"null"
14+
]
15+
},
16+
"attributePosition": {
17+
"type": [
18+
"string",
19+
"null"
20+
]
21+
},
22+
"bracketSameLine": {
23+
"type": [
24+
"boolean",
25+
"null"
26+
]
27+
},
28+
"bracketSpacing": {
29+
"type": [
30+
"boolean",
31+
"null"
32+
]
33+
},
34+
"expand": {
35+
"type": [
36+
"string",
37+
"null"
38+
]
39+
},
40+
"experimentalOperatorPosition": {
41+
"type": [
42+
"string",
43+
"null"
44+
]
45+
},
46+
"experimentalSortImports": {
47+
"anyOf": [
48+
{
49+
"$ref": "#/definitions/SortImportsConfig"
50+
},
51+
{
52+
"type": "null"
53+
}
54+
]
55+
},
56+
"indentStyle": {
57+
"type": [
58+
"string",
59+
"null"
60+
]
61+
},
62+
"indentWidth": {
63+
"type": [
64+
"integer",
65+
"null"
66+
],
67+
"format": "uint8",
68+
"minimum": 0.0
69+
},
70+
"jsxQuoteStyle": {
71+
"type": [
72+
"string",
73+
"null"
74+
]
75+
},
76+
"lineEnding": {
77+
"type": [
78+
"string",
79+
"null"
80+
]
81+
},
82+
"lineWidth": {
83+
"type": [
84+
"integer",
85+
"null"
86+
],
87+
"format": "uint16",
88+
"minimum": 0.0
89+
},
90+
"quoteProperties": {
91+
"type": [
92+
"string",
93+
"null"
94+
]
95+
},
96+
"quoteStyle": {
97+
"type": [
98+
"string",
99+
"null"
100+
]
101+
},
102+
"semicolons": {
103+
"type": [
104+
"string",
105+
"null"
106+
]
107+
},
108+
"trailingCommas": {
109+
"type": [
110+
"string",
111+
"null"
112+
]
113+
}
114+
},
115+
"definitions": {
116+
"SortImportsConfig": {
117+
"type": "object",
118+
"properties": {
119+
"ignoreCase": {
120+
"default": true,
121+
"type": "boolean"
122+
},
123+
"order": {
124+
"default": "asc",
125+
"type": "string"
126+
},
127+
"partitionByComment": {
128+
"default": false,
129+
"type": "boolean"
130+
},
131+
"partitionByNewline": {
132+
"default": false,
133+
"type": "boolean"
134+
},
135+
"sortSideEffects": {
136+
"default": false,
137+
"type": "boolean"
138+
}
139+
}
140+
}
141+
}
142+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "Oxfmtrc",
4+
"type": "object",
5+
"properties": {
6+
"arrowParentheses": {
7+
"type": [
8+
"string",
9+
"null"
10+
]
11+
},
12+
"attributePosition": {
13+
"type": [
14+
"string",
15+
"null"
16+
]
17+
},
18+
"bracketSameLine": {
19+
"type": [
20+
"boolean",
21+
"null"
22+
]
23+
},
24+
"bracketSpacing": {
25+
"type": [
26+
"boolean",
27+
"null"
28+
]
29+
},
30+
"expand": {
31+
"type": [
32+
"string",
33+
"null"
34+
]
35+
},
36+
"experimentalOperatorPosition": {
37+
"type": [
38+
"string",
39+
"null"
40+
]
41+
},
42+
"experimentalSortImports": {
43+
"anyOf": [
44+
{
45+
"$ref": "#/definitions/SortImportsConfig"
46+
},
47+
{
48+
"type": "null"
49+
}
50+
]
51+
},
52+
"indentStyle": {
53+
"type": [
54+
"string",
55+
"null"
56+
]
57+
},
58+
"indentWidth": {
59+
"type": [
60+
"integer",
61+
"null"
62+
],
63+
"format": "uint8",
64+
"minimum": 0.0
65+
},
66+
"jsxQuoteStyle": {
67+
"type": [
68+
"string",
69+
"null"
70+
]
71+
},
72+
"lineEnding": {
73+
"type": [
74+
"string",
75+
"null"
76+
]
77+
},
78+
"lineWidth": {
79+
"type": [
80+
"integer",
81+
"null"
82+
],
83+
"format": "uint16",
84+
"minimum": 0.0
85+
},
86+
"quoteProperties": {
87+
"type": [
88+
"string",
89+
"null"
90+
]
91+
},
92+
"quoteStyle": {
93+
"type": [
94+
"string",
95+
"null"
96+
]
97+
},
98+
"semicolons": {
99+
"type": [
100+
"string",
101+
"null"
102+
]
103+
},
104+
"trailingCommas": {
105+
"type": [
106+
"string",
107+
"null"
108+
]
109+
}
110+
},
111+
"definitions": {
112+
"SortImportsConfig": {
113+
"type": "object",
114+
"properties": {
115+
"ignoreCase": {
116+
"default": true,
117+
"type": "boolean"
118+
},
119+
"order": {
120+
"default": "asc",
121+
"type": "string"
122+
},
123+
"partitionByComment": {
124+
"default": false,
125+
"type": "boolean"
126+
},
127+
"partitionByNewline": {
128+
"default": false,
129+
"type": "boolean"
130+
},
131+
"sortSideEffects": {
132+
"default": false,
133+
"type": "boolean"
134+
}
135+
}
136+
}
137+
}
138+
}

npm/oxfmt/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
},
2525
"files": [
2626
"bin/oxfmt",
27+
"configuration_schema.json",
2728
"README.md"
2829
]
2930
}

0 commit comments

Comments
 (0)