Skip to content

Commit

Permalink
{if !inject:beanName}{/if} is marked as invalid
Browse files Browse the repository at this point in the history
Fixes #828

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Mar 29, 2023
1 parent e6114a1 commit 16e0d75
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

import com.redhat.qute.parser.expression.Parts.PartKind;
import com.redhat.qute.parser.template.ASTVisitor;
import com.redhat.qute.parser.template.Parameter;
import com.redhat.qute.parser.template.Section;
import com.redhat.qute.parser.template.SectionKind;

/**
* Namespace part.
Expand All @@ -28,6 +31,8 @@ public class NamespacePart extends Part {

public static final String DATA_NAMESPACE = "data";

private int startName = -1;

public NamespacePart(int start, int end) {
super(start, end);
}
Expand All @@ -36,6 +41,27 @@ public NamespacePart(int start, int end) {
public PartKind getPartKind() {
return PartKind.Namespace;
}

@Override
public int getStartName() {
if (startName != -1) {
return startName;
}
startName = super.getStartName();
Parameter parameter = getOwnerParameter();
if (parameter != null) {
Section section = parameter.getOwnerSection();
if (section != null && section.getSectionKind() == SectionKind.IF) {
String text = getOwnerTemplate().getText();
if (text.charAt(startName) == '!') {
// ex : !inject:
// !inject --> inject
startName++;
}
}
}
return startName;
}

@Override
protected void accept0(ASTVisitor visitor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import com.redhat.qute.parser.expression.Parts.PartKind;
import com.redhat.qute.parser.template.ASTVisitor;
import com.redhat.qute.parser.template.Expression;
import com.redhat.qute.parser.template.JavaTypeInfoProvider;
import com.redhat.qute.parser.template.Parameter;
import com.redhat.qute.parser.template.Section;
Expand Down Expand Up @@ -148,24 +147,6 @@ protected boolean canBeOptional() {
return true;
}

/**
* Returns the owner parameter of the object part and null otherwise.
*
* <p>
* {#if foo?? }
* </p>
*
* <p>
* {#let foo='bar' }
* </p>
*
* @return the owner parameter of the object part and null otherwise.
*/
public Parameter getOwnerParameter() {
Expression expression = getParent().getParent();
return expression != null ? expression.getOwnerParameter() : null;
}

@Override
protected void accept0(ASTVisitor visitor) {
visitor.visit(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.redhat.qute.parser.template.Expression;
import com.redhat.qute.parser.template.Node;
import com.redhat.qute.parser.template.NodeKind;
import com.redhat.qute.parser.template.Parameter;
import com.redhat.qute.parser.template.Section;

/**
Expand Down Expand Up @@ -198,6 +199,23 @@ public String toString() {
return getPartName();
}

/**
* Returns the owner parameter of the object part and null otherwise.
*
* <p>
* {#if foo?? }
* </p>
*
* <p>
* {#let foo='bar' }
* </p>
*
* @return the owner parameter of the object part and null otherwise.
*/
public Parameter getOwnerParameter() {
Expression expression = getParent().getParent();
return expression != null ? expression.getOwnerParameter() : null;
}
/**
* Returns the part kind.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,15 @@ public void conditionWithMethod() throws Exception {
}

@Test
public void notOperator() {
public void notOperatorWithObjectPart() {
String template = "{#if !true}NOK{#else}OK{/if}";
testDiagnosticsFor(template);

Diagnostic d = d(0, 6, 0, 9, QuteErrorCode.UndefinedObject, "`foo` cannot be resolved to an object.",
DiagnosticSeverity.Warning);

template = "{#if !foo}NOK{#else}OK{/if}";
testDiagnosticsFor(template, d);
testDiagnosticsFor(template, d(0, 6, 0, 9, QuteErrorCode.UndefinedObject, "`foo` cannot be resolved to an object.",
DiagnosticSeverity.Warning));
}

@Test
public void onlyNotOperator() {
String template = "{#if !}{/if}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,20 @@ public void cdi() {
template = "{cdi:bean.isEmpty()}";
testDiagnosticsFor(template);
}

@Test
public void notOperatorWithNamespacePart() {
String template = "{#if !cdi:bean}NOK{#else}OK{/if}";
testDiagnosticsFor(template);

template = "{#if !cdi:foo}NOK{#else}OK{/if}";
testDiagnosticsFor(template, d(0, 10, 0, 13, QuteErrorCode.UndefinedObject, "`foo` cannot be resolved to an object.",
DiagnosticSeverity.Warning));

template = "{#if !foo:bar}NOK{#else}OK{/if}";
testDiagnosticsFor(template, d(0, 6, 0, 9, QuteErrorCode.UndefinedNamespace, "No namespace resolver found for: `foo`.",
DiagnosticSeverity.Warning));
}

@Test
public void badNamespace() throws Exception {
Expand Down

0 comments on commit 16e0d75

Please sign in to comment.