Skip to content

Commit

Permalink
[FIX] XMLTemplateAnalyzer: Fix detection of nested views
Browse files Browse the repository at this point in the history
Fixes: SAP/ui5-tooling#624
JIRA: CPOUI5FOUNDATION-484
  • Loading branch information
matz3 committed Oct 28, 2022
1 parent acb3d9f commit 1cbfd82
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
51 changes: 51 additions & 0 deletions lib/lbt/analyzer/XMLTemplateAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,20 @@ const FRAGMENT_FRAGMENTNAME_ATTRIBUTE = "fragmentName";
const FRAGMENT_TYPE_ATTRIBUTE = "type";

// different view types
const VIEW_MODULE = "sap/ui/core/mvc/View.js";
const HTMLVIEW_MODULE = "sap/ui/core/mvc/HTMLView.js";
const JSVIEW_MODULE = "sap/ui/core/mvc/JSView.js";
const JSONVIEW_MODULE = "sap/ui/core/mvc/JSONView.js";
const XMLVIEW_MODULE = "sap/ui/core/mvc/XMLView.js";
const TEMPLATEVIEW_MODULE = "sap/ui/core/mvc/TemplateView.js";
const ANYVIEW_VIEWNAME_ATTRIBUTE = "viewName";
const XMLVIEW_CONTROLLERNAME_ATTRIBUTE = "controllerName";
const XMLVIEW_RESBUNDLENAME_ATTRIBUTE = "resourceBundleName";
const XMLVIEW_CORE_REQUIRE_ATTRIBUTE_NS = {
uri: "sap.ui.core",
local: "require"
};
const VIEW_TYPE_ATTRIBUTE = "type";

/*
* Helper to simplify access to node attributes.
Expand Down Expand Up @@ -347,6 +350,54 @@ class XMLTemplateAnalyzer {
// console.log("child view detected %s", childViewModuleName);
this._addDependency(childViewModuleName, conditional);
}
} else if ( moduleName === TEMPLATEVIEW_MODULE ) {
const viewName = getAttribute(node, ANYVIEW_VIEWNAME_ATTRIBUTE);
if ( viewName ) {
const childViewModuleName = ModuleName.fromUI5LegacyName( viewName, ".view.tmpl" );
// console.log("child view detected %s", childViewModuleName);
this._addDependency(childViewModuleName, conditional);
}
} else if ( moduleName === VIEW_MODULE ) {
const viewName = getAttribute(node, ANYVIEW_VIEWNAME_ATTRIBUTE);
if ( viewName ) {
let childViewModuleName;

if (viewName.startsWith("module:")) {
childViewModuleName = viewName.slice("module:".length) + ".js";
} else {
const viewType = getAttribute(node, VIEW_TYPE_ATTRIBUTE);

let viewTypeExtension;

switch (viewType) {
case "JS":
viewTypeExtension = ".view.js";
break;
case "JSON":
viewTypeExtension = ".view.json";
break;
case "Template":
viewTypeExtension = ".view.tmpl";
break;
case "XML":
viewTypeExtension = ".view.xml";
break;
case "HTML":
viewTypeExtension = ".view.html";
break;
default:
log.warn(`Unable to analyze sap.ui5/rootView: Unknown type '${viewType}'`);
}

if (viewTypeExtension) {
childViewModuleName = ModuleName.fromUI5LegacyName(viewName, viewTypeExtension);
}
}
if (childViewModuleName) {
// console.log("child view detected %s", childViewModuleName);
this._addDependency(childViewModuleName, conditional);
}
}
}
} catch (err) {
// ignore missing resources
Expand Down
55 changes: 55 additions & 0 deletions test/lib/lbt/analyzer/XMLTemplateAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,61 @@ test("integration: Analysis of an xml view with core:require (missing comma, par
"Implicit dependency should be added since an XMLView is analyzed");
});

test("integration: Analysis of an xml view with nested views", async (t) => {
const xml = `<mvc:View xmlns:mvc="sap.ui.core.mvc" xmlns:core="sap.ui.core" xmlns="sap.m">
<mvc:XMLView viewName="myapp.views.MyXMLView"/>
<mvc:JSView viewName="myapp.views.MyJSView"/>
<mvc:JSONView viewName="myapp.views.MyJSONView"/>
<mvc:TemplateView viewName="myapp.views.MyTemplateView"/>
<mvc:HTMLView viewName="myapp.views.MyHTMLView"/>
<mvc:View viewName="module:myapp/views/MyTypedView" />
<mvc:View viewName="myapp.views.MyXMLView2" type="XML" />
<mvc:View viewName="myapp.views.MyJSView2" type="JS" />
<mvc:View viewName="myapp.views.MyJSONView2" type="JSON" />
<mvc:View viewName="myapp.views.MyTemplateView2" type="Template" />
<mvc:View viewName="myapp.views.MyHTMLView2" type="HTML" />
<mvc:View viewName="myapp.views.UnknownViewType" type="unknown" />
</mvc:View>`;

const moduleInfo = new ModuleInfo();

const analyzer = new XMLTemplateAnalyzer(fakeMockPool);
await analyzer.analyzeView(xml, moduleInfo);
t.deepEqual(moduleInfo.dependencies,
[
"sap/ui/core/mvc/XMLView.js",
"myapp/views/MyXMLView.view.xml",

"sap/ui/core/mvc/JSView.js",
"myapp/views/MyJSView.view.js",

"sap/ui/core/mvc/JSONView.js",
"myapp/views/MyJSONView.view.json",

"sap/ui/core/mvc/TemplateView.js",
"myapp/views/MyTemplateView.view.tmpl",

"sap/ui/core/mvc/HTMLView.js",
"myapp/views/MyHTMLView.view.html",

"sap/ui/core/mvc/View.js",
"myapp/views/MyTypedView.js",

"myapp/views/MyXMLView2.view.xml",
"myapp/views/MyJSView2.view.js",
"myapp/views/MyJSONView2.view.json",
"myapp/views/MyTemplateView2.view.tmpl",
"myapp/views/MyHTMLView2.view.html"
], "Dependencies should come from the XML template");
t.false(moduleInfo.isImplicitDependency("sap/ui/core/mvc/XMLView.js"),
"XMLView is a strict dependency as the XMLView also has a nested XMLView");
});

test("integration: Analysis of an xml fragment", async (t) => {
const xml = `<HBox xmlns:m="sap.m" xmlns:l="sap.ui.layout" controllerName="myController">
<items>
Expand Down

0 comments on commit 1cbfd82

Please sign in to comment.