You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2017/09/28 01:00:33 UTC

groovy git commit: Refine safe chain operator

Repository: groovy
Updated Branches:
  refs/heads/master ce1d251bf -> eac49ec2d


Refine safe chain operator


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/eac49ec2
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/eac49ec2
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/eac49ec2

Branch: refs/heads/master
Commit: eac49ec2dbd944bfb036c1c1cd2daac72b686112
Parents: ce1d251
Author: sunlan <su...@apache.org>
Authored: Thu Sep 28 09:00:28 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Thu Sep 28 09:00:28 2017 +0800

----------------------------------------------------------------------
 .../apache/groovy/parser/antlr4/AstBuilder.java |  3 +-
 .../resources/core/SafeChainOperator.groovy     | 56 +++++++++++++++-----
 2 files changed, 46 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/eac49ec2/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index ad632d9..69e2fc9 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -1993,9 +1993,10 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
 
         if (asBoolean(ctx.indexPropertyArgs())) { // e.g. list[1, 3, 5]
             Tuple2<Token, Expression> tuple = this.visitIndexPropertyArgs(ctx.indexPropertyArgs());
+            boolean isSafeChain = isTrue(baseExpr, PATH_EXPRESSION_BASE_EXPR_SAFE_CHAIN);
 
             return configureAST(
-                    new BinaryExpression(baseExpr, createGroovyToken(tuple.getFirst()), tuple.getSecond(), asBoolean(ctx.indexPropertyArgs().QUESTION())),
+                    new BinaryExpression(baseExpr, createGroovyToken(tuple.getFirst()), tuple.getSecond(), isSafeChain || asBoolean(ctx.indexPropertyArgs().QUESTION())),
                     ctx);
         }
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/eac49ec2/subprojects/parser-antlr4/src/test/resources/core/SafeChainOperator.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/core/SafeChainOperator.groovy b/subprojects/parser-antlr4/src/test/resources/core/SafeChainOperator.groovy
index 790dbfe..e3784ba 100644
--- a/subprojects/parser-antlr4/src/test/resources/core/SafeChainOperator.groovy
+++ b/subprojects/parser-antlr4/src/test/resources/core/SafeChainOperator.groovy
@@ -1,3 +1,5 @@
+import groovy.transform.CompileStatic
+
 /*
  *  Licensed to the Apache Software Foundation (ASF) under one
  *  or more contributor license agreements.  See the NOTICE file
@@ -16,15 +18,45 @@
  *  specific language governing permissions and limitations
  *  under the License.
  */
-assert 3 == 1??.plus(2)
-assert 6 == 1??.plus(2).plus(3)
-assert 6 == 1??.plus(2)?.plus(3)
-assert 6 == 1??.plus(2)??.plus(3)
-assert 10 == 1??.plus(2)?.plus(3).plus(4)
-assert 10 == 1?.plus(2)??.plus(3).plus(4)
-assert 10 == 1?.plus(2)?.plus(3)??.plus(4)
-assert 10 == 1.plus(2).plus(3)??.plus(4)
-assert null == null??.plus(2).plus(3)
-assert null == null??.plus(2).plus(3).plus(4)
-assert null == null??.plus(2)??.plus(3).plus(4)
-assert null == null??.plus(2)??.plus(3)?.plus(4)
+def testSCO() {
+    assert 3 == 1??.plus(2)
+    assert 6 == 1??.plus(2).plus(3)
+    assert 6 == 1??.plus(2)?.plus(3)
+    assert 6 == 1??.plus(2)??.plus(3)
+    assert 10 == 1??.plus(2)?.plus(3).plus(4)
+    assert 10 == 1?.plus(2)??.plus(3).plus(4)
+    assert 10 == 1?.plus(2)?.plus(3)??.plus(4)
+    assert 10 == 1.plus(2).plus(3)??.plus(4)
+
+    Integer num = null
+    assert null == num??.plus(2).plus(3)
+    assert null == num??.plus(2).plus(3).plus(4)
+    assert null == num??.plus(2)??.plus(3).plus(4)
+    assert null == num??.plus(2)??.plus(3)?.plus(4)
+
+    String str = null
+    assert null == str??.substring(0, 1)[0]
+}
+testSCO()
+
+@CompileStatic
+def testCsSCO() {
+    assert 3 == 1??.plus(2)
+    assert 6 == 1??.plus(2).plus(3)
+    assert 6 == 1??.plus(2)?.plus(3)
+    assert 6 == 1??.plus(2)??.plus(3)
+    assert 10 == 1??.plus(2)?.plus(3).plus(4)
+    assert 10 == 1?.plus(2)??.plus(3).plus(4)
+    assert 10 == 1?.plus(2)?.plus(3)??.plus(4)
+    assert 10 == 1.plus(2).plus(3)??.plus(4)
+
+    Integer num = null
+    assert null == num??.plus(2).plus(3)
+    assert null == num??.plus(2).plus(3).plus(4)
+    assert null == num??.plus(2)??.plus(3).plus(4)
+    assert null == num??.plus(2)??.plus(3)?.plus(4)
+
+    String str = null
+    assert null == str??.substring(0, 1)[0]
+}
+testCsSCO()