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 2021/04/19 05:48:39 UTC

[groovy] branch master updated: Tweak error message for invalid GINQ

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 0cc52bc  Tweak error message for invalid GINQ
0cc52bc is described below

commit 0cc52bcb83b826d8007142181290d992f2c5f5ff
Author: Daniel Sun <su...@apache.org>
AuthorDate: Mon Apr 19 13:48:25 2021 +0800

    Tweak error message for invalid GINQ
---
 .../org/apache/groovy/ginq/dsl/GinqAstBuilder.java | 16 ++++++++-
 .../org/apache/groovy/ginq/GinqErrorTest.groovy    | 39 +++++++++++++++++++++-
 2 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/dsl/GinqAstBuilder.java b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/dsl/GinqAstBuilder.java
index 9d36427..379472a 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/dsl/GinqAstBuilder.java
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/dsl/GinqAstBuilder.java
@@ -41,6 +41,7 @@ import org.codehaus.groovy.ast.expr.CastExpression;
 import org.codehaus.groovy.ast.expr.DeclarationExpression;
 import org.codehaus.groovy.ast.expr.Expression;
 import org.codehaus.groovy.ast.expr.MethodCallExpression;
+import org.codehaus.groovy.ast.expr.PropertyExpression;
 import org.codehaus.groovy.ast.expr.VariableExpression;
 import org.codehaus.groovy.ast.stmt.BlockStatement;
 import org.codehaus.groovy.ast.stmt.ExpressionStatement;
@@ -364,7 +365,7 @@ public class GinqAstBuilder extends CodeVisitorSupport implements SyntaxErrorRep
         if (KEYWORD_SET.contains(expression.getText())) {
             this.collectSyntaxError(
                     new GinqSyntaxError(
-                            "Invalid syntax found in `" + expression.getText() + "' clause",
+                            "Invalid syntax found in `" + expression.getText() + "` clause",
                             expression.getLineNumber(), expression.getColumnNumber()
                     )
             );
@@ -374,6 +375,19 @@ public class GinqAstBuilder extends CodeVisitorSupport implements SyntaxErrorRep
     }
 
     @Override
+    public void visitPropertyExpression(PropertyExpression expression) {
+        super.visitPropertyExpression(expression);
+        if (isSelectMethodCallExpression(expression.getObjectExpression())) {
+            this.collectSyntaxError(
+                    new GinqSyntaxError(
+                            "Invalid syntax found in `select` clause, maybe `as` is missing when renaming field.",
+                            expression.getLineNumber(), expression.getColumnNumber()
+                    )
+            );
+        }
+    }
+
+    @Override
     public void visitDeclarationExpression(DeclarationExpression expression) {
         final String typeName = expression.getLeftExpression().getType().getNameWithoutPackage();
         if (KEYWORD_SET.contains(typeName)) {
diff --git a/subprojects/groovy-ginq/src/test/groovy/org/apache/groovy/ginq/GinqErrorTest.groovy b/subprojects/groovy-ginq/src/test/groovy/org/apache/groovy/ginq/GinqErrorTest.groovy
index de60e91..0beefad 100644
--- a/subprojects/groovy-ginq/src/test/groovy/org/apache/groovy/ginq/GinqErrorTest.groovy
+++ b/subprojects/groovy-ginq/src/test/groovy/org/apache/groovy/ginq/GinqErrorTest.groovy
@@ -21,6 +21,7 @@ package org.apache.groovy.ginq
 import groovy.transform.CompileStatic
 import org.junit.Test
 
+import static groovy.test.GroovyAssert.assertScript
 import static groovy.test.GroovyAssert.shouldFail
 
 @CompileStatic
@@ -276,7 +277,7 @@ class GinqErrorTest {
             }
         '''
 
-        assert err.toString().contains("Invalid syntax found in `where' clause @ line 3, column 17.")
+        assert err.toString().contains("Invalid syntax found in `where` clause @ line 3, column 17.")
     }
 
     @Test
@@ -598,4 +599,40 @@ class GinqErrorTest {
 
         assert err.toString().contains('`select` clause is missing @ line 2, column 17.')
     }
+
+    @Test
+    void "testGinq - missing as - 1"() {
+        def err = shouldFail '''\
+            GQ {
+                from n in [1, 2]
+                select count() cnt
+            }
+        '''
+
+        assert err.toString().contains('Invalid syntax found in `select` clause, maybe `as` is missing when renaming field. @ line 3, column 17.')
+    }
+
+    @Test
+    void "testGinq - missing as - 2"() {
+        def err = shouldFail '''\
+            GQ {
+                from n in [1, 2]
+                select n num
+            }
+        '''
+
+        assert err.toString().contains('Invalid syntax found in `select` clause, maybe `as` is missing when renaming field. @ line 3, column 17.')
+    }
+
+    @Test
+    void "testGinq - missing as - 3"() {
+        def err = shouldFail '''\
+            GQ {
+                from n in [1, 2]
+                select n num1, n as num2
+            }
+        '''
+
+        assert err.toString().contains('Invalid syntax found in `select` clause @ line 3, column 17.')
+    }
 }