Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support using predefined controller extensions #120

Merged
merged 1 commit into from Nov 30, 2023

Conversation

akudev
Copy link
Contributor

@akudev akudev commented Nov 28, 2023

When using a controller extension in your own controller as a kind of mix-in, UI5 currently expects in the "extend" config object the extension class to be assigned to a property.

In TypeScript, however, the TS compiler must see the property being typed with an instance of the extension class, so subsequent calls to the extension can be written like they should
(this.routing.doSomething()).

This change introduces the "@transformControllerExtension" comment and decorator which triggers a special kind of transformation for annotated class properties. The controller extension type assigned to the subsequent class property is then assigned as class in the extend object in JavaScript. And this assignment is not moved into the constructor like it normally would be.

Fixes SAP/ui5-typescript#420

When using a controller extension in your own controller as a kind of
mix-in, UI5 currently expects in the "extend" config object the
extension class to be assigned to a property.

In TypeScript, however, the TS compiler must see the property being
typed with an *instance* of the extension class, so subsequent calls to
the extension can be written like they should
(this.routing.doSomething()).

This change introduces the "@transformControllerExtension" comment and
decorator which triggers a special kind of transformation for annotated
class properties. The controller extension type assigned to the
subsequent class property is then assigned as class in the extend object
in JavaScript. And this assignment is not moved into the constructor
like it normally would be.

Fixes SAP/ui5-typescript#420
@akudev
Copy link
Contributor Author

akudev commented Nov 29, 2023

The high-level approach here is:

  • In real life UI5 build pipelines the TypeScript transpiler usually runs first and immediately in the Program.enter phase removes unneeded imports. As a controller extension usage like e.g. the class property declaration routing: Routing is just a type assignment, the import of Routing just imports a type and appears unneeded from TS perspective. Hence, now the pre() hook is used to get a list of all initially present imports.
  • In the regular class transformation, class properties which represent controller extensions are recognized by the @controllerExtension comment or decorator (and confirmed by looking at their structure) and converted to a value assignment (that is needed at runtime).
  • If the respective import has been removed (see above), it is re-added.
  • In the unit tests, however, TypeScript runs after this plugin. This means that the import would still look good, but be removed later. To prevent this, a binding reference to the newly added value assignment is created. This makes TypeScript notice that the import is still needed.

@akudev akudev merged commit d7cb66c into ui5-community:main Nov 30, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Documentation / Feature request: How to use controller extensions in TS?
2 participants