Vampire commented on code in PR #275:
URL: https://github.com/apache/groovy-geb/pull/275#discussion_r2167386690
##########
module/geb-core/src/main/groovy/geb/js/JavascriptInterface.groovy:
##########
@@ -53,8 +53,11 @@ class JavascriptInterface {
jsArgs = args[0..(args.size() - 2)]
}
+ if (script instanceof Closure) {
+ script = script()
+ }
if (!(script instanceof CharSequence)) {
- throw new IllegalArgumentException("The last argument to the js
function must be string-like")
+ throw new IllegalArgumentException("The last argument to the js
function must be string-like or a Closure returning a string-like")
Review Comment:
> "throw stuff in the method"
Well, good ol' Groovy duck-typing, isn't it? :-D
> Why not other functional interfaces?
> Can you say a bit more about the felt need behind this PR? What led you
here?
Well, yeah, other FI could be supported of course.
My main intention was to support Syntax that feels more natural and readable
to me.
Without this, you can for example do
```groovy
js.exec 1, 2, '''
someJsMethod(arguments[0], arguments[1]);
// lots of javascript
return true;
'''
```
But I having multiple comma-separated arguments and additionally multi-line
without parens feels a bit strange to me,
besides that you first have the arguments, then the script they are for and
that intermixed.
Or you can do
```groovy
js.exec(1, 2, '''
someJsMethod(arguments[0], arguments[1]);
// lots of javascript
return true;
''')
```
but that also imho does not read super nice.
So my target was to have the new syntax
```groovy
js.exec(1, 2) {
'''
someJsMethod(arguments[0], arguments[1]);
// lots of javascript
return true;
'''
}
```
where the arguments are comma-separated in parentheses and the script comes
in the following block outside the parentheses, to have a clearer separation
between the script and the arguments for the script and have an imho also much
better readable syntax.
--
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]