Skip to content

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/extension/alterschema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ sourcemeta_library(NAMESPACE sourcemeta PROJECT core NAME alterschema
linter/max_contains_without_contains.h
linter/min_contains_without_contains.h
linter/modern_official_dialect_with_empty_fragment.h
linter/then_empty.h
linter/else_empty.h
linter/then_without_if.h)

if(SOURCEMETA_CORE_INSTALL)
Expand Down
4 changes: 4 additions & 0 deletions src/extension/alterschema/alterschema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ contains_any(const Vocabularies &container,
#include "linter/duplicate_anyof_branches.h"
#include "linter/duplicate_enum_values.h"
#include "linter/duplicate_required_values.h"
#include "linter/else_empty.h"
#include "linter/else_without_if.h"
#include "linter/enum_to_const.h"
#include "linter/enum_with_type.h"
Expand All @@ -69,6 +70,7 @@ contains_any(const Vocabularies &container,
#include "linter/pattern_properties_default.h"
#include "linter/properties_default.h"
#include "linter/single_type_array.h"
#include "linter/then_empty.h"
#include "linter/then_without_if.h"
#include "linter/unevaluated_items_default.h"
#include "linter/unevaluated_properties_default.h"
Expand Down Expand Up @@ -98,6 +100,8 @@ auto add(SchemaTransformer &bundle, const AlterSchemaMode mode)
bundle.add<IfWithoutThenElse>();
bundle.add<MaxContainsWithoutContains>();
bundle.add<MinContainsWithoutContains>();
bundle.add<ThenEmpty>();
bundle.add<ElseEmpty>();
bundle.add<ThenWithoutIf>();
bundle.add<DependenciesPropertyTautology>();
bundle.add<DependentRequiredTautology>();
Expand Down
26 changes: 26 additions & 0 deletions src/extension/alterschema/linter/else_empty.h
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())));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as below

}

auto transform(JSON &schema) const -> void override { schema.erase("else"); }
};
26 changes: 26 additions & 0 deletions src/extension/alterschema/linter/then_empty.h
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())));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line really necessary? I guess the rule applies independently of if (and even if if is not there?)

Copy link
Member

Choose a reason for hiding this comment

The 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 then/else without if? I'm still not sure why you check for if being a boolean. Maybe just the schema.defines("if") is enough?

}

auto transform(JSON &schema) const -> void override { schema.erase("then"); }
};
102 changes: 102 additions & 0 deletions test/alterschema/alterschema_lint_2019_09_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2032,3 +2032,105 @@ TEST(AlterSchema_lint_2019_09, modern_official_dialect_with_empty_fragment_3) {

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, then_empty_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"if": {
"properties": {
"flag": {
"const": true
}
}
},
"then": {}
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

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

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, else_empty_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"if": {
"properties": {
"flag": {
"const": true
}
}
},
"else": {}
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

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

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, then_empty_2) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"if": {
"properties": {
"flag": {
"const": true
}
}
},
"then": {},
"else": {}
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

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

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2019_09, else_empty_2) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"if": {
"properties": {
"flag": {
"const": true
}
}
},
"then": {
"type": "string"
},
"else": {}
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2019-09/schema",
"if": {
"properties": {
"flag": {
"const": true
}
}
},
"then": {
"type": "string"
}
})JSON");

EXPECT_EQ(document, expected);
}
161 changes: 161 additions & 0 deletions test/alterschema/alterschema_lint_2020_12_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2207,3 +2207,164 @@ TEST(AlterSchema_lint_2020_12, modern_official_dialect_with_empty_fragment_3) {

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2020_12, then_empty_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"if": {
"properties": {
"flag": {
"const": true
}
}
},
"then": {}
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

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

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2020_12, else_empty_1) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"if": {
"properties": {
"flag": {
"const": true
}
}
},
"else": {}
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

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

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2020_12, then_empty_2) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"if": {
"properties": {
"flag": {
"const": true
}
}
},
"then": {},
"else": {}
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

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

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2020_12, else_empty_2) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"if": {
"properties": {
"flag": {
"const": true
}
}
},
"then": {
"type": "string"
},
"else": {}
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"if": {
"properties": {
"flag": {
"const": true
}
}
},
"then": {
"type": "string"
}
})JSON");

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2020_12, else_empty_3) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"if": {
"properties": {
"flag": {
"const": true
}
}
},
"else": true
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

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

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2020_12, then_empty_3) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"if": {
"properties": {
"flag": {
"const": true
}
}
},
"then": true
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

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

EXPECT_EQ(document, expected);
}

TEST(AlterSchema_lint_2020_12, then_empty_4) {
sourcemeta::core::JSON document = sourcemeta::core::parse_json(R"JSON({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"then": {}
})JSON");

LINT_AND_FIX_FOR_READABILITY(document);

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

EXPECT_EQ(document, expected);
}
Loading
Loading