Skip to content

Commit f14fb18

Browse files
graememorganronshapiro
authored andcommitted
Make InvalidInlineTag match uses of () to create inline tags rather than {}.
RELNOTES: N/A ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=233739717
1 parent aa49ef3 commit f14fb18

File tree

1 file changed

+62
-9
lines changed

1 file changed

+62
-9
lines changed

Diff for: core/src/main/java/com/google/errorprone/bugpatterns/javadoc/InvalidInlineTag.java

+62-9
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ private final class InvalidTagChecker extends DocTreePathScanner<Void, Void> {
120120
private final ImmutableSet<String> parameters;
121121

122122
private final Pattern misplacedCurly;
123+
private final Pattern parensRatherThanCurly;
123124

124125
private final Set<DocTree> fixedTags = new HashSet<>();
125126

@@ -128,14 +129,13 @@ private InvalidTagChecker(
128129
this.state = state;
129130
this.validTags = validTags;
130131
this.parameters = parameters;
131-
this.misplacedCurly =
132-
Pattern.compile(
133-
String.format(
134-
"@(%s)\\{",
135-
validTags.stream()
136-
.filter(tag -> tag.type() == TagType.INLINE)
137-
.map(JavadocTag::name)
138-
.collect(joining("|"))));
132+
String validInlineTags =
133+
validTags.stream()
134+
.filter(tag -> tag.type() == TagType.INLINE)
135+
.map(JavadocTag::name)
136+
.collect(joining("|"));
137+
this.misplacedCurly = Pattern.compile(String.format("@(%s)\\{", validInlineTags));
138+
this.parensRatherThanCurly = Pattern.compile(String.format("\\(@(%s)", validInlineTags));
139139
}
140140

141141
@Override
@@ -163,12 +163,14 @@ public Void visitErroneous(ErroneousTree erroneousTree, Void unused) {
163163
@Override
164164
public Void visitText(TextTree node, Void unused) {
165165
handleMalformedTags(node);
166+
handleIncorrectParens(node);
166167
handleDanglingParams(node);
167168
return super.visitText(node, null);
168169
}
169170

170171
private void handleMalformedTags(TextTree node) {
171-
Matcher matcher = misplacedCurly.matcher(node.getBody());
172+
String body = node.getBody();
173+
Matcher matcher = misplacedCurly.matcher(body);
172174
Comment comment = ((DCDocComment) getCurrentPath().getDocComment()).comment;
173175
while (matcher.find()) {
174176
int beforeAt = comment.getSourcePos(((DCText) node).pos + matcher.start());
@@ -186,6 +188,57 @@ private void handleMalformedTags(TextTree node) {
186188
}
187189
}
188190

191+
private void handleIncorrectParens(TextTree node) {
192+
String body = node.getBody();
193+
Matcher matcher = parensRatherThanCurly.matcher(body);
194+
Comment comment = ((DCDocComment) getCurrentPath().getDocComment()).comment;
195+
while (matcher.find()) {
196+
int beforeAt = comment.getSourcePos(((DCText) node).pos + matcher.start());
197+
SuggestedFix.Builder fix = SuggestedFix.builder().replace(beforeAt, beforeAt + 1, "{");
198+
199+
Optional<Integer> found = findClosingBrace(body, matcher.start(1));
200+
found.ifPresent(
201+
pos -> {
202+
int closing = comment.getSourcePos(((DCText) node).pos + pos);
203+
fix.replace(closing, closing + 1, "}");
204+
});
205+
206+
state.reportMatch(
207+
buildDescription(
208+
getDiagnosticPosition(beforeAt, getCurrentPath().getTreePath().getLeaf()))
209+
.setMessage(
210+
String.format(
211+
"Curly braces should be used for inline Javadoc tags: {%s ...}",
212+
matcher.group(1)))
213+
.addFix(fix.build())
214+
.build());
215+
}
216+
}
217+
218+
/** Looks for a matching closing brace, if one is found. */
219+
private Optional<Integer> findClosingBrace(String body, int startPos) {
220+
int parenDepth = 0;
221+
for (int pos = startPos; pos < body.length(); ++pos) {
222+
char c = body.charAt(pos);
223+
switch (c) {
224+
case '(':
225+
parenDepth++;
226+
continue;
227+
case ')':
228+
if (parenDepth == 0) {
229+
return Optional.of(pos);
230+
}
231+
parenDepth--;
232+
break;
233+
case '}':
234+
return Optional.empty();
235+
default:
236+
// fall out
237+
}
238+
}
239+
return Optional.empty();
240+
}
241+
189242
private void handleDanglingParams(TextTree node) {
190243
Matcher matcher = PARAM_MATCHER.matcher(node.getBody());
191244
Comment comment = ((DCDocComment) getCurrentPath().getDocComment()).comment;

0 commit comments

Comments
 (0)