Skip to content

Commit a74085e

Browse files
authored
Support --indentation in the fmt and lint commands (#468)
See: getmanfred/mac#46 Signed-off-by: Juan Cruz Viotti <[email protected]>
1 parent 8c48f48 commit a74085e

12 files changed

+175
-8
lines changed

docs/format.markdown

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Formatting
55
jsonschema fmt [schemas-or-directories...]
66
[--check/-c] [--verbose/-v] [--extension/-e <extension>]
77
[--ignore/-i <schemas-or-directories>] [--keep-ordering/-k]
8+
[--indentation/-n <spaces>]
89
```
910

1011
Schemas are code. As such, they are expected follow consistent stylistic
@@ -54,6 +55,12 @@ jsonschema fmt path/to/my/schema_1.json path/to/my/schema_2.json
5455
jsonschema fmt path/to/my/schema_1.json path/to/my/schema_2.json --keep-ordering
5556
```
5657

58+
### Format JSON Schemas in-place while indenting on 4 spaces
59+
60+
```sh
61+
jsonschema fmt path/to/my/schema_1.json path/to/my/schema_2.json --indentation 4
62+
```
63+
5764
### Format every `.json` file in a given directory (recursively)
5865

5966
```sh

docs/lint.markdown

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jsonschema lint [schemas-or-directories...] [--http/-h] [--fix/-f]
66
[--json/-j] [--verbose/-v] [--resolve/-r <schemas-or-directories> ...]
77
[--extension/-e <extension>] [--ignore/-i <schemas-or-directories>]
88
[--exclude/-x <rule-name>] [--only/-o <rule-name>] [--list/-l]
9-
[--default-dialect/-d <uri>] [--strict/-s]
9+
[--default-dialect/-d <uri>] [--strict/-s] [--indentation/-n <spaces>]
1010
```
1111

1212
JSON Schema is a surprisingly expressive schema language. Like with traditional
@@ -133,6 +133,12 @@ jsonschema lint --extension .schema.json
133133
jsonschema lint path/to/my/schema.json --fix
134134
```
135135

136+
### Fix lint warnings on a single schema while indenting on 4 spaces
137+
138+
```sh
139+
jsonschema lint path/to/my/schema.json --fix --indentation 4
140+
```
141+
136142
### Fix lint warnings on a single schema while preserving keyword ordering
137143

138144
```sh

src/command_fmt.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
auto sourcemeta::jsonschema::cli::fmt(const sourcemeta::core::Options &options)
1313
-> int {
14+
const auto indentation{parse_indentation(options)};
1415
for (const auto &entry :
1516
for_each_json(options.positional(), parse_ignore(options),
1617
parse_extensions(options))) {
@@ -28,10 +29,11 @@ auto sourcemeta::jsonschema::cli::fmt(const sourcemeta::core::Options &options)
2829
std::ostringstream expected;
2930

3031
if (options.contains("keep-ordering")) {
31-
sourcemeta::core::prettify(entry.second, expected);
32+
sourcemeta::core::prettify(entry.second, expected, indentation);
3233
} else {
3334
sourcemeta::core::prettify(entry.second, expected,
34-
sourcemeta::core::schema_format_compare);
35+
sourcemeta::core::schema_format_compare,
36+
indentation);
3537
}
3638

3739
expected << "\n";
@@ -50,10 +52,11 @@ auto sourcemeta::jsonschema::cli::fmt(const sourcemeta::core::Options &options)
5052
std::ofstream output{entry.first};
5153

5254
if (options.contains("keep-ordering")) {
53-
sourcemeta::core::prettify(entry.second, output);
55+
sourcemeta::core::prettify(entry.second, output, indentation);
5456
} else {
5557
sourcemeta::core::prettify(entry.second, output,
56-
sourcemeta::core::schema_format_compare);
58+
sourcemeta::core::schema_format_compare,
59+
indentation);
5760
}
5861

5962
output << "\n";

src/command_lint.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ auto sourcemeta::jsonschema::cli::lint(const sourcemeta::core::Options &options)
192192
auto errors_array = sourcemeta::core::JSON::make_array();
193193
std::vector<std::uint8_t> scores;
194194
const auto dialect{default_dialect(options)};
195+
const auto indentation{parse_indentation(options)};
195196
const auto custom_resolver{
196197
resolver(options, options.contains("http"), dialect)};
197198

@@ -243,7 +244,7 @@ auto sourcemeta::jsonschema::cli::lint(const sourcemeta::core::Options &options)
243244
if (wrapper_result == EXIT_SUCCESS) {
244245
if (copy != entry.second) {
245246
std::ofstream output{entry.first};
246-
sourcemeta::core::prettify(copy, output);
247+
sourcemeta::core::prettify(copy, output, indentation);
247248
output << "\n";
248249
}
249250
} else {
@@ -298,7 +299,7 @@ auto sourcemeta::jsonschema::cli::lint(const sourcemeta::core::Options &options)
298299
}
299300

300301
output_json_object.assign("errors", sourcemeta::core::JSON{errors_array});
301-
sourcemeta::core::prettify(output_json_object, std::cout);
302+
sourcemeta::core::prettify(output_json_object, std::cout, indentation);
302303
std::cout << "\n";
303304
}
304305

