Skip to content

Commit

Permalink
Fix + update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matz3 committed Apr 21, 2022
1 parent 2daf76d commit 57bcf31
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
15 changes: 13 additions & 2 deletions lib/lbt/analyzer/JSModuleAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ class JSModuleAnalyzer {
*/
let bIsUi5Module = false;

// Module name via @ui5-bundle comment in first line. Overrides all other main module candidates.
let firstLineBundleName;

// first analyze the whole AST...
visit(ast, false);

Expand All @@ -314,7 +317,12 @@ class JSModuleAnalyzer {
log.verbose(`bundle include directive ${subModule}`);
} else if ( comment.value.startsWith("@ui5-bundle ") ) {
const bundleName = comment.value.slice("@ui5-bundle ".length);
setMainModuleInfo(bundleName, null);
if (comment.start === 0) {
// Remember the name from the first line to use it as final name
firstLineBundleName = bundleName;
} else {
setMainModuleInfo(bundleName, null);
}
log.verbose(`bundle name directive ${bundleName}`);
} else {
log.warn(`unrecognized bundle directive ${comment.value}`);
Expand All @@ -324,7 +332,10 @@ class JSModuleAnalyzer {
}

// ...and finally take conclusions about the file's content
if ( !mainModuleFound ) {
if ( firstLineBundleName ) {
// If the first line has a bundle name, use it and override all other found names
info.name = firstLineBundleName;
} else if ( !mainModuleFound ) {
// if there's exactly one module definition in this file but it didn't
// immediately qualify as main module, make it now the main module
if ( candidateName != null && nModuleDeclarations == 1 ) {
Expand Down
18 changes: 9 additions & 9 deletions test/lib/lbt/analyzer/JSModuleAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ jQuery.sap.registerPreloadedModules({
"submodule from jQuery.sap.registerPreloadedModules");
});

test("Module that contains jQuery.sap.declare should not be derived as subModule", (t) => {
test("Module that contains jQuery.sap.declare should be derived as subModule", (t) => {
const content = `
sap.ui.define([], function() {
jQuery.sap.declare("foo.bar");
Expand All @@ -695,10 +695,10 @@ sap.ui.define([], function() {
t.is(info.name, "modules/module-with-jquery-sap-declare.js", "TBD");
t.deepEqual(info.dependencies, ["jquery.sap.global.js"], "TBD");
t.is(info.rawModule, false, "TBD");
// t.is(info.format, "ui5-define", "TBD");
t.is(info.format, "ui5-declare", "TBD"); // Shouldn't this be ui5-define?
t.is(info.requiresTopLevelScope, false, "TBD");
t.deepEqual(info.subModules, [],
"no subModules should be detected");
t.deepEqual(info.subModules, ["foo/bar.js"],
"jQuery.sap.declare subModule should be detected");
});

test("Bundle that contains jQuery.sap.declare (sap.ui.predefine) should not be derived as module name", (t) => {
Expand All @@ -711,11 +711,11 @@ sap.ui.predefine("test1/module1", [], function() {
t.is(info.name, "test1/library-preload.js",
"TBD");
t.is(info.rawModule, false, "TBD");
// t.is(info.format, "ui5-declare", "TBD");
t.is(info.format, "ui5-declare", "TBD"); // Shouldn't this be ui5-define?
t.is(info.requiresTopLevelScope, false, "TBD");
t.deepEqual(info.subModules, ["test1/module1.js"],
t.deepEqual(info.subModules, ["test1/module1.js"], // Shouldn't foo/bar.js be listed here?
"subModule via sap.ui.predefine should be detected");
t.deepEqual(info.dependencies, ["ui5loader-autoconfig.js"],
t.deepEqual(info.dependencies, ["jquery.sap.global.js"],
"TBD");
});

Expand All @@ -734,9 +734,9 @@ sap.ui.require.preload({
t.is(info.name, "test1/library-preload.js",
"TBD");
t.is(info.rawModule, false, "TBD");
// t.is(info.format, "ui5-declare", "TBD");
t.is(info.format, "ui5-define", "TBD");
t.is(info.requiresTopLevelScope, false, "TBD");
t.deepEqual(info.subModules, ["test1/module1.js"],
t.deepEqual(info.subModules, ["test1/module1.js"], // Shouldn't foo/bar.js be listed here?
"subModule via sap.ui.predefine should be detected");
t.deepEqual(info.dependencies, ["ui5loader-autoconfig.js"],
"TBD");
Expand Down

0 comments on commit 57bcf31

Please sign in to comment.