Skip to content

Commit

Permalink
[INTERNAL] Enable stricter parsing of analyseLibraryJS
Browse files Browse the repository at this point in the history
  • Loading branch information
flovogt committed Aug 4, 2022
1 parent 26064a9 commit cb7e451
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/lbt/analyzer/analyzeLibraryJS.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";
const {parseJS, Syntax, VisitorKeys} = require("../utils/parseUtils");
const {getPropertyKey, isMethodCall, isIdentifier, getStringArray} = require("../utils/ASTUtils");
const log = require("@ui5/logger").getLogger("lbt:analyzer:LibraryJS");

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

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

Expand Down
78 changes: 78 additions & 0 deletions test/lib/lbt/analyzer/analyzeLibraryJS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const test = require("ava");
const sinon = require("sinon");
const mock = require("mock-require");

function createMockResource(content, path) {
return {
async getBuffer() {
return content;
},
getPath() {
return path;
}
};
}

test("analyze: library.js with non supported property", async (t) => {
const libraryJS = `sap.ui.define([
'sap/ui/core/Core',
], function(Core) {
"use strict";
sap.ui.getCore().initLibrary({
name : "library.test",
version: "1.0.0",
customProperty1: "UI5",
dependencies : ["sap.ui.core"],
types: [
"library.test.ButtonType",
"library.test.DialogType",
],
interfaces: [
"library.test.IContent",
],
controls: [
"library.test.Button",
"library.test.CheckBox",
"library.test.Dialog",
"library.test.Input",
"library.test.Label",
"library.test.Link",
"library.test.Menu",
"library.test.Text"
],
elements: [
"library.test.MenuItem"
],
extensions: {
customExtension: "UI5"
},
customProperty2: "UI5"
});
return thisLib;
});`;

const librayJSPath = "library/test/library.js";
const logger = require("@ui5/logger");
const errorLogStub = sinon.stub();
const myLoggerInstance = {
error: errorLogStub
};
sinon.stub(logger, "getLogger").returns(myLoggerInstance);
const analyzeLibraryJSWithStubbedLogger = mock.reRequire("../../../../lib/lbt/analyzer/analyzeLibraryJS");

const mockResource = createMockResource(libraryJS, librayJSPath);

await analyzeLibraryJSWithStubbedLogger(mockResource);

t.is(errorLogStub.callCount, 2, "Error log is called twice");
t.is(errorLogStub.getCall(0).args[0],
"Unexpected property: 'customProperty1' in sap.ui.getCore().initLibrary call in 'library/test/library.js'",
"The error log message of the first call is correct");
t.is(errorLogStub.getCall(1).args[0],
"Unexpected property: 'customProperty2' in sap.ui.getCore().initLibrary call in 'library/test/library.js'",
"The error log message of the first call is correct");
});

0 comments on commit cb7e451

Please sign in to comment.