Skip to content
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

Port redundant/if_without_then_else from JSON BinPack #209

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/linter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ noa_library(NAMESPACE sourcemeta PROJECT alterschema NAME linter
redundant/else_without_if.h
redundant/empty_dependencies.h
redundant/empty_dependent_required.h
redundant/if_without_then_else.h
redundant/items_array_default.h
redundant/items_schema_default.h
redundant/max_contains_without_contains.h
Expand Down
2 changes: 2 additions & 0 deletions src/linter/linter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ auto contains_any(const T &container, const T &values) -> bool {
#include "redundant/else_without_if.h"
#include "redundant/empty_dependencies.h"
#include "redundant/empty_dependent_required.h"
#include "redundant/if_without_then_else.h"
#include "redundant/items_array_default.h"
#include "redundant/items_schema_default.h"
#include "redundant/max_contains_without_contains.h"
Expand Down Expand Up @@ -82,6 +83,7 @@ auto add(Bundle &bundle, const LinterCategory category) -> void {
bundle.add<ElseWithoutIf>();
bundle.add<EmptyDependencies>();
bundle.add<EmptyDependentRequired>();
bundle.add<IfWithoutThenElse>();
bundle.add<ItemsArrayDefault>();
bundle.add<ItemsSchemaDefault>();
bundle.add<MaxContainsWithoutContains>();
Expand Down
24 changes: 24 additions & 0 deletions src/linter/redundant/if_without_then_else.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class IfWithoutThenElse final : public Rule {
public:
IfWithoutThenElse()
: Rule{"if_without_then_else",
"The `if` keyword is meaningless "
"without the presence of the `then` or `else` keywords"} {};

[[nodiscard]] auto
condition(const sourcemeta::jsontoolkit::JSON &schema, const std::string &,
const std::set<std::string> &vocabularies,
const sourcemeta::jsontoolkit::Pointer &) const -> bool 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") && !schema.defines("else");
}

auto transform(Transformer &transformer) const -> void override {
transformer.erase("if");
}
};
21 changes: 19 additions & 2 deletions test/linter/2019_09_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,23 @@ TEST(Lint_2019_09, else_without_if_2) {
EXPECT_EQ(document, expected);
}

TEST(Lint_2019_09, if_without_then_else_1) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"if": true
})JSON");

LINT_AND_FIX(document);

const sourcemeta::jsontoolkit::JSON expected =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema"
})JSON");

EXPECT_EQ(document, expected);
}

TEST(Lint_2019_09, unevaluated_properties_default_1) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
Expand Down Expand Up @@ -824,7 +841,7 @@ TEST(Lint_2019_09, dependent_required_tautology_2) {
EXPECT_EQ(document, expected);
}

TEST(Lint_2019_09, properties_default) {
TEST(Lint_2019_09, properties_default_1) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
Expand All @@ -841,7 +858,7 @@ TEST(Lint_2019_09, properties_default) {
EXPECT_EQ(document, expected);
}

TEST(Lint_2019_09, pattern_properties_default) {
TEST(Lint_2019_09, pattern_properties_default_1) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
Expand Down
21 changes: 19 additions & 2 deletions test/linter/2020_12_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,23 @@ TEST(Lint_2020_12, else_without_if_2) {
EXPECT_EQ(document, expected);
}

TEST(Lint_2020_12, if_without_then_else_1) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"if": true
})JSON");

LINT_AND_FIX(document);

const sourcemeta::jsontoolkit::JSON expected =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema"
})JSON");

EXPECT_EQ(document, expected);
}

TEST(Lint_2020_12, unevaluated_properties_default_1) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
Expand Down Expand Up @@ -807,7 +824,7 @@ TEST(Lint_2020_12, dependent_required_tautology_2) {
EXPECT_EQ(document, expected);
}

