From 91a1656302d162cf02f8ee29ba9cc6f0841f7bf8 Mon Sep 17 00:00:00 2001 From: kstich Date: Mon, 14 Mar 2022 16:27:31 -0700 Subject: [PATCH] Exclude enum and intEnum from some linting This commit excludes linting enum and intEnum members for AbbreviationName and CamelCase checks, as they are intended to be CAPS_SNAKE. --- .../linters/AbbreviationNameValidator.java | 13 +++++++++-- .../smithy/linters/CamelCaseValidator.java | 6 +++++ .../abbreviation-name-validator-test.json | 22 +++++++++++++++++++ .../camel-case-validator-defaults-test.json | 22 +++++++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/smithy-linters/src/main/java/software/amazon/smithy/linters/AbbreviationNameValidator.java b/smithy-linters/src/main/java/software/amazon/smithy/linters/AbbreviationNameValidator.java index bc11ee03015..72a856d10d1 100644 --- a/smithy-linters/src/main/java/software/amazon/smithy/linters/AbbreviationNameValidator.java +++ b/smithy-linters/src/main/java/software/amazon/smithy/linters/AbbreviationNameValidator.java @@ -76,11 +76,20 @@ private AbbreviationNameValidator(Config config) { @Override public List validate(Model model) { return model.shapes() - .flatMap(this::validateShapeName) + .flatMap(shape -> validateShapeName(model, shape)) .collect(Collectors.toList()); } - private Stream validateShapeName(Shape shape) { + private Stream validateShapeName(Model model, Shape shape) { + // Exclude members of enums from AbbreviationName validation, + // as they're intended to be CAPS_SNAKE. + if (shape.isMemberShape()) { + Shape container = model.expectShape(shape.asMemberShape().get().getContainer()); + if (container.isEnumShape() || container.isIntEnumShape()) { + return Stream.empty(); + } + } + String descriptor = shape.isMemberShape() ? "member" : "shape"; String name = shape.asMemberShape() .map(MemberShape::getMemberName) diff --git a/smithy-linters/src/main/java/software/amazon/smithy/linters/CamelCaseValidator.java b/smithy-linters/src/main/java/software/amazon/smithy/linters/CamelCaseValidator.java index cabe6d8d36d..a4ea239b30c 100644 --- a/smithy-linters/src/main/java/software/amazon/smithy/linters/CamelCaseValidator.java +++ b/smithy-linters/src/main/java/software/amazon/smithy/linters/CamelCaseValidator.java @@ -137,6 +137,12 @@ public List validate(Model model) { Pattern isValidMemberName = config.getMemberNames().getRegex(); model.shapes(MemberShape.class) + // Exclude members of enums from CamelCase validation, + // as they're intended to be CAPS_SNAKE. + .filter(shape -> { + Shape container = model.expectShape(shape.asMemberShape().get().getContainer()); + return !container.isEnumShape() && !container.isIntEnumShape(); + }) .filter(shape -> !isValidMemberName.matcher(shape.getMemberName()).find()) .map(shape -> danger(shape, format( "Member shape member name, `%s`, is not %s camel case", diff --git a/smithy-linters/src/test/resources/software/amazon/smithy/linters/errorfiles/abbreviation-name-validator-test.json b/smithy-linters/src/test/resources/software/amazon/smithy/linters/errorfiles/abbreviation-name-validator-test.json index daf73931277..be5a0ba9772 100644 --- a/smithy-linters/src/test/resources/software/amazon/smithy/linters/errorfiles/abbreviation-name-validator-test.json +++ b/smithy-linters/src/test/resources/software/amazon/smithy/linters/errorfiles/abbreviation-name-validator-test.json @@ -35,6 +35,28 @@ "target": "ns.foo#FooId" } } + }, + "ns.foo#Enum": { + "type": "enum", + "members": { + "NAME": { + "target": "smithy.api#Unit", + "traits": { + "smithy.api#enumValue": "foo" + } + } + } + }, + "ns.foo#IntEnum": { + "type": "intEnum", + "members": { + "NAME": { + "target": "smithy.api#Unit", + "traits": { + "smithy.api#enumValue": 1 + } + } + } } }, "metadata": { diff --git a/smithy-linters/src/test/resources/software/amazon/smithy/linters/errorfiles/camel-case-validator-defaults-test.json b/smithy-linters/src/test/resources/software/amazon/smithy/linters/errorfiles/camel-case-validator-defaults-test.json index d689aa97cf2..39b9dd54c7f 100644 --- a/smithy-linters/src/test/resources/software/amazon/smithy/linters/errorfiles/camel-case-validator-defaults-test.json +++ b/smithy-linters/src/test/resources/software/amazon/smithy/linters/errorfiles/camel-case-validator-defaults-test.json @@ -95,6 +95,28 @@ }, "smithy.api#authDefinition": {} } + }, + "ns.foo#Enum": { + "type": "enum", + "members": { + "NAME": { + "target": "smithy.api#Unit", + "traits": { + "smithy.api#enumValue": "foo" + } + } + } + }, + "ns.foo#IntEnum": { + "type": "intEnum", + "members": { + "NAME": { + "target": "smithy.api#Unit", + "traits": { + "smithy.api#enumValue": 1 + } + } + } } }, "metadata": {