diff --git a/lib/lbt/analyzer/analyzeLibraryJS.js b/lib/lbt/analyzer/analyzeLibraryJS.js index cfca2948f..2e739c7f6 100644 --- a/lib/lbt/analyzer/analyzeLibraryJS.js +++ b/lib/lbt/analyzer/analyzeLibraryJS.js @@ -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"]; @@ -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()}'` + ); } }); diff --git a/test/lib/lbt/analyzer/analyzeLibraryJS.js b/test/lib/lbt/analyzer/analyzeLibraryJS.js new file mode 100644 index 000000000..6d3d5184e --- /dev/null +++ b/test/lib/lbt/analyzer/analyzeLibraryJS.js @@ -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"); +});