TEST(Lint_2020_12, properties_default) {
TEST(Lint_2020_12, properties_default_1) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
Expand All @@ -824,7 +841,7 @@ TEST(Lint_2020_12, properties_default) {
EXPECT_EQ(document, expected);
}

TEST(Lint_2020_12, pattern_properties_default) {
TEST(Lint_2020_12, pattern_properties_default_1) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
Expand Down
2 changes: 1 addition & 1 deletion test/linter/draft1_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ TEST(Lint_draft1, minimum_real_for_integer_1) {
EXPECT_EQ(document, expected);
}

TEST(Lint_draft1, properties_default) {
TEST(Lint_draft1, properties_default_1) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-01/schema#",
Expand Down
2 changes: 1 addition & 1 deletion test/linter/draft2_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ TEST(Lint_draft2, minimum_real_for_integer_1) {
EXPECT_EQ(document, expected);
}

TEST(Lint_draft2, properties_default) {
TEST(Lint_draft2, properties_default_1) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-02/schema#",
Expand Down
4 changes: 2 additions & 2 deletions test/linter/draft3_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ TEST(Lint_draft3, dependent_required_tautology_2) {
EXPECT_EQ(document, expected);
}

TEST(Lint_draft3, properties_default) {
TEST(Lint_draft3, properties_default_1) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-03/schema#",
Expand All @@ -286,7 +286,7 @@ TEST(Lint_draft3, properties_default) {
EXPECT_EQ(document, expected);
}

TEST(Lint_draft3, pattern_properties_default) {
TEST(Lint_draft3, pattern_properties_default_1) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-03/schema#",
Expand Down
4 changes: 2 additions & 2 deletions test/linter/draft4_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ TEST(Lint_draft4, dependent_required_tautology_2) {
EXPECT_EQ(document, expected);
}

TEST(Lint_draft4, properties_default) {
TEST(Lint_draft4, properties_default_1) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
Expand All @@ -340,7 +340,7 @@ TEST(Lint_draft4, properties_default) {
EXPECT_EQ(document, expected);
}

TEST(Lint_draft4, pattern_properties_default) {
TEST(Lint_draft4, pattern_properties_default_1) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-04/schema#",
Expand Down
4 changes: 2 additions & 2 deletions test/linter/draft6_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ TEST(Lint_draft6, dependent_required_tautology_2) {
EXPECT_EQ(document, expected);
}

TEST(Lint_draft6, properties_default) {
TEST(Lint_draft6, properties_default_1) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-06/schema#",
Expand All @@ -491,7 +491,7 @@ TEST(Lint_draft6, properties_default) {
EXPECT_EQ(document, expected);
}

TEST(Lint_draft6, pattern_properties_default) {
TEST(Lint_draft6, pattern_properties_default_1) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-06/schema#",
Expand Down
21 changes: 19 additions & 2 deletions test/linter/draft7_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,23 @@ TEST(Lint_draft7, else_without_if_2) {
EXPECT_EQ(document, expected);
}

TEST(Lint_draft7, if_without_then_else_1) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-07/schema#",
"if": true
})JSON");

LINT_AND_FIX(document);

const sourcemeta::jsontoolkit::JSON expected =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-07/schema#"
})JSON");

EXPECT_EQ(document, expected);
}

TEST(Lint_draft7, additional_properties_default_1) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
Expand Down Expand Up @@ -565,7 +582,7 @@ TEST(Lint_draft7, dependent_required_tautology_2) {
EXPECT_EQ(document, expected);
}

TEST(Lint_draft7, properties_default) {
TEST(Lint_draft7, properties_default_1) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-07/schema#",
Expand All @@ -582,7 +599,7 @@ TEST(Lint_draft7, properties_default) {
EXPECT_EQ(document, expected);
}

TEST(Lint_draft7, pattern_properties_default) {
TEST(Lint_draft7, pattern_properties_default_1) {
sourcemeta::jsontoolkit::JSON document =
sourcemeta::jsontoolkit::parse(R"JSON({
"$schema": "http://json-schema.org/draft-07/schema#",
Expand Down