Skip to content

Commit

Permalink
Support ! operator in #if without incrementing the part start offset
Browse files Browse the repository at this point in the history
Fixes redhat-developer#816

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Mar 16, 2023
1 parent c0fc338 commit bd4ca98
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.redhat.qute.parser.template.JavaTypeInfoProvider;
import com.redhat.qute.parser.template.Parameter;
import com.redhat.qute.parser.template.Section;
import com.redhat.qute.parser.template.SectionKind;
import com.redhat.qute.parser.template.Template;
import com.redhat.qute.parser.template.sections.LoopSection;

Expand All @@ -33,7 +34,7 @@

public class ObjectPart extends Part {

private Boolean notComputed;
private int startName = -1;

public ObjectPart(int start, int end) {
super(start, end);
Expand All @@ -46,29 +47,23 @@ public PartKind getPartKind() {

@Override
public int getStartName() {
computeNotIfNeeded();
return super.getStartName();
}

private void computeNotIfNeeded() {
if (notComputed != null) {
return;
if (startName != -1) {
return startName;
}
computeNot();
}

private synchronized void computeNot() {
if (notComputed != null) {
return;
}
String text = getOwnerTemplate().getText();
int start = super.getStartName();
if (text.charAt(start) == '!') {
// ex : !true
// !true --> true
super.setStart(start + 1);
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 : !true
// !true --> true
startName++;
}
}
}
notComputed = Boolean.TRUE;
return startName;
}

public JavaTypeInfoProvider resolveJavaType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,15 @@ private ResolvedJavaTypeInfo validateObjectPart(String namespace, ObjectPart obj
return null;
}

String literalJavaType = LiteralSupport.getLiteralJavaType(objectPart.getPartName());
String partName = objectPart.getPartName();
if (partName.isEmpty()) {
// This case comes from when only ! is used in #if
// ex : {#if !}
// In this case ! is not considered as an object part.
return null;
}

String literalJavaType = LiteralSupport.getLiteralJavaType(partName);
if (literalJavaType != null) {
// The object part is a literal type (ex : true)
return null;
Expand All @@ -723,11 +731,15 @@ private ResolvedJavaTypeInfo validateObjectPart(String namespace, ObjectPart obj
return null;
}

if (CaseSection.isCaseSection(ownerSection)) {
if (Section.isCaseSection(ownerSection)) {
// Skip validation for case section, is done later
return null;
}


if (ownerSection != null && ownerSection.getSectionKind() == SectionKind.IF) {

}

// ex : {item} --> undefined object
DiagnosticSeverity severity = validationSettings.getUndefinedObject().getDiagnosticSeverity();
if (severity == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

import io.quarkus.qute.Engine;
import io.quarkus.qute.EngineBuilder;
import io.quarkus.qute.ErrorCode;
import io.quarkus.qute.ParserError;
import io.quarkus.qute.TemplateException;
import io.quarkus.qute.TemplateNode.Origin;
import io.quarkus.qute.UserTagSectionHelper;
Expand Down Expand Up @@ -67,19 +69,31 @@ public void validateWithRealQuteParser(Template template, List<Diagnostic> diagn
Engine engine = engineBuilder.build();
engine.parse(templateContent);
} catch (TemplateException e) {
String message = e.getMessage();
if (message.contains("no section helper found for")) {
if (isIgnoreError(e)) {
// Ignore error "no section helper found for" which is managed with
// QuteDiagnostic to highlight the section start correctly.
return;
}
String message = e.getMessage();
Range range = createRange(e, template);
Diagnostic diagnostic = createDiagnostic(range, message, DiagnosticSeverity.Error,
QuteErrorCode.SyntaxError);
diagnostics.add(diagnostic);
}
}

private static boolean isIgnoreError(TemplateException e) {
// As the Qute real parser is not fault tolerant (it report just one error) and
// as it reports bad offset for the error
// we ignore some errors which are managed by QuteDiagnostics to highlight
// several errors and highlight section, object part, etc correctly.
ErrorCode code = e.getCode();
if (ParserError.NO_SECTION_HELPER_FOUND == code) {
return true;
}
return false;
}

private static void addUserTag(Collection<UserTag> tags, EngineBuilder engineBuilder) {
for (UserTag userTag : tags) {
String tagName = userTag.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ public void notOperator() {
testDiagnosticsFor(template, d);
}

@Test
public void onlyNotOperator() {
String template = "{#if !}{/if}";
Diagnostic d = d(0, 0, 0, 0, QuteErrorCode.SyntaxError, "Unsupported param type: NOT",
DiagnosticSeverity.Error);
testDiagnosticsFor(template, d);
}

@Test
public void noOptionalParameterInIfBlock() {
String template = "{#if foo}\r\n" + //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,11 @@ public void autoClose() throws Exception {
ConfigurationItemEditType.update, "ignore", //
d)));
}

@Test
public void notAsObjectPartName() throws Exception {
String template = "{@int !value}\r\n" + //
"{#let name=!value /}";
testDiagnosticsFor(template);
}
}

0 comments on commit bd4ca98

Please sign in to comment.