jdaugherty commented on code in PR #15669:
URL: https://github.com/apache/grails-core/pull/15669#discussion_r3344153652


##########
grails-core/src/main/groovy/org/grails/compiler/ControllerTagLibTypeCheckingExtension.groovy:
##########
@@ -0,0 +1,95 @@
+/*
+ *  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
+ *
+ *    https://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.
+ */
+package org.grails.compiler
+
+import org.codehaus.groovy.ast.ClassNode
+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.transform.stc.GroovyTypeCheckingExtensionSupport
+import org.grails.core.artefact.ControllerArtefactHandler
+
+/**
+ * A type-checking extension that allows {@code @GrailsCompileStatic} 
controllers
+ * to invoke tag library methods without compile-time errors.
+ *
+ * <p>Tag calls in controllers are dispatched at runtime through
+ * {@code TagLibraryInvoker#methodMissing} and
+ * {@code TagLibraryInvoker#propertyMissing}. These hooks are
+ * invisible to the static type checker, so this extension marks the affected
+ * expressions as dynamic, silencing the false-positive errors while preserving
+ * full type checking for all other code in the controller.
+ *
+ * <p>Controller detection mirrors {@code ControllerActionTransformer}: a 
class is
+ * treated as a controller when its qualified name ends with {@code 
"Controller"}.
+ *
+ * <p>Two calling patterns are supported:
+ * <ul>
+ *   <li>Direct calls on {@code this}: {@code link(controller: 'home')},
+ *       {@code message(code: 'key')}</li>
+ *   <li>Namespaced calls via a namespace dispatcher property:
+ *       {@code g.message(code: 'key')}, {@code my.customTag(attr: 'val')}</li>
+ * </ul>
+ *
+ * @since 7.0
+ */
+class ControllerTagLibTypeCheckingExtension extends 
GroovyTypeCheckingExtensionSupport.TypeCheckingDSL {
+
+    @Override
+    Object run() {
+        beforeVisitClass { ClassNode classNode ->
+            newScope {
+                isController = 
classNode.name.endsWith(ControllerArtefactHandler.TYPE)
+                dynamicNamespaceProperties = [] as Set
+            }
+        }
+
+        afterVisitClass { ClassNode classNode ->
+            scopeExit()
+        }
+
+        unresolvedVariable { VariableExpression ve ->
+            if (currentScope?.isController) {
+                currentScope.dynamicNamespaceProperties << ve
+                return makeDynamic(ve)
+            }
+            null
+        }

Review Comment:
   The only other solution would be to somehow notify the compiler of 
namespaces on the classpath, which would require saving the known namespaces 
and accessing them in the compiler.  This is probably possible, but out of 
scope of this change.  Ithink this is a net benefit, but we probably should 
ticket this as an enhancement
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to