Skip to content

Commit cb7e451

Browse files
committed
[INTERNAL] Enable stricter parsing of analyseLibraryJS
1 parent 26064a9 commit cb7e451

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

lib/lbt/analyzer/analyzeLibraryJS.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22
const {parseJS, Syntax, VisitorKeys} = require("../utils/parseUtils");
33
const {getPropertyKey, isMethodCall, isIdentifier, getStringArray} = require("../utils/ASTUtils");
4+
const log = require("@ui5/logger").getLogger("lbt:analyzer:LibraryJS");
45

56
const CALL__SAP_UI_GETCORE = ["sap", "ui", "getCore"];
67

@@ -38,8 +39,12 @@ async function analyze(resource) {
3839
libInfo.controls = getStringArray(value, true);
3940
} else if ( key === "elements" && value.type == Syntax.ArrayExpression ) {
4041
libInfo.elements = getStringArray(value, true);
42+
} else if ( key === "dependencies" || key === "extensions" || key === "name" || key === "version" ) {
43+
// do nothing, for all other supported properties
4144
} else {
42-
// TODO: Maybe log an error/warning when unexpected properties are defined?
45+
log.error(
46+
`Unexpected property: '${key}' in sap.ui.getCore().initLibrary call in '${resource.getPath()}'`
47+
);
4348
}
4449
});
4550

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
const test = require("ava");
2+
const sinon = require("sinon");
3+
const mock = require("mock-require");
4+
5+
function createMockResource(content, path) {
6+
return {
7+
async getBuffer() {
8+
return content;
9+
},
10+
getPath() {
11+
return path;
12+
}
13+
};
14+
}
15+
16+
test("analyze: library.js with non supported property", async (t) => {
17+
const libraryJS = `sap.ui.define([
18+
'sap/ui/core/Core',
19+
], function(Core) {
20+
21+
"use strict";
22+
23+
sap.ui.getCore().initLibrary({
24+
name : "library.test",
25+
version: "1.0.0",
26+
customProperty1: "UI5",
27+
dependencies : ["sap.ui.core"],
28+
types: [
29+
"library.test.ButtonType",
30+
"library.test.DialogType",
31+
],
32+
interfaces: [
33+
"library.test.IContent",
34+
],
35+
controls: [
36+
"library.test.Button",
37+
"library.test.CheckBox",
38+
"library.test.Dialog",
39+
"library.test.Input",
40+
"library.test.Label",
41+
"library.test.Link",
42+
"library.test.Menu",
43+
"library.test.Text"
44+
],
45+
elements: [
46+
"library.test.MenuItem"
47+
],
48+
extensions: {
49+
customExtension: "UI5"
50+
},
51+
customProperty2: "UI5"
52+
});
53+
54+
return thisLib;
55+
56+
});`;
57+
58+
const librayJSPath = "library/test/library.js";
59+
const logger = require("@ui5/logger");
60+
const errorLogStub = sinon.stub();
61+
const myLoggerInstance = {
62+
error: errorLogStub
63+
};
64+
sinon.stub(logger, "getLogger").returns(myLoggerInstance);
65+
const analyzeLibraryJSWithStubbedLogger = mock.reRequire("../../../../lib/lbt/analyzer/analyzeLibraryJS");
66+
67+
const mockResource = createMockResource(libraryJS, librayJSPath);
68+
69+
await analyzeLibraryJSWithStubbedLogger(mockResource);
70+
71+
t.is(errorLogStub.callCount, 2, "Error log is called twice");
72+
t.is(errorLogStub.getCall(0).args[0],
73+
"Unexpected property: 'customProperty1' in sap.ui.getCore().initLibrary call in 'library/test/library.js'",
74+
"The error log message of the first call is correct");
75+
t.is(errorLogStub.getCall(1).args[0],
76+
"Unexpected property: 'customProperty2' in sap.ui.getCore().initLibrary call in 'library/test/library.js'",
77+
"The error log message of the first call is correct");
78+
});

0 commit comments

Comments
 (0)