babel-plugin-transform-modules-ui5 7.5.0 → 7.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,28 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [7.6.1](https://github.com/ui5-community/babel-plugin-transform-modules-ui5/compare/v7.6.0...v7.6.1) (2024-09-13)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * support wrapped controller extension assignments ([dfa51b6](https://github.com/ui5-community/babel-plugin-transform-modules-ui5/commit/dfa51b613259aeedb3f20987f5e5165ca35545d8))
12
+
13
+
14
+
15
+
16
+
17
+ # [7.6.0](https://github.com/ui5-community/babel-plugin-transform-modules-ui5/compare/v7.5.0...v7.6.0) (2024-07-22)
18
+
19
+
20
+ ### Features
21
+
22
+ * support sap.ui.require for [@sap](https://github.com/sap)UiRequire annotated modules ([#131](https://github.com/ui5-community/babel-plugin-transform-modules-ui5/issues/131)) ([23c4ac6](https://github.com/ui5-community/babel-plugin-transform-modules-ui5/commit/23c4ac67eef5a825692c528852ef62bbd621d13e))
23
+
24
+
25
+
26
+
27
+
6
28
  # [7.5.0](https://github.com/ui5-community/babel-plugin-transform-modules-ui5/compare/v7.4.2...v7.5.0) (2024-07-12)
7
29
 
8
30
 
@@ -161,24 +161,20 @@ function convertClassToUI5Extend(path, node, classInfo, extraStaticProps, import
161
161
  // @transformControllerExtension marker, because it is not a type assignment. In the resulting code, the
162
162
  // "ControllerExtension.use(...)" part should be removed and the content of the brackets should be assigned
163
163
  // directly to the member property.
164
- if (_core.types.isCallExpression(member.value)) {
165
- const callee = member.value.callee;
166
- if (_core.types.isMemberExpression(callee) && _core.types.isIdentifier(callee.object) && _core.types.isIdentifier(callee.property) && callee.property.name === "use" // we are looking for "ControllerExtension.use(...)"
167
- ) {
168
- var _memberPath$hub2, _callee$object, _importDeclaration$so;
169
- const importDeclaration = (0, _imports.getImportDeclaration)(memberPath === null || memberPath === void 0 || (_memberPath$hub2 = memberPath.hub) === null || _memberPath$hub2 === void 0 || (_memberPath$hub2 = _memberPath$hub2.file) === null || _memberPath$hub2 === void 0 || (_memberPath$hub2 = _memberPath$hub2.opts) === null || _memberPath$hub2 === void 0 ? void 0 : _memberPath$hub2.filename, callee === null || callee === void 0 || (_callee$object = callee.object) === null || _callee$object === void 0 ? void 0 : _callee$object.name // usually, but not necessarily always: "ControllerExtension"...
170
- );
171
- // ...hence we rather look at the imported module name to be sure
172
- if ((importDeclaration === null || importDeclaration === void 0 || (_importDeclaration$so = importDeclaration.source) === null || _importDeclaration$so === void 0 ? void 0 : _importDeclaration$so.value) === "sap/ui/core/mvc/ControllerExtension") {
173
- if (!member.value.arguments || member.value.arguments.length !== 1) {
174
- // exactly one argument must be there
175
- throw memberPath.buildCodeFrameError(`ControllerExtension.use() must be called with exactly one argument but has ${member.value.arguments ? member.value.arguments.length : 0}`);
176
- }
177
- member.value = member.value.arguments[0];
178
- extendProps.unshift(buildObjectProperty(member)); // add it to the properties of the extend() config object
179
- continue; // prevent the member from also being added to the constructor
180
- }
181
- }
164
+ const rightSide = member.value;
165
+ if (isCallToControllerExtensionUse(rightSide, memberPath)) {
166
+ member.value = rightSide.arguments[0];
167
+ extendProps.unshift(buildObjectProperty(member)); // add it to the properties of the extend() config object
168
+ continue; // prevent the member from also being added to the constructor
169
+ }
170
+
171
+ // code instrumentation sometimes wraps ControllerExtension.use() calls like:
172
+ // this.routing = (cov_1uvvg22e7l().s[5]++, ControllerExtension.use(Routing.override({ … })));
173
+ if (_core.types.isSequenceExpression(rightSide) && isCallToControllerExtensionUse(rightSide.expressions[rightSide.expressions.length - 1], memberPath)) {
174
+ rightSide.expressions[rightSide.expressions.length - 1] = rightSide.expressions[rightSide.expressions.length - 1].arguments[0];
175
+ member.value = rightSide;
176
+ extendProps.unshift(buildObjectProperty(member)); // add it to the properties of the extend() config object
177
+ continue; // prevent the member from also being added to the constructor
182
178
  }
183
179
 
184
180
  // Special handling for TypeScript limitation where metadata, renderer and overrides must be properties.
@@ -212,6 +208,35 @@ function convertClassToUI5Extend(path, node, classInfo, extraStaticProps, import
212
208
  }
213
209
  }
214
210
 
211
+ /**
212
+ * Checks whether the given thing is a CallExpression that calls ControllerExtension.use(...)
213
+ *
214
+ * @param {*} expression the thing to check - does not need to be a CallExpression
215
+ * @param {string} memberPath
216
+ * @returns true if the given expression is a CallExpression that calls ControllerExtension.use(...)
217
+ */
218
+ function isCallToControllerExtensionUse(expression, memberPath) {
219
+ if (!_core.types.isCallExpression(expression)) {
220
+ return false;
221
+ }
222
+ const callee = expression.callee;
223
+ if (_core.types.isMemberExpression(callee) && _core.types.isIdentifier(callee.object) && _core.types.isIdentifier(callee.property) && callee.property.name === "use" // we are looking for "ControllerExtension.use(...)"
224
+ ) {
225
+ var _memberPath$hub2, _callee$object, _importDeclaration$so;
226
+ const importDeclaration = (0, _imports.getImportDeclaration)(memberPath === null || memberPath === void 0 || (_memberPath$hub2 = memberPath.hub) === null || _memberPath$hub2 === void 0 || (_memberPath$hub2 = _memberPath$hub2.file) === null || _memberPath$hub2 === void 0 || (_memberPath$hub2 = _memberPath$hub2.opts) === null || _memberPath$hub2 === void 0 ? void 0 : _memberPath$hub2.filename, callee === null || callee === void 0 || (_callee$object = callee.object) === null || _callee$object === void 0 ? void 0 : _callee$object.name // usually, but not necessarily always: "ControllerExtension"...
227
+ );
228
+ // ...hence we rather look at the imported module name to be sure
229
+ if ((importDeclaration === null || importDeclaration === void 0 || (_importDeclaration$so = importDeclaration.source) === null || _importDeclaration$so === void 0 ? void 0 : _importDeclaration$so.value) === "sap/ui/core/mvc/ControllerExtension") {
230
+ if (!expression.arguments || expression.arguments.length !== 1) {
231
+ // exactly one argument must be there
232
+ throw memberPath.buildCodeFrameError(`ControllerExtension.use() must be called with exactly one argument but has ${expression.arguments ? expression.arguments.length : 0}`);
233
+ }
234
+ return true;
235
+ }
236
+ }
237
+ return false; // return false if not a match
238
+ }
239
+
215
240
  // Arrow function properties need to get moved to the constructor so that
216
241
  // they're bound properly to the class instance, to align with the spec.
217
242
  // For controllers, use onInit rather than constructor, since controller constructors don't work.
@@ -139,14 +139,19 @@ function wrap(visitor, programNode, opts) {
139
139
  if (imports.some(imp => imp.interop)) {
140
140
  body.unshift(th.buildDefaultImportInterop());
141
141
  }
142
- const define = generateDefine(body, imports, exportGlobal || opts.exportAllGlobal, hasUseStrict(programNode));
142
+
143
+ // should we use sap.ui.require instead of sap.ui.define?
144
+ let useSapUiRequire = hasUseSapUiRequire(visitor.parent.comments, body, true);
145
+
146
+ // generate the sap.ui.define or sap.ui.require
147
+ const defineOrRequire = generateDefineOrRequire(body, imports, exportGlobal || opts.exportAllGlobal, useSapUiRequire);
143
148
 
144
149
  // add the "use strict" directive if not on program node
145
150
  if (!opts.neverUseStrict && !hasUseStrict(programNode)) {
146
- const defineFnBody = define.expression.arguments[1].body;
147
- defineFnBody.directives = [_core.types.directive(_core.types.directiveLiteral("use strict")), ...(defineFnBody.directives || [])];
151
+ const defineOrRequireFnBody = defineOrRequire.expression.arguments[1].body;
152
+ defineOrRequireFnBody.directives = [_core.types.directive(_core.types.directiveLiteral("use strict")), ...(defineOrRequireFnBody.directives || [])];
148
153
  }
149
- programNode.body = [...preDefine, define];
154
+ programNode.body = [...preDefine, defineOrRequire];
150
155
 
151
156
  // if a copyright comment is present we append it to the new program node
152
157
  if (copyright && visitor.parent) {
@@ -157,11 +162,36 @@ function wrap(visitor, programNode, opts) {
157
162
  function hasUseStrict(node) {
158
163
  return (node.directives || []).some(directive => directive.value.value === "use strict");
159
164
  }
160
- function generateDefine(body, imports, exportGlobal) {
165
+ function hasUseSapUiRequire(comments, body, remove) {
166
+ // detect the @sapUiRequire comment
167
+ return comments.some(comment => {
168
+ let found = false;
169
+ // check for existing comment block
170
+ if (comment.type === "CommentBlock") {
171
+ found = comment.value.trim() === "@sapUiRequire";
172
+ }
173
+ // remove the comment (if it is somewhere in the body)
174
+ if (found && remove) {
175
+ body === null || body === void 0 || body.forEach(node => {
176
+ (0, _core.traverse)(node, {
177
+ enter(path) {
178
+ ["leadingComments", "trailingComments", "innerComments"].forEach(key => {
179
+ var _path$node$key;
180
+ path.node[key] = (_path$node$key = path.node[key]) === null || _path$node$key === void 0 ? void 0 : _path$node$key.filter(c => c !== comment);
181
+ });
182
+ },
183
+ noScope: true
184
+ });
185
+ });
186
+ }
187
+ return found;
188
+ });
189
+ }
190
+ function generateDefineOrRequire(body, imports, exportGlobal, useRequire) {
161
191
  const defineOpts = {
162
192
  SOURCES: _core.types.arrayExpression(imports.map(i => _core.types.stringLiteral(i.src))),
163
193
  PARAMS: imports.map(i => _core.types.identifier(i.tmpName)),
164
194
  BODY: body
165
195
  };
166
- return exportGlobal ? th.buildDefineGlobal(defineOpts) : th.buildDefine(defineOpts);
196
+ return useRequire ? th.buildRequire(defineOpts) : exportGlobal ? th.buildDefineGlobal(defineOpts) : th.buildDefine(defineOpts);
167
197
  }
@@ -5,13 +5,18 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.buildInheritingFunction = exports.buildInheritingConstructor = exports.buildExtendAssign = exports.buildExportDefault = exports.buildDynamicImportHelper = exports.buildDefineGlobal = exports.buildDefine = exports.buildDefaultImportInterop = exports.buildDefaultImportDeconstructor = exports.buildDeclareExports = exports.buildConstDeclaration = exports.buildAssign = exports.buildAllExportHelper = exports.buildAllExport = void 0;
7
7
  exports.buildNamedExport = buildNamedExport;
8
- exports.exportsIdentifier = exports.buildTempExport = exports.buildReturnExports = exports.buildReturn = exports.buildNamedImportDestructor = void 0;
8
+ exports.exportsIdentifier = exports.buildTempExport = exports.buildReturnExports = exports.buildReturn = exports.buildRequire = exports.buildNamedImportDestructor = void 0;
9
9
  var _core = require("@babel/core");
10
10
  const exportName = "__exports";
11
11
  const exportsIdentifier = exports.exportsIdentifier = _core.types.identifier(exportName);
12
12
  const buildAssign = exports.buildAssign = (0, _core.template)(`
13
13
  OBJECT.NAME = VALUE;
14
14
  `);
15
+ const buildRequire = exports.buildRequire = (0, _core.template)(`
16
+ sap.ui.require(SOURCES, function (PARAMS) {
17
+ BODY;
18
+ });
19
+ `);
15
20
  const buildDefine = exports.buildDefine = (0, _core.template)(`
16
21
  sap.ui.define(SOURCES, function (PARAMS) {
17
22
  BODY;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "babel-plugin-transform-modules-ui5",
3
- "version": "7.5.0",
3
+ "version": "7.6.1",
4
4
  "description": "An unofficial babel plugin for SAP UI5.",
5
5
  "main": "dist/index.js",
6
6
  "repository": {
@@ -94,5 +94,5 @@
94
94
  "prettier": "^3.1.0",
95
95
  "rimraf": "^5.0.5"
96
96
  },
97
- "gitHead": "afd2f7c5644541c95ea3218a306fce1c71391050"
97
+ "gitHead": "bd1be642e7f4e1849a2716cefa64f4e048f42bae"
98
98
  }