From 24c1dddc75303547eaad898a5fe32f2e25203a11 Mon Sep 17 00:00:00 2001 From: Hayden Baker Date: Mon, 6 May 2024 16:55:02 -0700 Subject: [PATCH 1/2] Fix formatter to correctly convert invalid doc comments --- .../amazon/smithy/syntax/FixBadDocComments.java | 11 +++++------ .../amazon/smithy/syntax/ParseAndFormatTest.java | 5 ++--- .../fixes-invalid-doc-comments.formatted.smithy | 8 ++++---- .../formatter/fixes-invalid-doc-comments.smithy | 5 +++-- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/smithy-syntax/src/main/java/software/amazon/smithy/syntax/FixBadDocComments.java b/smithy-syntax/src/main/java/software/amazon/smithy/syntax/FixBadDocComments.java index 7a62655cf78..a358a3f0f1d 100644 --- a/smithy-syntax/src/main/java/software/amazon/smithy/syntax/FixBadDocComments.java +++ b/smithy-syntax/src/main/java/software/amazon/smithy/syntax/FixBadDocComments.java @@ -67,6 +67,11 @@ public TokenTree apply(TokenTree tree) { updateNestedChildren(cursor); } + // Doc comments should not appear in TRAIT_STATEMENTS. + for (TreeCursor cursor : shapeSection.findChildrenByType(TreeType.TRAIT_STATEMENTS)) { + updateNestedChildren(cursor); + } + // Fix doc comments that come before apply statements. TreeCursor shapeStatements = shapeSection.getFirstChild(TreeType.SHAPE_STATEMENTS); if (shapeStatements != null) { @@ -77,12 +82,6 @@ public TokenTree apply(TokenTree tree) { updateNestedChildren(br); } } - - // Remove trailing doc comments in member bodies. - for (TreeCursor members : shapeStatements.findChildrenByType(TreeType.SHAPE_MEMBERS)) { - TreeCursor ws = members.getLastChild(TreeType.WS); - updateDirectChildren(ws); - } } return tree; diff --git a/smithy-syntax/src/test/java/software/amazon/smithy/syntax/ParseAndFormatTest.java b/smithy-syntax/src/test/java/software/amazon/smithy/syntax/ParseAndFormatTest.java index 3eb26da410c..e87105486fa 100644 --- a/smithy-syntax/src/test/java/software/amazon/smithy/syntax/ParseAndFormatTest.java +++ b/smithy-syntax/src/test/java/software/amazon/smithy/syntax/ParseAndFormatTest.java @@ -1,7 +1,6 @@ package software.amazon.smithy.syntax; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.IOException; import java.nio.file.Files; @@ -54,7 +53,7 @@ public void testRunner(Path filename) { String formatted = Formatter.format(tree, 120); String expected = IoUtils.readUtf8File(formattedFile); - assertThat(formatted, equalTo(expected)); + assertEquals(expected, formatted); } public static List tests() throws Exception { diff --git a/smithy-syntax/src/test/resources/software/amazon/smithy/syntax/formatter/fixes-invalid-doc-comments.formatted.smithy b/smithy-syntax/src/test/resources/software/amazon/smithy/syntax/formatter/fixes-invalid-doc-comments.formatted.smithy index b6984e10c44..d9537ec43ed 100644 --- a/smithy-syntax/src/test/resources/software/amazon/smithy/syntax/formatter/fixes-invalid-doc-comments.formatted.smithy +++ b/smithy-syntax/src/test/resources/software/amazon/smithy/syntax/formatter/fixes-invalid-doc-comments.formatted.smithy @@ -23,19 +23,19 @@ use smithy.api#String /// 12 (keep) @deprecated -/// 13 (change) +// 13 (change) structure Foo { /// 14 (keep) @length( // 15 (change) min: 1 ) - /// 16 (change) + // 16 (change) @since("1.x") - /// 17 (TODO: change) + // 17 (change) bar: String - // 18 (change) + /// 18 (TODO: handle this case somehow) } // 19 (change) diff --git a/smithy-syntax/src/test/resources/software/amazon/smithy/syntax/formatter/fixes-invalid-doc-comments.smithy b/smithy-syntax/src/test/resources/software/amazon/smithy/syntax/formatter/fixes-invalid-doc-comments.smithy index e16e23e1cc9..22dccaa6945 100644 --- a/smithy-syntax/src/test/resources/software/amazon/smithy/syntax/formatter/fixes-invalid-doc-comments.smithy +++ b/smithy-syntax/src/test/resources/software/amazon/smithy/syntax/formatter/fixes-invalid-doc-comments.smithy @@ -32,9 +32,10 @@ structure Foo { ) /// 16 (change) @since("1.x") - /// 17 (TODO: change) + /// 17 (change) bar: String - /// 18 (change) + + /// 18 (TODO: handle this case somehow) } /// 19 (change) From c039f6f07116f22599ab489174355977e7a368b4 Mon Sep 17 00:00:00 2001 From: Miles Ziemer Date: Wed, 8 May 2024 13:40:39 -0400 Subject: [PATCH 2/2] Fix trailing doc comment conversion, update test case --- .../amazon/smithy/syntax/FixBadDocComments.java | 10 ++++++++++ .../fixes-invalid-doc-comments.formatted.smithy | 15 +++++++++++++-- .../formatter/fixes-invalid-doc-comments.smithy | 14 ++++++++++++-- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/smithy-syntax/src/main/java/software/amazon/smithy/syntax/FixBadDocComments.java b/smithy-syntax/src/main/java/software/amazon/smithy/syntax/FixBadDocComments.java index a358a3f0f1d..d4e180f6cb0 100644 --- a/smithy-syntax/src/main/java/software/amazon/smithy/syntax/FixBadDocComments.java +++ b/smithy-syntax/src/main/java/software/amazon/smithy/syntax/FixBadDocComments.java @@ -82,6 +82,16 @@ public TokenTree apply(TokenTree tree) { updateNestedChildren(br); } } + // Fix any trailing doc comments in shape bodies + for (TreeCursor members : shapeStatements.findChildrenByType(TreeType.SHAPE_MEMBERS)) { + TreeCursor closeBrace = members.getLastChild(); + if (closeBrace != null) { + TreeCursor possibleTrailingWs = closeBrace.getPreviousSibling(); + if (possibleTrailingWs != null && possibleTrailingWs.getTree().getType() == TreeType.WS) { + updateDirectChildren(possibleTrailingWs); + } + } + } } return tree; diff --git a/smithy-syntax/src/test/resources/software/amazon/smithy/syntax/formatter/fixes-invalid-doc-comments.formatted.smithy b/smithy-syntax/src/test/resources/software/amazon/smithy/syntax/formatter/fixes-invalid-doc-comments.formatted.smithy index d9537ec43ed..e7e8d0d29b7 100644 --- a/smithy-syntax/src/test/resources/software/amazon/smithy/syntax/formatter/fixes-invalid-doc-comments.formatted.smithy +++ b/smithy-syntax/src/test/resources/software/amazon/smithy/syntax/formatter/fixes-invalid-doc-comments.formatted.smithy @@ -35,7 +35,12 @@ structure Foo { // 17 (change) bar: String - /// 18 (TODO: handle this case somehow) + /// 17a (keep) + @length(min: 1) + baz: String = "" + + /// 18 (TODO: Fix trailing comment after VALUE_ASSIGNMENT) + } // 19 (change) @@ -45,4 +50,10 @@ apply Foo @tags(["a"]) list Baz { member: Integer } -// 21 (change) + +structure Foo2 { + foo2: String + + // 21 (change) +} +// 22 (change) diff --git a/smithy-syntax/src/test/resources/software/amazon/smithy/syntax/formatter/fixes-invalid-doc-comments.smithy b/smithy-syntax/src/test/resources/software/amazon/smithy/syntax/formatter/fixes-invalid-doc-comments.smithy index 22dccaa6945..5aeb298412a 100644 --- a/smithy-syntax/src/test/resources/software/amazon/smithy/syntax/formatter/fixes-invalid-doc-comments.smithy +++ b/smithy-syntax/src/test/resources/software/amazon/smithy/syntax/formatter/fixes-invalid-doc-comments.smithy @@ -35,7 +35,11 @@ structure Foo { /// 17 (change) bar: String - /// 18 (TODO: handle this case somehow) + /// 17a (keep) + @length(min: 1) + baz: String = "" + + /// 18 (TODO: Fix trailing comment after VALUE_ASSIGNMENT) } /// 19 (change) @@ -46,4 +50,10 @@ list Baz { member: Integer } -/// 21 (change) +structure Foo2 { + foo2: String + + /// 21 (change) +} + +/// 22 (change)