[apex] ASTBlockStatement - verify curlyBrace

This commit is contained in:
Andreas Dangel
2024-02-09 09:50:24 +01:00
parent 17848c484d
commit 6c4f3efbc7
2 changed files with 32 additions and 6 deletions

View File

@@ -7,7 +7,6 @@ package net.sourceforge.pmd.lang.apex.ast;
import net.sourceforge.pmd.lang.document.TextDocument;
import com.google.summit.ast.Node;
import com.google.summit.ast.SourceLocation;
public final class ASTBlockStatement extends AbstractApexNode.Single<Node> {
private boolean curlyBrace;
@@ -33,12 +32,9 @@ public final class ASTBlockStatement extends AbstractApexNode.Single<Node> {
return;
}
// check, whether the this block statement really begins with a curly brace
// check, whether this block statement really begins with a curly brace
// unfortunately, for-loop and if-statements always contain a block statement,
// regardless whether curly braces where present or not.
// TODO: Revisit this, because it is Jorje-specific.
SourceLocation loc = node.getSourceLocation();
String sourceRegion = loc.extractFrom(sourceCode.getText().toString());
this.curlyBrace = sourceRegion.charAt(0) == '{';
this.curlyBrace = sourceCode.getText().slice(getTextRegion()).charAt(0) == '{';
}
}

View File

@@ -0,0 +1,30 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.apex.ast;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
class ASTBlockStatementTest extends ApexParserTestBase {
@Test
void noCurlyBraces() {
ASTBlockStatement blockStatement = parse("class Foo { { if (true) methodCall(); } }")
.descendants(ASTIfBlockStatement.class)
.firstChild(ASTBlockStatement.class)
.first();
assertFalse(blockStatement.hasCurlyBrace());
}
@Test
void withCurlyBraces() {
ASTBlockStatement blockStatement = parse("class Foo { { if (true) { methodCall(); } } }")
.descendants(ASTIfBlockStatement.class)
.firstChild(ASTBlockStatement.class)
.first();
assertTrue(blockStatement.hasCurlyBrace());
}
}