Skip to content

Commit

Permalink
[FIX] iterate over routes using a for loop if it is an object (#31)
Browse files Browse the repository at this point in the history
Routes may be defined as an array or object in the manifest.
If it its an object we use `for .. in` to iterate over it, for arrays
we stay with forEach.

Fixes #30
  • Loading branch information
themasch authored and codeworrior committed Jun 28, 2018
1 parent 97559c9 commit e9823f6
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 11 deletions.
41 changes: 30 additions & 11 deletions lib/lbt/analyzer/ComponentAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,41 @@ class ComponentAnalyzer {

let routing = ui5.routing;
if ( routing ) {
// console.log("routing: ", routing);
routing.routes.forEach( (route) => {
let target = routing.targets[route.target];
if ( target && target.viewName ) {
let module = ModuleName.fromUI5LegacyName(
(routing.config.viewPath ? routing.config.viewPath + "." : "") +
target.viewName, ".view." + routing.config.viewType.toLowerCase() );
log.verbose("converting route to view dependency ", module);
// TODO make this a conditional dependency, depending on the pattern?
info.addDependency(module);
if (Array.isArray(routing.routes)) {
routing.routes.forEach((route) => this._visitRoute(route, routing, info));
} else {
for (let key in routing.routes) {
if (!routing.routes.hasOwnProperty(key)) {
continue;
}
const route = routing.routes[key];
this._visitRoute(route, routing, info);
}
});
}
}

return info;
}

/**
* called for any route, this adds the view used by a route as a dependency.
*
* @param {object} route the single route
* @param {object} routing the full routing object from the manifest
* @param {ModuleInfo} info ModuleInfo object that should be enriched
* @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);
}
}
}

module.exports = ComponentAnalyzer;
86 changes: 86 additions & 0 deletions test/lib/lbt/analyzer/ComponentAnalyzer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const {test} = require("ava");
const Path = require("path");
const ComponentAnalyzer = require("../../../../lib/lbt/analyzer/ComponentAnalyzer");


function createMockPool(path, manifest) {
const expectedPath = Path.join(path, "manifest.json");
return {
async findResource(name) {
if (name !== expectedPath) {
throw new Error(`unexpected resource name: ${name}, expected ${expectedPath}`);
}
return {
async buffer() {
return JSON.stringify(manifest);
}
};
}
};
}

test("routing with routes as array", (t) => {
const mockManifest = {
"sap.ui5": {
routing: {
config: {
viewPath: "test.view",
viewType: "XML"
},
routes: [
{
name: "test",
target: "test"
}
],
targets: {
test: {viewName: "App"}
}
}
}
};

const mockPool = createMockPool("test/", mockManifest);

const mockInfo = {
addDependency(name) {
t.is(name, "test/view/App.view.xml");
}
};

const subject = new ComponentAnalyzer(mockPool);
return subject.analyze({name: "test/Component.js"}, mockInfo);
});


test("routing with routes as object", (t) => {
const mockManifest = {
"sap.ui5": {
routing: {
config: {
viewPath: "test.view",
viewType: "XML"
},
routes: {
test: {
target: "test"
}
},
targets: {
test: {viewName: "App"}
}
}
}
};

const mockPool = createMockPool("test/", mockManifest);

const mockInfo = {
addDependency(name) {
t.is(name, "test/view/App.view.xml");
}
};

const subject = new ComponentAnalyzer(mockPool);
return subject.analyze({name: "test/Component.js"}, mockInfo);
});

0 comments on commit e9823f6

Please sign in to comment.