ui5-community / babel-plugin-transform-modules-ui5

A Babel transformer plugin for OpenUI5/SAPUI5. It allows you to develop UI5 applications by using the latest ECMAScript or TypeScript, including new syntax and objective oriented programming technology.
MIT License
34 stars 16 forks source link

File based namespace does not work #102

Closed plantago closed 1 year ago

plantago commented 1 year ago

I tried not to use JSDoc @namespace or decorator @namespace() and also specified the sourceRoot setting in .babelrc but my class was not converted to BaseClass.extend(...) form.

I have debugged the plugin and have found that function shouldConvertClass does not check classInfo.fileNamespace in the following if statement:

  if (
    classInfo.name ||
    classInfo.alias ||
    classInfo.controller ||
    classInfo.namespace
  ) {
    return true;
  }

If I add classInfo.fileNamespace:

  if (
    classInfo.name ||
    classInfo.alias ||
    classInfo.controller ||
    classInfo.namespace ||
    classInfo.fileNamespace // <- this is added
  ) {
    return true;
  }

then my class is converted.

petermuessig commented 1 year ago

Good catch, will add it in the next patch! Thx, @plantago

petermuessig commented 1 year ago

Hi @plantago ,

after looking into your problem and suggestion a bit more in detail, I think that's not what @r-murphy has intended with the sourceRoot property. While trying your suggested fix, I found some tests which failed and this made me reconsidering and investigating how this option finally was meant to work.

In cases like controllers with the extension .controller.js (even though if this is still valid or not) the plugin by default converts this class unless the option autoConvertAllExtendClasses is set to false. In this case and without specifying the namespace with a decorator or a JSDoc annotation, the name is derived relative to the sourceRoot. If we now change the behavior here, all classes will be auto converted just with the definition of the sourceRoot property. This will change affect existing apps now so that all classes extending others will be converted. AFAICS this is not expected.

But in your case if you want to make this work, you can force your expected behavior by specifying the sourceRoot and set the plugin option autoConvertAllExtendClasses to true.

HTH and BR, Peter