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 3, 2022
1 parent 26064a9 commit 112207c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
3 changes: 2 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 @@ -39,7 +40,7 @@ async function analyze(resource) {
} else if ( key === "elements" && value.type == Syntax.ArrayExpression ) {
libInfo.elements = getStringArray(value, true);
} else {
// TODO: Maybe log an error/warning when unexpected properties are defined?
log.error(`Unexpected property: '${key}' in sap.ui.getCore().initLibrary call`);
}
});

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

function createMockResource(libraryJS) {
return {
async getBuffer() {
return libraryJS;
}
};
}

test("analyze: library.js with non supported property", (t) => {
const libraryjs = `sap.ui.define([
'sap/ui/core/Core',
], function(Core) {
"use strict";
sap.ui.getCore().initLibrary({
name : "library.test",
customProperty: "UI5"
});
return thisLib;
});`;

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);

analyzeLibraryJSWithStubbedLogger(mockResource);

t.is(errorLogStub.callCount, 1, "Error log is called once");
});

0 comments on commit 112207c

Please sign in to comment.