From 51907fa5f717ecf845158b1d474e72bfa1c4bdc0 Mon Sep 17 00:00:00 2001 From: Mathieu Boretti Date: Fri, 2 Aug 2024 17:15:40 +0200 Subject: [PATCH] feat(instrumenter): add more expression mutator for Date (#4917) --- .../src/mutators/method-expression-mutator.ts | 20 +++++++++++--- .../method-expression-mutator.spec.ts | 26 ++++++++++++++----- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/packages/instrumenter/src/mutators/method-expression-mutator.ts b/packages/instrumenter/src/mutators/method-expression-mutator.ts index 21f53734cf..c58bdd034c 100644 --- a/packages/instrumenter/src/mutators/method-expression-mutator.ts +++ b/packages/instrumenter/src/mutators/method-expression-mutator.ts @@ -21,14 +21,28 @@ const replacements = new Map([ ['trim', null], ['trimEnd', 'trimStart'], ['min', 'max'], - ['getDate', 'getMonth'], - ['setDate', 'setMonth'], + ['getDate', 'getTime'], + ['setDate', 'setTime'], + ['getFullYear', 'getMonth'], + ['setFullYear', 'setMonth'], ['getHours', 'getMinutes'], ['setHours', 'setMinutes'], + ['getSeconds', 'getMilliseconds'], + ['setSeconds', 'setMilliseconds'], + ['getUTCDate', 'getTime'], + ['setUTCDate', 'setTime'], + ['getUTCFullYear', 'getUTCMonth'], + ['setUTCFullYear', 'setUTCMonth'], + ['getUTCHours', 'getUTCMinutes'], + ['setUTCHours', 'setUTCMinutes'], + ['getUTCSeconds', 'getUTCMilliseconds'], + ['setUTCSeconds', 'setUTCMilliseconds'], ]); +const noReverseRemplacements = ['getUTCDate', 'setUTCDate']; + for (const [key, value] of Array.from(replacements)) { - if (value) { + if (value && !noReverseRemplacements.includes(key)) { replacements.set(value, key); } } diff --git a/packages/instrumenter/test/unit/mutators/method-expression-mutator.spec.ts b/packages/instrumenter/test/unit/mutators/method-expression-mutator.spec.ts index 69db349ba1..13be2c28aa 100644 --- a/packages/instrumenter/test/unit/mutators/method-expression-mutator.spec.ts +++ b/packages/instrumenter/test/unit/mutators/method-expression-mutator.spec.ts @@ -116,25 +116,39 @@ describe(sut.name, () => { }); } - for (const [key, value] of [ + for (const [key, value, noReverse] of [ ['endsWith', 'startsWith'], ['every', 'some'], ['toLocaleLowerCase', 'toLocaleUpperCase'], ['toLowerCase', 'toUpperCase'], ['trimEnd', 'trimStart'], ['min', 'max'], - ['getDate', 'getMonth'], - ['setDate', 'setMonth'], + ['getDate', 'getTime'], + ['setDate', 'setTime'], + ['getFullYear', 'getMonth'], + ['setFullYear', 'setMonth'], ['getHours', 'getMinutes'], ['setHours', 'setMinutes'], + ['getSeconds', 'getMilliseconds'], + ['setSeconds', 'setMilliseconds'], + ['getUTCDate', 'getTime', true], + ['setUTCDate', 'setTime', true], + ['getUTCFullYear', 'getUTCMonth'], + ['setUTCFullYear', 'setUTCMonth'], + ['getUTCHours', 'getUTCMinutes'], + ['setUTCHours', 'setUTCMinutes'], + ['getUTCSeconds', 'getUTCMilliseconds'], + ['setUTCSeconds', 'setUTCMilliseconds'], ]) { it(`should replace ${key} with ${value}`, () => { expectJSMutation(sut, `text.${key}();`, `text.${value}();`); }); - it(`should replace ${value} with ${key}`, () => { - expectJSMutation(sut, `text.${value}();`, `text.${key}();`); - }); + if (!noReverse) { + it(`should replace ${value} with ${key}`, () => { + expectJSMutation(sut, `text.${value}();`, `text.${key}();`); + }); + } } for (const method of ['charAt', 'filter', 'reverse', 'slice', 'sort', 'substr', 'substring', 'trim']) {