Skip to content

Commit aaa6e21

Browse files
committed
fix: fix review comments - maintain full SequenceExpression
1 parent c344794 commit aaa6e21

File tree

3 files changed

+62
-55
lines changed

3 files changed

+62
-55
lines changed

packages/plugin/__test__/__snapshots__/test.js.snap

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,9 +1885,10 @@ exports[`typescript ts-class-controller-extension-wrapped.ts 1`] = `
18851885
};
18861886
};
18871887
const MyExtendedController = Controller.extend("test.controller.MyExtendedController", {
1888-
routing3: Routing.override({}),
1889-
routing2: Routing.override({}),
1890-
routing: Routing
1888+
routing4: (cov_1uvvg22e7l().s[5]++, ControllerExtension.use(Routing.override({})), ControllerExtension.use(Routing)),
1889+
routing3: (cov_1uvvg22e7l().s[5]++, cov_1uvvg22e7l().s[5]++, ControllerExtension.use(Routing.override({}))),
1890+
routing2: (cov_1uvvg22e7l().s[5]++, ControllerExtension.use(Routing.override({}))),
1891+
routing: (cov_1uvvg22e7l().s[5]++, ControllerExtension.use(Routing))
18911892
});
18921893
return MyExtendedController;
18931894
});"

packages/plugin/__test__/fixtures/typescript/ts-class-controller-extension-wrapped.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ export default class MyExtendedController extends Controller {
1414
routing = (cov_1uvvg22e7l().s[5]++, ControllerExtension.use(Routing));
1515
routing2 = (cov_1uvvg22e7l().s[5]++, ControllerExtension.use(Routing.override({})));
1616
routing3 = (cov_1uvvg22e7l().s[5]++, cov_1uvvg22e7l().s[5]++, ControllerExtension.use(Routing.override({})));
17+
routing4 = (cov_1uvvg22e7l().s[5]++, ControllerExtension.use(Routing.override({})), ControllerExtension.use(Routing));
1718
}

packages/plugin/src/classes/helpers/classes.js

Lines changed: 57 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -222,60 +222,24 @@ export function convertClassToUI5Extend(
222222
// @transformControllerExtension marker, because it is not a type assignment. In the resulting code, the
223223
// "ControllerExtension.use(...)" part should be removed and the content of the brackets should be assigned
224224
// directly to the member property.
225+
const rightSide = member.value;
226+
if (isCallToControllerExtensionUse(rightSide, memberPath)) {
227+
member.value = rightSide.arguments[0];
228+
extendProps.unshift(buildObjectProperty(member)); // add it to the properties of the extend() config object
229+
continue; // prevent the member from also being added to the constructor
230+
}
231+
232+
// code instrumentation sometimes wraps ControllerExtension.use() calls like:
233+
// this.routing = (cov_1uvvg22e7l().s[5]++, ControllerExtension.use(Routing.override({ … })));
225234
if (
226-
t.isCallExpression(member.value) ||
227-
t.isSequenceExpression(member.value)
235+
t.isSequenceExpression(rightSide) &&
236+
rightSide.expressions.some((expression) =>
237+
isCallToControllerExtensionUse(expression, memberPath)
238+
)
228239
) {
229-
let callExpression = member.value;
230-
231-
// code instrumentation sometimes wraps it like:
232-
// this.routing = (cov_1uvvg22e7l().s[5]++, ControllerExtension.use(Routing.override({ … })));
233-
if (t.isSequenceExpression(member.value)) {
234-
// iterate through the expressions in the sequence
235-
for (const expr of member.value.expressions) {
236-
if (t.isCallExpression(expr)) {
237-
callExpression = expr;
238-
break;
239-
}
240-
}
241-
}
242-
243-
if (t.isCallExpression(callExpression)) {
244-
const callee = callExpression.callee;
245-
if (
246-
t.isMemberExpression(callee) &&
247-
t.isIdentifier(callee.object) &&
248-
t.isIdentifier(callee.property) &&
249-
callee.property.name === "use" // we are looking for "ControllerExtension.use(...)"
250-
) {
251-
const importDeclaration = getImportDeclaration(
252-
memberPath?.hub?.file?.opts?.filename,
253-
callee?.object?.name // usually, but not necessarily always: "ControllerExtension"...
254-
);
255-
// ...hence we rather look at the imported module name to be sure
256-
if (
257-
importDeclaration?.source?.value ===
258-
"sap/ui/core/mvc/ControllerExtension"
259-
) {
260-
if (
261-
!callExpression.arguments ||
262-
callExpression.arguments.length !== 1
263-
) {
264-
// exactly one argument must be there
265-
throw memberPath.buildCodeFrameError(
266-
`ControllerExtension.use() must be called with exactly one argument but has ${
267-
callExpression.arguments
268-
? callExpression.arguments.length
269-
: 0
270-
}`
271-
);
272-
}
273-
member.value = callExpression.arguments[0];
274-
extendProps.unshift(buildObjectProperty(member)); // add it to the properties of the extend() config object
275-
continue; // prevent the member from also being added to the constructor
276-
}
277-
}
278-
}
240+
member.value = rightSide;
241+
extendProps.unshift(buildObjectProperty(member)); // add it to the properties of the extend() config object
242+
continue; // prevent the member from also being added to the constructor
279243
}
280244

281245
// Special handling for TypeScript limitation where metadata, renderer and overrides must be properties.
@@ -314,6 +278,47 @@ export function convertClassToUI5Extend(
314278
}
315279
}
316280

281+
/**
282+
* Checks whether the given thing is a CallExpression that calls ControllerExtension.use(...)
283+
*
284+
* @param {*} expression the thing to check - does not need to be a CallExpression
285+
* @param {string} memberPath
286+
* @returns true if the given expression is a CallExpression that calls ControllerExtension.use(...)
287+
*/
288+
function isCallToControllerExtensionUse(expression, memberPath) {
289+
if (!t.isCallExpression(expression)) {
290+
return false;
291+
}
292+
const callee = expression.callee;
293+
if (
294+
t.isMemberExpression(callee) &&
295+
t.isIdentifier(callee.object) &&
296+
t.isIdentifier(callee.property) &&
297+
callee.property.name === "use" // we are looking for "ControllerExtension.use(...)"
298+
) {
299+
const importDeclaration = getImportDeclaration(
300+
memberPath?.hub?.file?.opts?.filename,
301+
callee?.object?.name // usually, but not necessarily always: "ControllerExtension"...
302+
);
303+
// ...hence we rather look at the imported module name to be sure
304+
if (
305+
importDeclaration?.source?.value ===
306+
"sap/ui/core/mvc/ControllerExtension"
307+
) {
308+
if (!expression.arguments || expression.arguments.length !== 1) {
309+
// exactly one argument must be there
310+
throw memberPath.buildCodeFrameError(
311+
`ControllerExtension.use() must be called with exactly one argument but has ${
312+
expression.arguments ? expression.arguments.length : 0
313+
}`
314+
);
315+
}
316+
return true;
317+
}
318+
}
319+
return false; // return false if not a match
320+
}
321+
317322
// Arrow function properties need to get moved to the constructor so that
318323
// they're bound properly to the class instance, to align with the spec.
319324
// For controllers, use onInit rather than constructor, since controller constructors don't work.

0 commit comments

Comments
 (0)