Skip to content

Commit

Permalink
feat(instrumenter): add expression mutator for Date (#4917) (#4918)
Browse files Browse the repository at this point in the history
* feat(instrumenter): add expression mutator for Date (#4917)

* feat(instrumenter): add more expression mutator for Date (#4917)

* feat(instrumenter): add expression mutator for Date (#4917)

Remove the get mutator according exchange in the issue #4917
  • Loading branch information
boretti authored Aug 14, 2024
1 parent 69d8434 commit 8207ef6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
12 changes: 11 additions & 1 deletion packages/instrumenter/src/mutators/method-expression-mutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,20 @@ const replacements = new Map([
['trim', null],
['trimEnd', 'trimStart'],
['min', 'max'],
['setDate', 'setTime'],
['setFullYear', 'setMonth'],
['setHours', 'setMinutes'],
['setSeconds', 'setMilliseconds'],
['setUTCDate', 'setTime'],
['setUTCFullYear', 'setUTCMonth'],
['setUTCHours', 'setUTCMinutes'],
['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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,31 @@ 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'],
['setDate', 'setTime'],
['setFullYear', 'setMonth'],
['setHours', 'setMinutes'],
['setSeconds', 'setMilliseconds'],
['setUTCDate', 'setTime', true],
['setUTCFullYear', 'setUTCMonth'],
['setUTCHours', 'setUTCMinutes'],
['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']) {
Expand Down

0 comments on commit 8207ef6

Please sign in to comment.