Skip to content

Commit 3792268

Browse files
committed
[INTERNAL] XMLCompositeAnalyzer and JSModuleAnalyzer arrow function usage
1 parent 3c77cfe commit 3792268

File tree

4 files changed

+100
-12
lines changed

4 files changed

+100
-12
lines changed

lib/lbt/analyzer/JSModuleAnalyzer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,9 @@ class JSModuleAnalyzer {
692692
if ( modules && modules.type == Syntax.ObjectExpression ) {
693693
modules.properties.forEach( function(property) {
694694
let moduleName = getPropertyKey(property);
695+
if ( !moduleName ) {
696+
return;
697+
}
695698
if ( namesUseLegacyNotation ) {
696699
moduleName = ModuleName.fromUI5LegacyName(moduleName);
697700
}

lib/lbt/analyzer/XMLCompositeAnalyzer.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,24 @@ class XMLCompositeAnalyzer {
1919
const XMLC = defineCall.findImportName("sap/ui/core/XMLComposite.js");
2020
// console.log("local name for XMLComposite: %s", XMLC);
2121
if ( XMLC && defineCall.factory ) {
22-
defineCall.factory.body.body.forEach( (stmt) => {
23-
if ( stmt.type === Syntax.VariableDeclaration ) {
24-
stmt.declarations.forEach( (decl) => {
25-
fragmentName = this._checkForXMLCClassDefinition( XMLC, decl.init ) || fragmentName;
26-
});
27-
} else if ( stmt.type === Syntax.ExpressionStatement &&
28-
stmt.expression.type === Syntax.AssignmentExpression ) {
29-
fragmentName = this._checkForXMLCClassDefinition( XMLC, stmt.expression.right ) || fragmentName;
30-
}
31-
});
32-
if ( fragmentName ) {
33-
const fragmentModule = ModuleName.fromUI5LegacyName( fragmentName, ".control.xml" );
22+
if (defineCall.factory.type === Syntax.ArrowFunctionExpression &&
23+
defineCall.factory.expression === true) {
24+
fragmentName = this._checkForXMLCClassDefinition(XMLC, defineCall.factory.body);
25+
} else {
26+
defineCall.factory.body.body.forEach((stmt) => {
27+
if (stmt.type === Syntax.VariableDeclaration) {
28+
stmt.declarations.forEach((decl) => {
29+
fragmentName = this._checkForXMLCClassDefinition(XMLC, decl.init) || fragmentName;
30+
});
31+
} else if (stmt.type === Syntax.ExpressionStatement &&
32+
stmt.expression.type === Syntax.AssignmentExpression) {
33+
fragmentName =
34+
this._checkForXMLCClassDefinition(XMLC, stmt.expression.right) || fragmentName;
35+
}
36+
});
37+
}
38+
if (fragmentName) {
39+
const fragmentModule = ModuleName.fromUI5LegacyName(fragmentName, ".control.xml");
3440
log.verbose("fragment control: add dependency to template fragment %s", fragmentModule);
3541
info.addDependency(fragmentModule);
3642
}

test/lib/lbt/analyzer/JSModuleAnalyzer.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,20 @@ jQuery.sap.registerPreloadedModules({
702702
"submodule from jQuery.sap.registerPreloadedModules");
703703
});
704704

705+
test("jQuery.sap.registerPreloadedModules (with ObjectExpression, version 1.0 and SpreadExpression)", (t) => {
706+
const content = `
707+
jQuery.sap.registerPreloadedModules({
708+
"modules": {
709+
...foo
710+
},
711+
"version": "1.0"
712+
});
713+
`;
714+
const info = analyzeString(content, "modules/registerPreloadedModules-ObjectExpression.js");
715+
t.deepEqual(info.subModules, [],
716+
"submodule from jQuery.sap.registerPreloadedModules are empty");
717+
});
718+
705719
test("jQuery.sap.registerPreloadedModules (with ObjectExpression, version 2.0)", (t) => {
706720
const content = `
707721
jQuery.sap.registerPreloadedModules({
@@ -716,6 +730,20 @@ jQuery.sap.registerPreloadedModules({
716730
"submodule from jQuery.sap.registerPreloadedModules");
717731
});
718732

733+
test("jQuery.sap.registerPreloadedModules (with ObjectExpression, version 2.0 and SpreadExpression)", (t) => {
734+
const content = `
735+
jQuery.sap.registerPreloadedModules({
736+
"modules": {
737+
...foo
738+
},
739+
"version": "2.0"
740+
});
741+
`;
742+
const info = analyzeString(content, "modules/registerPreloadedModules-ObjectExpression.js");
743+
t.deepEqual(info.subModules, [],
744+
"submodule from jQuery.sap.registerPreloadedModules are empty");
745+
});
746+
719747
test("Module that contains jQuery.sap.declare should be derived as subModule", (t) => {
720748
const content = `
721749
sap.ui.define([], function() {

test/lib/lbt/analyzer/XMLCompositeAnalyzer.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,57 @@ test("integration: XMLComposite code", async (t) => {
2323
"Dependency should be created from component name");
2424
});
2525

26+
// TODO: Is it intended to be not supported to directly return the value
27+
test("integration: XMLComposite code without VariableDeclaration", async (t) => {
28+
const code = `sap.ui.define([
29+
'jquery.sap.global', 'sap/ui/core/XMLComposite'],
30+
function(jQuery, XMLComposite) {
31+
"use strict";
32+
return XMLComposite.extend("composites.ButtonList", {});
33+
});`;
34+
35+
const ast = parseJS(code);
36+
37+
const analyzer = new XMLCompositeAnalyzer();
38+
const name = "composites.ButtonList";
39+
const moduleInfo = new ModuleInfo();
40+
await analyzer.analyze(ast, name, moduleInfo);
41+
t.deepEqual(moduleInfo.dependencies, ["composites/ButtonList.control.xml"],
42+
"Dependency should be created from component name");
43+
});
44+
45+
test("integration: XMLComposite code with arrow function", async (t) => {
46+
const code = `sap.ui.define([
47+
'jquery.sap.global', 'sap/ui/core/XMLComposite'],
48+
(jQuery, XMLComposite) => {
49+
return XMLComposite.extend("composites.ButtonList", {});
50+
});`;
51+
52+
const ast = parseJS(code);
53+
54+
const analyzer = new XMLCompositeAnalyzer();
55+
const name = "composites.ButtonList";
56+
const moduleInfo = new ModuleInfo();
57+
await analyzer.analyze(ast, name, moduleInfo);
58+
t.deepEqual(moduleInfo.dependencies, ["composites/ButtonList.control.xml"],
59+
"Dependency should be created from component name");
60+
});
61+
62+
test("integration: XMLComposite code with arrow function with implicit return", async (t) => {
63+
const code = `sap.ui.define([
64+
'jquery.sap.global', 'sap/ui/core/XMLComposite'],
65+
(jQuery, XMLComposite) => XMLComposite.extend("composites.ButtonList", {}));`;
66+
67+
const ast = parseJS(code);
68+
69+
const analyzer = new XMLCompositeAnalyzer();
70+
const name = "composites.ButtonList";
71+
const moduleInfo = new ModuleInfo();
72+
await analyzer.analyze(ast, name, moduleInfo);
73+
t.deepEqual(moduleInfo.dependencies, ["composites/ButtonList.control.xml"],
74+
"Dependency should be created from component name");
75+
});
76+
2677
test("analyze: not an XMLComposite module", async (t) => {
2778
const code = `sap.ui.define([
2879
'jquery.sap.global', 'sap/ui/core/XMLComposite'],

0 commit comments

Comments
 (0)