Skip to content

feat: support using predefined controller extensions#120

Merged
akudev merged 1 commit intoui5-community:mainfrom
akudev:main
Nov 30, 2023
Merged

feat: support using predefined controller extensions#120
akudev merged 1 commit intoui5-community:mainfrom
akudev:main

Conversation

@akudev
Copy link
Copy Markdown
Collaborator

@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 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 UI5/typescript#420
@akudev
Copy link
Copy Markdown
Collaborator 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
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