Skip to content

Commit

Permalink
Fix ASTRewriteAnalyzer visit(YieldStatement) (eclipse-jdt#1509)
Browse files Browse the repository at this point in the history
- change visit to look for semi-colon to denote end token rather
  than break statement which isn't correct
- add new test to ASTRewritingSwitchExpressionsTest
- fixes eclipse-jdt#1508
  • Loading branch information
jjohnstn authored Oct 25, 2023
1 parent 56ae8eb commit 9680deb
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2022 IBM Corporation and others.
* Copyright (c) 2019, 2023 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -880,6 +880,72 @@ public void testSwitchExpressions_05_since_12() throws Exception {
assertEqualString(preview, builder.toString());
}

@SuppressWarnings("rawtypes")
public void testSwitchExpressions_06_since_12() throws Exception {
if (checkAPILevel()) {
return;
}
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1508
IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
StringBuilder builder= new StringBuilder();
builder.append(
"package test1;\n" +
"public class X {\n" +
" public String foo(int i) {\n" +
" String ret = switch(i) {\n" +
" case 0 -> {\n" +
" yield \"abc\";\n" +
" }\n" +
" default -> \"\";\n" +
" };\n" +
" return ret;" +
" }\n" +
"}\n");
ICompilationUnit cu= pack1.createCompilationUnit("X.java", builder.toString(), false, null);

CompilationUnit astRoot= createAST(this.apiLevel, cu);
ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());

assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
TypeDeclaration type= findTypeDeclaration(astRoot, "X");
MethodDeclaration methodDecl= findMethodDeclaration(type, "foo");
Block block= methodDecl.getBody();
List blockStatements= block.statements();
{
VariableDeclarationStatement varStatement= (VariableDeclarationStatement) blockStatements.get(0);
List fragments = varStatement.fragments();
assertEquals("Incorrect no of fragments", 1, fragments.size());
VariableDeclarationFragment fragment = (VariableDeclarationFragment) fragments.get(0);
SwitchExpression initializer = (SwitchExpression) fragment.getInitializer();
List statements= initializer.statements();
assertEquals("incorrect Number of statements", 4, statements.size());

Block block2 = (Block) statements.get(1);
List statements2 = block2.statements();
// replace the yield expression to exercise the fix
YieldStatement yieldStmt = (YieldStatement) statements2.get(0);
Expression exp = yieldStmt.getExpression();
ASTNode newLiteral = rewrite.createStringPlaceholder("\"def\"", ASTNode.STRING_LITERAL);
rewrite.replace(exp, newLiteral, null);
}
String preview= evaluateRewrite(cu, rewrite);
builder= new StringBuilder();
builder.append(
"package test1;\n" +
"public class X {\n" +
" public String foo(int i) {\n" +
" String ret = switch(i) {\n" +
" case 0 -> {\n" +
" yield \"def\";\n" +
" }\n" +
" default -> \"\";\n" +
" };\n" +
" return ret;" +
" }\n" +
"}\n");
assertEqualString(preview, builder.toString());
}

@SuppressWarnings("rawtypes")
public void testSwitchExpressions_Bug567975_since_12() throws Exception {
if (checkAPILevel()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4663,7 +4663,7 @@ public boolean visit(YieldStatement node) {
}

try {
int offset= node.isImplicit() ? node.getStartPosition() : getScanner().getTokenEndOffset(TerminalTokens.TokenNamebreak, node.getStartPosition());
int offset= node.isImplicit() ? node.getStartPosition() : getScanner().getTokenEndOffset(TerminalTokens.TokenNameSEMICOLON, node.getStartPosition());
if ((node.getAST().apiLevel() >= JLS14_INTERNAL)) {
rewriteNode(node, YieldStatement.EXPRESSION_PROPERTY, offset, ASTRewriteFormatter.SPACE); // space between yield and label
}
Expand Down

0 comments on commit 9680deb

Please sign in to comment.