Skip to content

Commit e9823f6

Browse files
themaschcodeworrior
authored andcommitted
[FIX] iterate over routes using a for loop if it is an object (#31)
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
1 parent 97559c9 commit e9823f6

File tree

2 files changed

+116
-11
lines changed

2 files changed

+116
-11
lines changed

lib/lbt/analyzer/ComponentAnalyzer.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,22 +117,41 @@ class ComponentAnalyzer {
117117

118118
let routing = ui5.routing;
119119
if ( routing ) {
120-
// console.log("routing: ", routing);
121-
routing.routes.forEach( (route) => {
122-
let target = routing.targets[route.target];
123-
if ( target && target.viewName ) {
124-
let module = ModuleName.fromUI5LegacyName(
125-
(routing.config.viewPath ? routing.config.viewPath + "." : "") +
126-
target.viewName, ".view." + routing.config.viewType.toLowerCase() );
127-
log.verbose("converting route to view dependency ", module);
128-
// TODO make this a conditional dependency, depending on the pattern?
129-
info.addDependency(module);
120+
if (Array.isArray(routing.routes)) {
121+
routing.routes.forEach((route) => this._visitRoute(route, routing, info));
122+
} else {
123+
for (let key in routing.routes) {
124+
if (!routing.routes.hasOwnProperty(key)) {
125+
continue;
126+
}
127+
const route = routing.routes[key];
128+
this._visitRoute(route, routing, info);
130129
}
131-
});
130+
}
132131
}
133132

134133
return info;
135134
}
135+
136+
/**
137+
* called for any route, this adds the view used by a route as a dependency.
138+
*
139+
* @param {object} route the single route
140+
* @param {object} routing the full routing object from the manifest
141+
* @param {ModuleInfo} info ModuleInfo object that should be enriched
142+
* @private
143+
*/
144+
_visitRoute( route, routing, info ) {
145+
const viewPath = routing.config.viewPath ? routing.config.viewPath + "." : "";
146+
const viewType = routing.config.viewType.toLowerCase();
147+
const target = routing.targets[route.target];
148+
if ( target && target.viewName ) {
149+
const module = ModuleName.fromUI5LegacyName(viewPath + target.viewName, ".view." + viewType);
150+
log.verbose("converting route to view dependency ", module);
151+
// TODO make this a conditional dependency, depending on the pattern?
152+
info.addDependency(module);
153+
}
154+
}
136155
}
137156

138157
module.exports = ComponentAnalyzer;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const {test} = require("ava");
2+
const Path = require("path");
3+
const ComponentAnalyzer = require("../../../../lib/lbt/analyzer/ComponentAnalyzer");
4+
5+
6+
function createMockPool(path, manifest) {
7+
const expectedPath = Path.join(path, "manifest.json");
8+
return {
9+
async findResource(name) {
10+
if (name !== expectedPath) {
11+
throw new Error(`unexpected resource name: ${name}, expected ${expectedPath}`);
12+
}
13+
return {
14+
async buffer() {
15+
return JSON.stringify(manifest);
16+
}
17+
};
18+
}
19+
};
20+
}
21+
22+
test("routing with routes as array", (t) => {
23+
const mockManifest = {
24+
"sap.ui5": {
25+
routing: {
26+
config: {
27+
viewPath: "test.view",
28+
viewType: "XML"
29+
},
30+
routes: [
31+
{
32+
name: "test",
33+
target: "test"
34+
}
35+
],
36+
targets: {
37+
test: {viewName: "App"}
38+
}
39+
}
40+
}
41+
};
42+
43+
const mockPool = createMockPool("test/", mockManifest);
44+
45+
const mockInfo = {
46+
addDependency(name) {
47+
t.is(name, "test/view/App.view.xml");
48+
}
49+
};
50+
51+
const subject = new ComponentAnalyzer(mockPool);
52+
return subject.analyze({name: "test/Component.js"}, mockInfo);
53+
});
54+
55+
56+
test("routing with routes as object", (t) => {
57+
const mockManifest = {
58+
"sap.ui5": {
59+
routing: {
60+
config: {
61+
viewPath: "test.view",
62+
viewType: "XML"
63+
},
64+
routes: {
65+
test: {
66+
target: "test"
67+
}
68+
},
69+
targets: {
70+
test: {viewName: "App"}
71+
}
72+
}
73+
}
74+
};
75+
76+
const mockPool = createMockPool("test/", mockManifest);
77+
78+
const mockInfo = {
79+
addDependency(name) {
80+
t.is(name, "test/view/App.view.xml");
81+
}
82+
};
83+
84+
const subject = new ComponentAnalyzer(mockPool);
85+
return subject.analyze({name: "test/Component.js"}, mockInfo);
86+
});

0 commit comments

Comments
 (0)