-
-
Notifications
You must be signed in to change notification settings - Fork 7
Linter: create rules to add syntaxtical sugar when else is set to false #1897
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
a8edf4c
43690c0
8da8754
a50b582
75df3aa
31f7e34
515f4e9
da42406
ebdaaa0
a65eb14
b0c7520
ce70451
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,54 @@ | ||
class ElseFalse final : public SchemaTransformRule { | ||
public: | ||
ElseFalse() | ||
: SchemaTransformRule{"else_false", | ||
"`if: S, else: false` collapses to just `S`"} {}; | ||
|
||
[[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") && | ||
Karan-Palan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
schema.defines("else") && is_schema(schema.at("else")) && | ||
schema.at("else").is_boolean() && !schema.at("else").to_boolean() && | ||
is_schema(schema.at("if")) && | ||
(!schema.defines("then") || (schema.at("then").is_boolean() && | ||
schema.at("then").to_boolean())) && | ||
(!schema.at("if").is_object() || [&schema](const JSON &if_schema) { | ||
for (const auto &entry : if_schema.as_object()) { | ||
if (schema.defines(entry.first)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}(schema.at("if"))); | ||
} | ||
|
||
auto transform(JSON &schema) const -> void override { | ||
auto if_schema = schema.at("if"); | ||
schema.erase("if"); | ||
Karan-Palan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
schema.erase("else"); | ||
schema.erase("then"); | ||
|
||
if (if_schema.is_object()) { | ||
for (auto &entry : if_schema.as_object()) { | ||
schema.assign(entry.first, entry.second); | ||
} | ||
} else if (if_schema.is_boolean() && !if_schema.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. I think this branch will be problematic. You are only keeping certain keywords like |
||
auto metadata = JSON::make_object(); | ||
for (const auto &entry : schema.as_object()) { | ||
if (entry.first.starts_with("$")) { | ||
metadata.assign(entry.first, entry.second); | ||
} | ||
} | ||
schema = std::move(metadata); | ||
schema.assign("not", JSON::make_object()); | ||
} | ||
} | ||
}; |
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.
I think at this point you can simplify the condition by making it a one liner on a
return
. That will make it easier to read, as we won't be reading negated conditionals