Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #30 by using a for loop instead of .forEach #31

Merged
merged 1 commit into from
Jun 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions lib/lbt/analyzer/ComponentAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,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);
});