src/main.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,22 @@ Global Options:
6969
7070
fmt [schemas-or-directories...] [--check/-c] [--extension/-e <extension>]
7171
[--ignore/-i <schemas-or-directories>] [--keep-ordering/-k]
72+
[--indentation/-n <spaces>]
7273
7374
Format the input schemas in-place or check they are formatted.
7475
This command does not support YAML schemas yet.
7576
7677
lint [schemas-or-directories...] [--fix/-f] [--json/-j]
7778
[--extension/-e <extension>] [--ignore/-i <schemas-or-directories>]
7879
[--exclude/-x <rule-name>] [--only/-o <rule-name>] [--list/-l]
79-
[--strict/-s]
80+
[--strict/-s] [--indentation/-n <spaces>]
8081
8182
Lint the input schemas and potentially fix the reported issues.
8283
The --fix/-f option is not supported when passing YAML schemas.
8384
Use --json/-j to output lint errors in JSON.
8485
Use --list/-l to print a summary of all enabled rules.
8586
Use --strict/-s to enable additional opinionated strict rules.
87+
Use --indentation/-n to keep indentation when auto-fixing
8688
8789
bundle <schema.json|.yaml> [--http/-h] [--extension/-e <extension>]
8890
[--ignore/-i <schemas-or-directories>] [--without-id/-w]
@@ -120,6 +122,7 @@ auto jsonschema_main(const std::string &program, const std::string &command,
120122
app.flag("keep-ordering", {"k"});
121123
app.option("extension", {"e"});
122124
app.option("ignore", {"i"});
125+
app.option("indentation", {"n"});
123126
app.parse(argc, argv, {.skip = 1});
124127
return sourcemeta::jsonschema::cli::fmt(app);
125128
} else if (command == "inspect") {
@@ -141,6 +144,7 @@ auto jsonschema_main(const std::string &program, const std::string &command,
141144
app.option("exclude", {"x"});
142145
app.option("only", {"o"});
143146
app.option("ignore", {"i"});
147+
app.option("indentation", {"n"});
144148
app.parse(argc, argv, {.skip = 1});
145149
return sourcemeta::jsonschema::cli::lint(app);
146150
} else if (command == "validate") {

src/utils.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,13 @@ auto default_dialect(const sourcemeta::core::Options &options)
330330
return std::nullopt;
331331
}
332332

333+
auto parse_indentation(const sourcemeta::core::Options &options)
334+
-> std::size_t {
335+
if (options.contains("indentation")) {
336+
return std::stoull(std::string{options.at("indentation").front()});
337+
}
338+
339+
return 2;
340+
}
341+
333342
} // namespace sourcemeta::jsonschema::cli

