From e4a60310227fc12ac5d5febea5d29124d575c779 Mon Sep 17 00:00:00 2001 From: Tommy Vinh Lam Date: Tue, 10 Sep 2019 16:39:45 +0200 Subject: [PATCH] [FEATURE][WIP] Add replacer for jQuery.sap.extend --- .../helpers/replacers/mergeOrObjectAssign.ts | 18 ++++----- src/tasks/helpers/replacers/test.js | 3 ++ test/replaceGlobals/assign.expected.js | 12 +++--- .../extendAndAssign.expected.js | 8 ++-- .../jquerySapExtend.config.json | 15 ++++++++ .../jquerySapExtend.expected.js | 37 +++++++++++++++++++ test/replaceGlobals/jquerySapExtend.js | 37 +++++++++++++++++++ .../versionReplacement.expected.js | 2 +- test/replaceGlobalsTest.ts | 30 +++++++++++++++ 9 files changed, 140 insertions(+), 22 deletions(-) create mode 100644 src/tasks/helpers/replacers/test.js create mode 100644 test/replaceGlobals/jquerySapExtend.config.json create mode 100644 test/replaceGlobals/jquerySapExtend.expected.js create mode 100644 test/replaceGlobals/jquerySapExtend.js diff --git a/src/tasks/helpers/replacers/mergeOrObjectAssign.ts b/src/tasks/helpers/replacers/mergeOrObjectAssign.ts index e0743406..1ac8e47d 100644 --- a/src/tasks/helpers/replacers/mergeOrObjectAssign.ts +++ b/src/tasks/helpers/replacers/mergeOrObjectAssign.ts @@ -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 @@ -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 + diff --git a/src/tasks/helpers/replacers/test.js b/src/tasks/helpers/replacers/test.js new file mode 100644 index 00000000..f9bfd510 --- /dev/null +++ b/src/tasks/helpers/replacers/test.js @@ -0,0 +1,3 @@ +sap.ui.require("", function() { + jQuery.sap.extend({}, {}); +}); \ No newline at end of file diff --git a/test/replaceGlobals/assign.expected.js b/test/replaceGlobals/assign.expected.js index dccfb78f..e32c4b56 100644 --- a/test/replaceGlobals/assign.expected.js +++ b/test/replaceGlobals/assign.expected.js @@ -3,8 +3,8 @@ */ // A module -sap.ui.define([], - function() { +sap.ui.define(["sap/base/util/merge"], + function(merge) { "use strict"; /** @@ -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(); } }; diff --git a/test/replaceGlobals/extendAndAssign.expected.js b/test/replaceGlobals/extendAndAssign.expected.js index 2f504089..c1b454b8 100644 --- a/test/replaceGlobals/extendAndAssign.expected.js +++ b/test/replaceGlobals/extendAndAssign.expected.js @@ -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(); } }; diff --git a/test/replaceGlobals/jquerySapExtend.config.json b/test/replaceGlobals/jquerySapExtend.config.json new file mode 100644 index 00000000..fc6e25df --- /dev/null +++ b/test/replaceGlobals/jquerySapExtend.config.json @@ -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" + } +} \ No newline at end of file diff --git a/test/replaceGlobals/jquerySapExtend.expected.js b/test/replaceGlobals/jquerySapExtend.expected.js new file mode 100644 index 00000000..5adff348 --- /dev/null +++ b/test/replaceGlobals/jquerySapExtend.expected.js @@ -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); \ No newline at end of file diff --git a/test/replaceGlobals/jquerySapExtend.js b/test/replaceGlobals/jquerySapExtend.js new file mode 100644 index 00000000..a79fbf48 --- /dev/null +++ b/test/replaceGlobals/jquerySapExtend.js @@ -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); diff --git a/test/replaceGlobals/versionReplacement.expected.js b/test/replaceGlobals/versionReplacement.expected.js index 5952745c..505057b6 100644 --- a/test/replaceGlobals/versionReplacement.expected.js +++ b/test/replaceGlobals/versionReplacement.expected.js @@ -19,7 +19,7 @@ sap.ui.define(["jquery.sap.script"], * @param sContent */ A.x = function () { - Object.assign({}, {}, {}); + jQuery.sap.extend(false, {}, {}, {}); jQuery.sap.uid(); }; diff --git a/test/replaceGlobalsTest.ts b/test/replaceGlobalsTest.ts index 9a210d56..9d047083 100644 --- a/test/replaceGlobalsTest.ts +++ b/test/replaceGlobalsTest.ts @@ -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"); @@ -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\"" ]); });