groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: Validate duplicated named parameters
Date Fri, 04 Aug 2017 18:11:17 GMT
Repository: groovy
Updated Branches:
  refs/heads/master f35abadad -> a3070da9a


Validate duplicated named parameters


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

Branch: refs/heads/master
Commit: a3070da9ad923213c57daa971e1f48d46e1c8e0e
Parents: f35abad
Author: sunlan <sunlan@apache.org>
Authored: Sat Aug 5 02:09:57 2017 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Sat Aug 5 02:11:08 2017 +0800

----------------------------------------------------------------------
 .../apache/groovy/parser/antlr4/AstBuilder.java | 22 +++++++++++++++++++-
 .../groovy/parser/antlr4/SyntaxErrorTest.groovy |  5 +++++
 .../fail/DuplicatedNamedParameter_01x.groovy    | 21 +++++++++++++++++++
 .../fail/DuplicatedNamedParameter_02x.groovy    | 21 +++++++++++++++++++
 4 files changed, 68 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/a3070da9/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 ce83274..e8f0a9b 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
@@ -2261,7 +2261,10 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
                 .forEach(e -> {
 
                     if (e instanceof MapEntryExpression) {
-                        mapEntryExpressionList.add((MapEntryExpression) e);
+                        MapEntryExpression mapEntryExpression = (MapEntryExpression) e;
+                        validateDuplicatedNamedParameter(mapEntryExpressionList, mapEntryExpression);
+
+                        mapEntryExpressionList.add(mapEntryExpression);
                     } else {
                         expressionList.add(e);
                     }
@@ -2292,6 +2295,23 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
         throw createParsingFailedException("Unsupported argument list: " + ctx.getText(),
ctx);
     }
 
+    private void validateDuplicatedNamedParameter(List<MapEntryExpression> mapEntryExpressionList,
MapEntryExpression mapEntryExpression) {
+        Expression keyExpression = mapEntryExpression.getKeyExpression();
+
+        if (null == keyExpression) {
+            return;
+        }
+
+        String parameterName = keyExpression.getText();
+
+        boolean isDuplicatedNamedParameter = mapEntryExpressionList.stream().anyMatch(m ->
m.getKeyExpression().getText().equals(parameterName));
+        if (!isDuplicatedNamedParameter) {
+            return;
+        }
+
+        throw createParsingFailedException("Duplicated named parameter '" + parameterName
+ "' found", mapEntryExpression);
+    }
+
     @Override
     public Expression visitEnhancedArgumentListElement(EnhancedArgumentListElementContext
ctx) {
         if (asBoolean(ctx.expressionListElement())) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/a3070da9/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
index cbf2b17..86bec8d 100644
--- a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
+++ b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
@@ -184,6 +184,11 @@ class SyntaxErrorTest extends GroovyTestCase {
         TestUtils.doRunAndShouldFail('fail/Assert_01x.groovy');
     }
 
+    void "test groovy core - DuplicatedNamedParameter"() {
+        TestUtils.doRunAndShouldFail('fail/DuplicatedNamedParameter_01x.groovy');
+        TestUtils.doRunAndShouldFail('fail/DuplicatedNamedParameter_02x.groovy');
+    }
+
     /**************************************/
     static unzipScriptAndShouldFail(String entryName, List ignoreClazzList, Map<String,
String> replacementsMap=[:], boolean toCheckNewParserOnly = false) {
         ignoreClazzList.addAll(TestUtils.COMMON_IGNORE_CLASS_LIST)

http://git-wip-us.apache.org/repos/asf/groovy/blob/a3070da9/subprojects/parser-antlr4/src/test/resources/fail/DuplicatedNamedParameter_01x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/fail/DuplicatedNamedParameter_01x.groovy
b/subprojects/parser-antlr4/src/test/resources/fail/DuplicatedNamedParameter_01x.groovy
new file mode 100644
index 0000000..035789f
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/DuplicatedNamedParameter_01x.groovy
@@ -0,0 +1,21 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+def closure = { println it }
+closure debit: 30, credit: 40, debit: 50, {}

http://git-wip-us.apache.org/repos/asf/groovy/blob/a3070da9/subprojects/parser-antlr4/src/test/resources/fail/DuplicatedNamedParameter_02x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/fail/DuplicatedNamedParameter_02x.groovy
b/subprojects/parser-antlr4/src/test/resources/fail/DuplicatedNamedParameter_02x.groovy
new file mode 100644
index 0000000..4481046
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/DuplicatedNamedParameter_02x.groovy
@@ -0,0 +1,21 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+def m(x) { println x }
+m debit: 30, credit: 40, debit: 50, {}


Mime
View raw message