Skip to content

Commit

Permalink
False positive error with param name and user tag
Browse files Browse the repository at this point in the history
Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Oct 17, 2024
1 parent c09055c commit d0a3399
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,10 @@ private static void validateSectionTag(Section section, Template template,
UserTag userTag = project.findUserTag(tagName);
if (userTag != null) {
// Validate parameters of user tag

List<String> existingParameters = new ArrayList<>();
// Check if the parameter exists
for (Parameter parameter : section.getParameters()) {
String paramName = !parameter.hasValueAssigned() ? IT_OBJECT_PART_NAME : parameter.getName();
String paramName = getParamName(parameter);
if (existingParameters.contains(paramName)) {
// It exists several parameters with the same name
Range range = QutePositionUtility.selectParameterName(parameter);
Expand All @@ -399,10 +398,27 @@ private static void validateSectionTag(Section section, Template template,
// Check if the declared parameter is defined in the user tag
UserTagParameter userTagParameter = userTag.findParameter(paramName);
if (userTagParameter == null) {
Range range = QutePositionUtility.selectParameterName(parameter);
Diagnostic diagnostic = createDiagnostic(range, DiagnosticSeverity.Warning,
QuteErrorCode.UndefinedParameter, paramName, tagName);
diagnostics.add(diagnostic);
if (!parameter.hasValueAssigned()) {
// ex : foo
// check if foocan be used as 'it'
paramName = IT_OBJECT_PART_NAME;
userTagParameter = userTag.findParameter(paramName);
if (existingParameters.contains(paramName)) {
// It exists several parameters with the same name
Range range = QutePositionUtility.selectParameterName(parameter);
Diagnostic diagnostic = createDiagnostic(range, DiagnosticSeverity.Warning,
QuteErrorCode.DuplicateParameter, paramName, tagName);
diagnostics.add(diagnostic);
} else {
existingParameters.add(paramName);
}
}
if (userTagParameter == null) {
Range range = QutePositionUtility.selectParameterName(parameter);
Diagnostic diagnostic = createDiagnostic(range, DiagnosticSeverity.Warning,
QuteErrorCode.UndefinedParameter, paramName, tagName);
diagnostics.add(diagnostic);
}
}
}
}
Expand Down Expand Up @@ -456,6 +472,27 @@ private static void validateSectionTag(Section section, Template template,

}

private static String getParamName(Parameter parameter) {
if (parameter.hasValueAssigned()) {
// ex : foo="bar" --> return 'foo'
return parameter.getName();
}
// ex:
// bar
// uri:bar
// bar.foo()
String name = parameter.getValue();
if (name.indexOf('.') != -1 || name.indexOf(':') != -1 || name.indexOf('(') != -1) {
// ex:
// uri:bar
// bar.foo()
// return 'it'
return IT_OBJECT_PART_NAME;
}

return name;
}

private static boolean canChangeContext(Section section) {
SectionKind sectionKind = section.getSectionKind();
return sectionKind == SectionKind.EACH || sectionKind == SectionKind.FOR || sectionKind == SectionKind.LET
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ public void definedAllParameterNames() {
testDiagnosticsFor(template);
}

@Test
public void definedAllParameterNamesWithoutAssignment() {
String template = "{@java.lang.String name}\n" + //
"{@java.lang.String id}\n" + //
"{#input name value='' id /}";
testDiagnosticsFor(template);
}

@Test
public void undefinedParameterName() {
String template = "{#input name='' XXXX='' /}";
Expand All @@ -71,8 +79,9 @@ public void ignoreUndefinedParameterNameWhenArgs() throws Exception {
// tagWithArgs.html contains an expression which uses _args
// in this case, we ignore the QuteErrorCode.UndefinedParameter error.
String template = "{#tagWithArgs name='' XXXX='' /}";

// There is no error with name (just an error with foo parameter which is required)

// There is no error with name (just an error with foo parameter which is
// required)
Diagnostic d = d(0, 1, 0, 13, QuteErrorCode.MissingRequiredParameter,
"Missing required parameter(s) `foo` of `tagWithArgs` user tag.",
DiagnosticSeverity.Warning);
Expand Down Expand Up @@ -127,6 +136,17 @@ public void duplicateItParameter() {
testDiagnosticsFor(template, d);
}

@Test
public void duplicateItParameter2() {
String template = "{@java.lang.String login}\n" + //
"{@java.lang.String login}\n" + //
"{#form login confirm /}";
Diagnostic d = d(2, 13, 2, 20, QuteErrorCode.DuplicateParameter,
"Duplicate parameter `it` of `form` user tag.",
DiagnosticSeverity.Warning);
testDiagnosticsFor(template, d);
}

@Test
public void missingRequiredParameterNameMultiple() throws Exception {
String template = "{#inputRequired /}";
Expand Down

0 comments on commit d0a3399

Please sign in to comment.