Skip to content
This repository was archived by the owner on Sep 12, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions src/tasks/helpers/replacers/mergeOrObjectAssign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {ASTReplaceable, ASTReplaceableResult, NodePath} from "ui5-migration";
const builders = recast.types.builders;

/**
* Replaces jQuery.sap.extend with either Object.assign (shallow) or
* sap/base/util/merge (deep)
* Replaces jQuery.sap.extend with sap/base/util/merge (deep)
*
* @param {recast.NodePath} node The top node of the module reference
* @param {string} name The name of the new module
Expand All @@ -24,32 +23,29 @@ const replaceable: ASTReplaceable = {
void {
const oInsertion = node.parentPath.value;


// CallExpression
if (oInsertion.type === Syntax.CallExpression) {
const oInsertionPoint = node.parentPath.parentPath.value;

const arg0 = oInsertion.arguments[0];
let bDeepCopy = false;
let bDeepCopy = true;
if (arg0.type === Syntax.Literal &&
typeof arg0.value === "boolean") {
oInsertion.arguments.shift();
bDeepCopy = arg0.value;
}

if (bDeepCopy) {
if (arg0.type === Syntax.Literal &&
typeof arg0.value === "boolean") {
oInsertion.arguments.shift();
}
oInsertionPoint[node.parentPath.name] = builders.callExpression(
builders.identifier(name || config.newVariableName),
oInsertion.arguments);
} else {
oInsertionPoint[node.parentPath.name] = builders.callExpression(
builders.memberExpression(
builders.identifier("Object"),
builders.identifier("assign")),
oInsertion.arguments);
// TODO: Logging jQuery.sap.extend (shallow) is not replaced
return { modified : false, addDependency : false };
}

} else {
throw new Error(
"insertion is of type " + oInsertion.type +
Expand Down
3 changes: 3 additions & 0 deletions src/tasks/helpers/replacers/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sap.ui.require("", function() {
jQuery.sap.extend({}, {});
});
12 changes: 6 additions & 6 deletions test/replaceGlobals/assign.expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*/

