-
-
Notifications
You must be signed in to change notification settings - Fork 6
Linter: add rule to remove empty then else #1819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
1e6758a
50a9ca0
1b78bd4
5500fbe
eef6cf9
9392d12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class ElseEmpty final : public SchemaTransformRule { | ||
public: | ||
ElseEmpty() | ||
: SchemaTransformRule{"else_empty", | ||
"Setting the `else` keyword to the empty schema " | ||
"does not add any further constraint"} {}; | ||
|
||
[[nodiscard]] auto | ||
condition(const JSON &schema, const JSON &, const Vocabularies &vocabularies, | ||
const SchemaFrame &, const SchemaFrame::Location &, | ||
const SchemaWalker &, const SchemaResolver &) const | ||
-> SchemaTransformRule::Result override { | ||
return contains_any( | ||
vocabularies, | ||
{"https://json-schema.org/draft/2020-12/vocab/applicator", | ||
"https://json-schema.org/draft/2019-09/vocab/applicator", | ||
"http://json-schema.org/draft-07/schema#"}) && | ||
schema.is_object() && schema.defines("if") && | ||
schema.defines("else") && is_schema(schema.at("else")) && | ||
((schema.at("else").is_object() && schema.at("else").empty()) || | ||
(schema.at("else").is_boolean() && schema.at("else").to_boolean() && | ||
!(schema.at("if").is_boolean() && schema.at("if").to_boolean()))); | ||
} | ||
|
||
auto transform(JSON &schema) const -> void override { schema.erase("else"); } | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
class ThenEmpty final : public SchemaTransformRule { | ||
public: | ||
ThenEmpty() | ||
: SchemaTransformRule{"then_empty", | ||
"Setting the `then` keyword to the empty schema " | ||
"does not add any further constraint"} {}; | ||
|
||
[[nodiscard]] auto | ||
condition(const JSON &schema, const JSON &, const Vocabularies &vocabularies, | ||
const SchemaFrame &, const SchemaFrame::Location &, | ||
const SchemaWalker &, const SchemaResolver &) const | ||
-> SchemaTransformRule::Result override { | ||
return contains_any( | ||
vocabularies, | ||
{"https://json-schema.org/draft/2020-12/vocab/applicator", | ||
"https://json-schema.org/draft/2019-09/vocab/applicator", | ||
"http://json-schema.org/draft-07/schema#"}) && | ||
schema.is_object() && schema.defines("if") && | ||
schema.defines("then") && is_schema(schema.at("then")) && | ||
((schema.at("then").is_object() && schema.at("then").empty()) || | ||
(schema.at("then").is_boolean() && schema.at("then").to_boolean() && | ||
!(schema.at("if").is_boolean() && schema.at("if").to_boolean()))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this line really necessary? I guess the rule applies independently of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, is it to avoid conflicts with the other rule that removes dangling |
||
} | ||
|
||
auto transform(JSON &schema) const -> void override { schema.erase("then"); } | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as below