Skip to content

Commit 526157a

Browse files
authored
Handle more styles of comments (#4)
1 parent d219535 commit 526157a

2 files changed

Lines changed: 67 additions & 2 deletions

File tree

src/main/java/com/github/jimschubert/rewrite/docker/internal/DockerfileParser.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,16 @@ Docker.Instruction parse() {
435435

436436
private Docker.@NotNull Comment parseComment() {
437437
StringWithPadding stringWithPadding = StringWithPadding.of(instruction.toString());
438-
Docker.Literal commentLiteral = createLiteral(stringWithPadding.content()).withPrefix(stringWithPadding.prefix());
438+
Docker.Literal commentLiteral = createLiteral(stringWithPadding.content());
439+
if (commentLiteral == null) {
440+
// if the comment is empty, we need to create a literal with an empty string
441+
commentLiteral = Docker.Literal.build(
442+
Quoting.UNQUOTED,
443+
Space.EMPTY,
444+
"",
445+
Space.EMPTY);
446+
}
447+
commentLiteral = commentLiteral.withPrefix(stringWithPadding.prefix());
439448
return new Docker.Comment(
440449
Tree.randomId(),
441450
state.prefix(),
@@ -831,7 +840,7 @@ private static String handleInstructionType(String line, InstructionParser parse
831840
} else if (instructionType != null) {
832841
parser.instructionType = instructionType;
833842
// remove the first word from line
834-
line = (spaceIndex == -1) ? line : line.substring(spaceIndex);
843+
line = (spaceIndex == -1) ? line.substring(0, firstWord.length()-1) : line.substring(spaceIndex);
835844
} else if (state.prefix() != null && !state.prefix().isEmpty()) {
836845
// if there was any prefix stored previously, add it to the multiline instruction
837846
// this is a special case for multi-line instructions like comments
@@ -882,6 +891,10 @@ private static Class<? extends Docker.Instruction> instructionFromText(String s)
882891
case COMMENT:
883892
return Docker.Comment.class;
884893
default:
894+
// special case for non-standard comments
895+
if (s.startsWith("#")) {
896+
return Docker.Comment.class;
897+
}
885898
return null;
886899
}
887900
}

src/test/java/com/github/jimschubert/rewrite/docker/internal/DockerfileParserTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,58 @@ void testCommentSingleWithTrailingSpace() {
3636
assertComment(comment, " ", "This is a comment", "\t\t");
3737
}
3838

39+
@Test
40+
void testHeaderStyleComments() {
41+
DockerfileParser parser = new DockerfileParser();
42+
Docker.Document doc = parser.parse(new ByteArrayInputStream(
43+
"""
44+
#
45+
# NOTE: THIS DOCKERFILE IS GENERATED VIA "apply-templates.sh"
46+
#
47+
# PLEASE DO NOT EDIT IT DIRECTLY.
48+
#
49+
50+
FROM alpine:3.21
51+
""".getBytes(StandardCharsets.UTF_8)));
52+
53+
Docker.Stage stage = assertSingleStageWithChildCount(doc, 6);
54+
assertComment((Docker.Comment) stage.getChildren().get(0), "", "", "");
55+
assertComment((Docker.Comment) stage.getChildren().get(1), " ", "NOTE: THIS DOCKERFILE IS GENERATED VIA \"apply-templates.sh\"", "");
56+
assertComment((Docker.Comment) stage.getChildren().get(2), "", "", "");
57+
assertComment((Docker.Comment) stage.getChildren().get(3), " ", "PLEASE DO NOT EDIT IT DIRECTLY.", "");
58+
assertComment((Docker.Comment) stage.getChildren().get(4), "", "", "");
59+
60+
Docker.From from = (Docker.From) stage.getChildren().get(5);
61+
assertEquals(Space.EMPTY, from.getPrefix());
62+
assertLiteral(from.getImage(), Quoting.UNQUOTED, " ", "alpine", "");
63+
assertEquals("3.21", from.getTag());
64+
assertEquals("", from.getImage().getTrailing().getWhitespace());
65+
assertEquals(" ", from.getImage().getPrefix().getWhitespace());
66+
}
67+
68+
@Test
69+
void testFlowerboxStyleComments() {
70+
DockerfileParser parser = new DockerfileParser();
71+
Docker.Document doc = parser.parse(new ByteArrayInputStream(
72+
"""
73+
################################################
74+
# ####
75+
# This is a comment
76+
#####
77+
################################################
78+
""".getBytes(StandardCharsets.UTF_8)));
79+
80+
Docker.Stage stage = assertSingleStageWithChildCount(doc, 5);
81+
// note that this is one character less than the first line, because the first # is the "instruction"
82+
assertComment((Docker.Comment) stage.getChildren().get(0), "", "###############################################", "");
83+
assertComment((Docker.Comment) stage.getChildren().get(1), " ", "####", "");
84+
assertComment((Docker.Comment) stage.getChildren().get(2), " ", "This is a comment", "");
85+
// note the lack of the prefix space
86+
assertComment((Docker.Comment) stage.getChildren().get(3), "", "####", "");
87+
// note that this is one character less than the last line, because the first # is the "instruction"
88+
assertComment((Docker.Comment) stage.getChildren().get(4), "", "###############################################", "");
89+
}
90+
3991
@Test
4092
void testRetainCarriageReturn() {
4193
DockerfileParser parser = new DockerfileParser();

0 commit comments

Comments
 (0)