// A module
sap.ui.define([],
function() {
sap.ui.define(["sap/base/util/merge"],
function(merge) {
"use strict";

/**
Expand All @@ -23,11 +23,11 @@ sap.ui.define([],
if (oParam.control(0)) {
var sKey = "Test." + iconName + oParam.control;
if (iconInfo.resourceBundle.hasText(sKey)) {
$(sKey).control(Object.assign({}, sKey, sContent));
$(sKey).control(jQuery.sap.extend(false, {}, sKey, sContent));
}
var x$ = Object.assign(sKey, sContent);
x$ += Object.assign(sKey, sContent);
x$ += Object.assign({}, sContent);
var x$ = jQuery.sap.extend(false, sKey, sContent);
x$ += merge(sKey, sContent);
x$ += merge({}, sContent);
x$.control();
}
};
Expand Down
8 changes: 4 additions & 4 deletions test/replaceGlobals/extendAndAssign.expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ sap.ui.define(["sap/base/util/merge"],
if (oParam.control(1)) {
var sKey = "Test." + iconName + oParam.control;
if (iconInfo.resourceBundle.hasText(sKey)) {
$(sKey).control(Object.assign({}, sKey, sContent));
$(sKey).control(jQuery.sap.extend(false, {}, sKey, sContent));
}
var x$ = Object.assign(sKey, sContent);
x$ += Object.assign(sKey, sContent);
x$ += Object.assign({}, sContent);
var x$ = jQuery.sap.extend(false, sKey, sContent);
x$ += merge(sKey, sContent);
x$ += merge({}, sContent);
x$.control();
}
};
Expand Down
15 changes: 15 additions & 0 deletions test/replaceGlobals/jquerySapExtend.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"modules": {
"sap/ui/thirdparty/jquery": {
"jQuery.sap.extend": {
"newModulePath": "sap/base/util/merge",
"newVariableName": "merge",
"replacer": "mergeOrObjectAssign",
"version": "^1.58.0"
}
}
},
"replacers": {
"mergeOrObjectAssign": "tasks/helpers/replacers/mergeOrObjectAssign.js"
}
}
37 changes: 37 additions & 0 deletions test/replaceGlobals/jquerySapExtend.expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* !
* ${copyright}
*/

// A module
sap.ui.define(["sap/ui/thirdparty/jquery", "sap/base/util/merge"],
function(jQuery, merge) {
"use strict";

/**
*
* @type {{}}
*/
var A = {};

/**
*
* @param oParam
* @param sContent
*/
A.x = function (oParam, sContent) {
merge({}, oParam);
merge({}, {});
merge({ foo: "foo" }, { bar: "bar" }, { baz: "baz" });
merge({ foo: "foo" }, oParam);
merge({}, { bar: "bar" }, { baz: "baz" });
merge({}, { bar: "bar" }, { baz: undefined });
jQuery.sap.extend(false, {}, oParam);
jQuery.sap.extend(false, {}, {});
jQuery.sap.extend(false, { foo: "foo" }, { bar: "bar" }, { baz: "baz" });
jQuery.sap.extend(false, { foo: "foo" }, oParam);
jQuery.sap.extend(false, {}, { bar: "bar" }, { baz: "baz" });
jQuery.sap.extend(false, {}, { bar: "bar" }, { baz: undefined });
};

return A;
}, /* bExport= */ true);
37 changes: 37 additions & 0 deletions test/replaceGlobals/jquerySapExtend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* !
* ${copyright}
*/

// A module
sap.ui.define([],
function() {
"use strict";

/**
*
* @type {{}}
*/
var A = {};

/**
*
* @param oParam
* @param sContent
*/
A.x = function (oParam, sContent) {
jQuery.sap.extend({}, oParam);
jQuery.sap.extend(true, {}, {});
jQuery.sap.extend({ foo: "foo" }, { bar: "bar" }, { baz: "baz" });
jQuery.sap.extend({ foo: "foo" }, oParam);
jQuery.sap.extend({}, { bar: "bar" }, { baz: "baz" });
jQuery.sap.extend({}, { bar: "bar" }, { baz: undefined });
jQuery.sap.extend(false, {}, oParam);
jQuery.sap.extend(false, {}, {});
jQuery.sap.extend(false, { foo: "foo" }, { bar: "bar" }, { baz: "baz" });
jQuery.sap.extend(false, { foo: "foo" }, oParam);
jQuery.sap.extend(false, {}, { bar: "bar" }, { baz: "baz" });
jQuery.sap.extend(false, {}, { bar: "bar" }, { baz: undefined });
};

return A;
}, /* bExport= */ true);
2 changes: 1 addition & 1 deletion test/replaceGlobals/versionReplacement.expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ sap.ui.define(["jquery.sap.script"],
* @param sContent
*/
A.x = function () {
Object.assign({}, {}, {});
jQuery.sap.extend(false, {}, {}, {});
jQuery.sap.uid();
};

Expand Down
30 changes: 30 additions & 0 deletions test/replaceGlobalsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1880,6 +1880,35 @@ describe("replaceGlobals", function() {
} ]);
});

it("should replace jQuery.sap.extend", function(done) {
const expectedContent = fs.readFileSync(
rootDir + "jquerySapExtend.expected.js", "utf8");
const config = JSON.parse(
fs.readFileSync(rootDir + "jquerySapExtend.config.json"));
const module =
new CustomFileInfo(rootDir + "jquerySapExtend.js");
analyseMigrateAndTest(module, true, expectedContent, config, done, [
"trace: 22: Replace global call with \"sap.ui.thirdparty.jquery\"",
"trace: 22: Found call to replace \"jQuery.extend\"",
"trace: 23: Replace global call with \"sap.ui.thirdparty.jquery\"",
"trace: 23: Found call to replace \"jQuery.extend\"",
"trace: 24: Replace global call with \"sap.ui.thirdparty.jquery\"",
"trace: 24: Found call to replace \"jQuery.extend\"",
"trace: 25: Replace global call with \"sap.ui.thirdparty.jquery\"",
"trace: 25: Found call to replace \"jQuery.extend\"",
"trace: 26: Replace global call with \"sap.ui.thirdparty.jquery\"",
"trace: 26: Found call to replace \"jQuery.extend\"",
"trace: 27: Replace global call with \"sap.ui.thirdparty.jquery\"",
"trace: 27: Found call to replace \"jQuery.extend\"",
"trace: 22: Replaced call \"jQuery.extend\"",
"trace: 23: Replaced call \"jQuery.extend\"",
"trace: 24: Replaced call \"jQuery.extend\"",
"trace: 25: Replaced call \"jQuery.extend\"",
"trace: 26: Replaced call \"jQuery.extend\"",
"trace: 27: Replaced call \"jQuery.extend\"",
]);
});

it("should replace and add dependency for extend", function(done) {
const expectedContent =
fs.readFileSync(rootDir + "extend.expected.js", "utf8");
Expand Down Expand Up @@ -1916,6 +1945,7 @@ describe("replaceGlobals", function() {
"trace: 28: Replaced call \"jQuery.sap.extend\"",
"trace: 29: Replaced call \"jQuery.sap.extend\"",
"trace: 30: Replaced call \"jQuery.sap.extend\"",
"trace: 7: Add dependency \"sap/base/util/merge\" named \"merge\""
]);
});

Expand Down