Skip to content

Commit

Permalink
Fixing handling of trailing empty single-line comment (#5077)
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-at-moderne authored Feb 21, 2025
1 parent 49855ca commit 217c918
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public static Space format(String formatting) {
last = c;
}

if ((comment.length() > 0)) {
if ((comment.length() > 0) || inSingleLineComment) {
comments.add(new Comment(inLineSlashOrHashComment, comment.toString(), prefix.toString(), Markers.EMPTY));
prefix = new StringBuilder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,18 @@ void emptyDoubleSlashCommentJustBeforeCurlyBraceEnd() {
)
);
}

@Test
void emptySingleLineCommentAtTheEndOfTheFile() {
rewriteRun(
hcl(
"""
module "something" {
source = "../else/"
}
//
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public static Space format(String formatting, int beginIndex, int toIndex) {
last = c;
}
// If a file ends with a single-line comment there may be no terminating newline
if (comment.length() > 0) {
if (comment.length() > 0 || inSingleLineComment) {
comments.add(new Comment(false, comment.toString(), prefix.toString(), Markers.EMPTY));
prefix.setLength(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void defaults(RecipeSpec spec) {
void parseJsonDocument() {
rewriteRun(
json(
"""
"""
{
// comments
unquoted: 'and you can quote me on that',
Expand All @@ -50,6 +50,7 @@ void parseJsonDocument() {
trailingComma: 'in objects', andIn: ['arrays',],
"backwardsCompatible": "with JSON",
}
//
"""
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public static Space format(String formatting, int beginIndex, int toIndex) {
}
}
// If a file ends with a single-line comment there may be no terminating newline
if (comment.length() > 0) {
if (comment.length() > 0 || inSingleLineComment) {
comments.add(new Comment(comment.toString(), prefix.toString(), Markers.EMPTY));
prefix.setLength(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,18 @@ void trailingComment() {
);
}

@Test
void trailingEmptyComment() {
rewriteRun(
toml(
"""
str = "I'm a string. \\"You can quote me\\". Name\\tJos\\u00E9\\nLocation\\tSF."
#
"""
)
);
}

@Test
void multiBytesUnicode() {
rewriteRun(
Expand Down

0 comments on commit 217c918

Please sign in to comment.