From f5ac138c083d54de10e35bc88e7e2498647f3531 Mon Sep 17 00:00:00 2001 From: Frank Weigel Date: Thu, 5 Jul 2018 08:02:46 +0200 Subject: [PATCH] [FIX] Close gaps in routing support of ComponentAnalyzer - accept routes with multiple targets - merge target-local configuration with main router configuration --- lib/lbt/analyzer/ComponentAnalyzer.js | 29 ++++--- test/lib/lbt/analyzer/ComponentAnalyzer.js | 88 ++++++++++++++++++++++ 2 files changed, 108 insertions(+), 9 deletions(-) diff --git a/lib/lbt/analyzer/ComponentAnalyzer.js b/lib/lbt/analyzer/ComponentAnalyzer.js index 5fcefb6dc..accb2ec39 100644 --- a/lib/lbt/analyzer/ComponentAnalyzer.js +++ b/lib/lbt/analyzer/ComponentAnalyzer.js @@ -33,6 +33,13 @@ function each(obj, fn) { } } +function makeArray(value) { + if ( Array.isArray(value) ) { + return value; + } + return value == null ? [] : [value]; +} + /** * Analyzes the manifest for a Component.js to find more dependencies. * @since 1.47.0 @@ -142,15 +149,19 @@ class ComponentAnalyzer { * @private */ _visitRoute( route, routing, info ) { - const viewPath = routing.config.viewPath ? routing.config.viewPath + "." : ""; - const viewType = routing.config.viewType.toLowerCase(); - const target = routing.targets[route.target]; - if ( target && target.viewName ) { - const module = ModuleName.fromUI5LegacyName(viewPath + target.viewName, ".view." + viewType); - log.verbose("converting route to view dependency ", module); - // TODO make this a conditional dependency, depending on the pattern? - info.addDependency(module); - } + makeArray(route.target).forEach((targetRef) => { + const target = routing.targets[targetRef]; + if ( target && target.viewName ) { + // merge target config with default config + const config = Object.assign({}, routing.config, target); + const module = ModuleName.fromUI5LegacyName( + (config.viewPath ? config.viewPath + "." : "") + + config.viewName, ".view." + config.viewType.toLowerCase() ); + log.verbose("converting routing target '%s' to view dependency '%s'", targetRef, module); + // TODO make this a conditional dependency, depending on the pattern? + info.addDependency(module); + } + }); } } diff --git a/test/lib/lbt/analyzer/ComponentAnalyzer.js b/test/lib/lbt/analyzer/ComponentAnalyzer.js index e0c092b20..981d1b557 100644 --- a/test/lib/lbt/analyzer/ComponentAnalyzer.js +++ b/test/lib/lbt/analyzer/ComponentAnalyzer.js @@ -84,3 +84,91 @@ test("routing with routes as object", (t) => { const subject = new ComponentAnalyzer(mockPool); return subject.analyze({name: "test/Component.js"}, mockInfo); }); + +test("routing with route with multiple targets", (t) => { + const mockManifest = { + "sap.ui5": { + routing: { + config: { + viewPath: "test.view", + viewType: "XML" + }, + routes: { + test: { + target: ["test1", "test2"] + } + }, + targets: { + test1: {viewName: "Master"}, + test2: {viewName: "Detail"} + } + } + } + }; + + const mockPool = createMockPool("test/", mockManifest); + + const mockInfo = { + deps: [], + addDependency(name) { + this.deps.push(name); + } + }; + + const subject = new ComponentAnalyzer(mockPool); + return subject.analyze({name: "test/Component.js"}, mockInfo).then( () => { + t.deepEqual(mockInfo.deps, [ + "test/view/Master.view.xml", + "test/view/Detail.view.xml" + ], "dependencies should be correct"); + }); +}); + +test("routing with targets with local config", (t) => { + const mockManifest = { + "sap.ui5": { + routing: { + config: { + viewPath: "test.view", + viewType: "XML" + }, + routes: { + test1: { + target: "test1" + }, + test2: { + target: "test2" + } + }, + targets: { + test1: { + viewName: "Master", + viewType: "JS" + }, + test2: { + viewName: "Detail", + viewPath: "test.subview" + } + } + } + } + }; + + const mockPool = createMockPool("test/", mockManifest); + + const mockInfo = { + deps: [], + addDependency(name) { + this.deps.push(name); + } + }; + + const subject = new ComponentAnalyzer(mockPool); + return subject.analyze({name: "test/Component.js"}, mockInfo).then( () => { + t.deepEqual(mockInfo.deps, [ + "test/view/Master.view.js", + "test/subview/Detail.view.xml" + ], "dependencies should be correct"); + }); +}); +