Skip to content

Support named parameters for executeJs. #24385

@Legioth

Description

@Legioth

Describe your motivation

For any non-trivial executeJs expression, it's good practice to extract each positional parameter such as $0 into a named variable, e.g. const foo = $0;. This enables using the value based on the name as doSomething(foo) instead of doSomething($0).

Describe the solution you'd like

Make it possible to provide explicitly named parameters from the server so that the JS expression can directly use that name without ever mentioning $0.

Usage could look like this:

element.executeJs("doSomething(foo)").withParameter("foo", "Some value");

PendingJavaScriptResult is also used by callJsFunction where a separate withParameter makes no sense. We should thus introduce a subtype of PendingJavaScriptResult with named parameter support that is used only by executeJs but not callJsFunction.

It should also not be allowed to add new parameters after isSentToBrowser() has changed to true.

A similar method should also be added to the JsFunction API.

Describe alternatives you've considered

As an alternative to chaining on the return value, a map of named parameters could also be passed directly to executeJs. The problem is that this would be ambiguous with a regular map Map since executeJs(script, Map.of("foo", "Some value")); could also be seen as passing a map as $0. That could be made compatible either by also passing the map as $0 or by introducing a new dedicated type that won't be used in existing code. I still prefer withParameter since it's more ergonomic to continue on the existing method rather than separately building a Map or a dedicated parameter-passing object.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Complexity: MediumA task requires medium efforts for solving
    No fields configured for Feature.

    Projects

    Status

    ⚒️ In progress

    Status

    Work In Progress

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions