Skip to content

Commit 0fdb427

Browse files
committed
Handle ChainExpression as conditional
1 parent a55d570 commit 0fdb427

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

lib/lbt/analyzer/JSModuleAnalyzer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const EnrichedVisitorKeys = (function() {
6666
BreakStatement: [],
6767
CallExpression: [], // special handling
6868
CatchClause: ["param", "body"],
69-
ChainExpression: [],
69+
ChainExpression: ["expression"],
7070
ClassBody: [],
7171
ClassDeclaration: [],
7272
ClassExpression: [],

test/fixtures/lbt/modules/es6-syntax.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ sap.ui.define([
4040
};
4141

4242
// chain expression
43-
44-
// TODO: This is not detected as conditional dependency
45-
// sap?.ui?.require(["conditional/module4"]);
43+
sap?.ui?.require(["conditional/module4"]);
4644

4745
// iterator pattern
4846
const iterator = {

test/lib/lbt/analyzer/JSModuleAnalyzer.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ test("ES6 Syntax", (t) => {
495495
// "conditional/module11.js", // TODO: see es6-syntax.js
496496
"conditional/module2.js",
497497
"conditional/module3.js",
498-
// "conditional/module4.js", // TODO: see es6-syntax.js
498+
"conditional/module4.js",
499499
"conditional/module6.js",
500500
"conditional/module7.js",
501501
"conditional/module8.js",
@@ -644,6 +644,38 @@ test("ES6 Template Literal in sap.ui.predefine", (t) => {
644644
});
645645
});
646646

647+
test("ChainExpression", (t) => {
648+
const content = `
649+
sap.ui.define(['require', 'static/module1'], (require) => {
650+
sap?.ui?.require?.(['conditional/module2']);
651+
sap?.ui?.requireSync?.('conditional/module3');
652+
jQuery?.sap?.require?.('conditional.module4');
653+
require?.(['conditional/module5']);
654+
});`;
655+
const info = analyzeString(content, "modules/ChainExpression.js");
656+
657+
const expected = [
658+
"conditional/module2.js",
659+
"conditional/module3.js",
660+
"conditional/module4.js",
661+
"conditional/module5.js",
662+
"jquery.sap.global.js",
663+
"static/module1.js",
664+
];
665+
const actual = info.dependencies.sort();
666+
t.deepEqual(actual, expected, "module dependencies should match");
667+
expected.forEach((dep) => {
668+
t.is(info.isConditionalDependency(dep), /^conditional\//.test(dep),
669+
`only dependencies to 'conditional/*' modules should be conditional (${dep})`);
670+
t.is(info.isImplicitDependency(dep), !/^(?:conditional|static)\//.test(dep),
671+
`all dependencies other than 'conditional/*' and 'static/*' should be implicit (${dep})`);
672+
});
673+
t.false(info.dynamicDependencies,
674+
`no use of dynamic dependencies should have been detected`);
675+
t.false(info.rawModule,
676+
`ui5 module`);
677+
});
678+
647679
test("Dynamic import (declare/require)", (t) => {
648680
return analyze("modules/declare_dynamic_require.js").then((info) => {
649681
t.true(info.dynamicDependencies,

0 commit comments

Comments
 (0)