src/utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ auto parse_ignore(const sourcemeta::core::Options &options)
7676
auto default_dialect(const sourcemeta::core::Options &options)
7777
-> std::optional<std::string>;
7878

79+
auto parse_indentation(const sourcemeta::core::Options &options) -> std::size_t;
80+
7981
} // namespace sourcemeta::jsonschema::cli
8082

8183
#endif

test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ add_jsonschema_test_unix(format_check_single_pass_keep_ordering)
3737
add_jsonschema_test_unix(format_directory_ignore_directory)
3838
add_jsonschema_test_unix(format_directory_ignore_file)
3939
add_jsonschema_test_unix(format_check_single_invalid)
40+
add_jsonschema_test_unix(format_check_single_fail_indentation)
41+
add_jsonschema_test_unix(format_check_single_pass_indentation)
42+
add_jsonschema_test_unix(format_single_indentation)
4043

4144
# Validate
4245
add_jsonschema_test_unix(validate/fail_instance_directory)
@@ -255,6 +258,7 @@ add_jsonschema_test_unix(lint/fail_lint_default)
255258
add_jsonschema_test_unix(lint/pass_lint_json)
256259
add_jsonschema_test_unix(lint/pass_lint_fix_json)
257260
add_jsonschema_test_unix(lint/pass_lint_fix_strict)
261+
add_jsonschema_test_unix(lint/pass_lint_fix_indentation)
258262
add_jsonschema_test_unix(lint/pass_lint_ignore_short)
259263
add_jsonschema_test_unix(lint/pass_lint_ignore_long)
260264
add_jsonschema_test_unix(lint/pass_lint_examples_fix)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/sh
2+
3+
set -o errexit
4+
set -o nounset
5+
6+
TMP="$(mktemp -d)"
7+
clean() { rm -rf "$TMP"; }
8+
trap clean EXIT
9+
10+
cat << 'EOF' > "$TMP/schema.json"
11+
{
12+
"$schema": "http://json-schema.org/draft-04/schema#",
13+
"type": 1
14+
}
15+
EOF
16+
17+
"$1" fmt "$TMP/schema.json" --indentation 4 --check 2>"$TMP/stderr.txt" && CODE="$?" || CODE="$?"
18+
test "$CODE" = "1" || exit 1
19+
20+
cat << EOF > "$TMP/error.txt"
21+
FAIL: $(realpath "$TMP")/schema.json
22+
Got:
23+
{
24+
"\$schema": "http://json-schema.org/draft-04/schema#",
25+
"type": 1
26+
}
27+
28+
But expected:
29+
{
30+
"\$schema": "http://json-schema.org/draft-04/schema#",
31+
"type": 1
32+
}
33+
34+
EOF
35+
36+
diff "$TMP/stderr.txt" "$TMP/error.txt"
37+
38+
cat << 'EOF' > "$TMP/expected.json"
39+
{
40+
"$schema": "http://json-schema.org/draft-04/schema#",
41+
"type": 1
42+
}
43+
EOF
44+
45+
diff "$TMP/schema.json" "$TMP/expected.json"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/sh
2+
3+
set -o errexit
4+
set -o nounset
5+
6+
TMP="$(mktemp -d)"
7+
clean() { rm -rf "$TMP"; }
8+
trap clean EXIT
9+
10+
cat << 'EOF' > "$TMP/schema.json"
11+
{
12+
"$schema": "http://json-schema.org/draft-04/schema#",
13+
"type": 1
14+
}
15+
EOF
16+
17+
"$1" fmt "$TMP/schema.json" --check --indentation 4
18+
19+
cat << 'EOF' > "$TMP/expected.json"
20+
{
21+
"$schema": "http://json-schema.org/draft-04/schema#",
22+
"type": 1
23+
}
24+
EOF
25+
26+
diff "$TMP/schema.json" "$TMP/expected.json"

0 commit comments

Comments
 (0)