-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mutators): Add method expression mutator (#3508)
Add the method expression mutator. It mutates methods call expressions: - Change `.endsWith()` to `.startsWith()` and vice versa - Change `.trimEnd()` to `.trimStart()` and vice versa - Change `.toLowerCase()` to .` toUpperCase()` and vice versa - Change `.toLocalLowerCase()` to `.toLocalUpperCase() ` and vice versa - Change `.some()` to `.every() ` and vice versa - Remove `.trim()` - Remove `.substr()` - Remove `.substring()` - Remove `.sort()` - Remove `.reverse()` - Remove `.filter()` - Remove `.slice()` - Remove `.charAt()` If you don't like this, you can disable this mutator using: ```json { "mutator": { "excludedMutations": ["MethodExpression"] } } ``` Co-authored-by: Nico Jansen <jansennico@gmail.com>
- Loading branch information
1 parent
907a5d0
commit 70a4e4f
Showing
10 changed files
with
331 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
packages/instrumenter/src/mutators/method-expression-mutator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import babel from '@babel/core'; | ||
|
||
import { deepCloneNode } from '../util/syntax-helpers.js'; | ||
|
||
import { NodeMutator } from './node-mutator.js'; | ||
|
||
const { types } = babel; | ||
|
||
const replacements = new Map([ | ||
['charAt', null], | ||
['endsWith', 'startsWith'], | ||
['every', 'some'], | ||
['filter', null], | ||
['reverse', null], | ||
['slice', null], | ||
['sort', null], | ||
['substr', null], | ||
['substring', null], | ||
['toLocaleLowerCase', 'toLocaleUpperCase'], | ||
['toLowerCase', 'toUpperCase'], | ||
['trim', null], | ||
['trimEnd', 'trimStart'], | ||
]); | ||
|
||
for (const [key, value] of Array.from(replacements)) { | ||
if (value) { | ||
replacements.set(value, key); | ||
} | ||
} | ||
|
||
export const methodExpressionMutator: NodeMutator = { | ||
name: 'MethodExpression', | ||
|
||
*mutate(path) { | ||
if (!(path.isCallExpression() || path.isOptionalCallExpression())) { | ||
return; | ||
} | ||
|
||
const { callee } = path.node; | ||
if (!(types.isMemberExpression(callee) || types.isOptionalMemberExpression(callee)) || !types.isIdentifier(callee.property)) { | ||
return; | ||
} | ||
|
||
const newName = replacements.get(callee.property.name); | ||
if (newName === undefined) { | ||
return; | ||
} | ||
|
||
if (newName === null) { | ||
// Remove the method expression. I.e. `foo.trim()` => `foo` | ||
yield deepCloneNode(callee.object); | ||
return; | ||
} | ||
|
||
// Replace the method expression. I.e. `foo.toLowerCase()` => `foo.toUpperCase` | ||
const nodeArguments = path.node.arguments.map((argumentNode) => deepCloneNode(argumentNode)); | ||
|
||
const mutatedCallee = types.isMemberExpression(callee) | ||
? types.memberExpression(deepCloneNode(callee.object), types.identifier(newName), false, callee.optional) | ||
: types.optionalMemberExpression(deepCloneNode(callee.object), types.identifier(newName), false, callee.optional); | ||
|
||
yield types.isCallExpression(path.node) | ||
? types.callExpression(mutatedCallee, nodeArguments) | ||
: types.optionalCallExpression(mutatedCallee, nodeArguments, path.node.optional); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
packages/instrumenter/test/unit/mutators/method-expression-mutator.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { methodExpressionMutator as sut } from '../../../src/mutators/method-expression-mutator.js'; | ||
import { expectJSMutation } from '../../helpers/expect-mutation.js'; | ||
|
||
describe(sut.name, () => { | ||
it('should have name "MethodExpression"', () => { | ||
expect(sut.name).eq('MethodExpression'); | ||
}); | ||
|
||
describe('functions', () => { | ||
it('should ignore a non-method function', () => { | ||
expectJSMutation(sut, 'function endsWith() {} endsWith();'); | ||
}); | ||
}); | ||
|
||
describe('methods', () => { | ||
for (const [input, output, description] of [ | ||
['text.trim();', 'text;', 'removed, non-optional'], | ||
['text?.trim();', 'text;', 'removed, optional member'], | ||
['text.trim?.();', 'text;', 'removed, optional call'], | ||
['text?.trim?.();', 'text;', 'removed, optional member, optional call'], | ||
['parent.text?.trim();', 'parent.text;', 'removed, optional member in a non-optional parent'], | ||
['parent.text.trim?.();', 'parent.text;', 'removed, optional call in a non-optional parent'], | ||
['parent.text?.trim?.();', 'parent.text;', 'removed, optional member, optional call in a non-optional parent'], | ||
['parent?.text?.trim();', 'parent?.text;', 'removed, optional member in an optional parent'], | ||
['parent?.text.trim?.();', 'parent?.text;', 'removed, optional call in an optional parent'], | ||
['parent?.text?.trim?.();', 'parent?.text;', 'removed, optional member, optional call in an optional parent'], | ||
['text.startsWith();', 'text.endsWith();', 'replaced, non-optional'], | ||
['text?.startsWith();', 'text?.endsWith();', 'replaced, optional member'], | ||
['text.startsWith?.();', 'text.endsWith?.();', 'replaced, optional call'], | ||
['text?.startsWith?.();', 'text?.endsWith?.();', 'replaced, optional member, optional call'], | ||
['text.startsWith("foo").valueOf();', 'text.endsWith("foo").valueOf();', 'replaced in the middle of a chain'], | ||
['parent.text?.startsWith();', 'parent.text?.endsWith();', 'replaced, optional member in a non-optional parent'], | ||
['parent.text.startsWith?.();', 'parent.text.endsWith?.();', 'replaced, optional call in a non-optional parent'], | ||
['parent.text?.startsWith?.();', 'parent.text?.endsWith?.();', 'replaced, optional member, optional call in a non-optional parent'], | ||
['parent?.text?.startsWith();', 'parent?.text?.endsWith();', 'replaced, optional member in an optional parent'], | ||
['parent?.text.startsWith?.();', 'parent?.text.endsWith?.();', 'replaced, optional call in an optional parent'], | ||
['parent?.text?.startsWith?.();', 'parent?.text?.endsWith?.();', 'replaced, optional member, optional call in an optional parent'], | ||
['text.trim(abc);', 'text;', 'removed, non-optional with an argument'], | ||
['text?.trim(abc);', 'text;', 'removed, optional member with an argument'], | ||
['text.trim?.(abc);', 'text;', 'removed, optional call with an argument'], | ||
['text?.trim?.(abc);', 'text;', 'removed, optional member, optional call with an argument'], | ||
['parent.text?.trim(abc);', 'parent.text;', 'removed, optional member in a non-optional parent with an argument'], | ||
['parent.text.trim?.(abc);', 'parent.text;', 'removed, optional call in a non-optional parent with an argument'], | ||
['parent.text?.trim?.(abc);', 'parent.text;', 'removed, optional member, optional call in a non-optional parent with an argument'], | ||
['parent?.text?.trim(abc);', 'parent?.text;', 'removed, optional member in an optional parent with an argument'], | ||
['parent?.text.trim?.(abc);', 'parent?.text;', 'removed, optional call in an optional parent with an argument'], | ||
['parent?.text?.trim?.(abc);', 'parent?.text;', 'removed, optional member, optional call in an optional parent with an argument'], | ||
['text.startsWith(abc);', 'text.endsWith(abc);', 'replaced, non-optional with an argument'], | ||
['text?.startsWith(abc);', 'text?.endsWith(abc);', 'replaced, optional member with an argument'], | ||
['text.startsWith?.(abc);', 'text.endsWith?.(abc);', 'replaced, optional call with an argument'], | ||
['text?.startsWith?.(abc);', 'text?.endsWith?.(abc);', 'replaced, optional member, optional call with an argument'], | ||
['parent.text?.startsWith(abc);', 'parent.text?.endsWith(abc);', 'replaced, optional member in a non-optional parent with an argument'], | ||
['parent.text.startsWith?.(abc);', 'parent.text.endsWith?.(abc);', 'replaced, optional call in a non-optional parent with an argument'], | ||
[ | ||
'parent.text?.startsWith?.(abc);', | ||
'parent.text?.endsWith?.(abc);', | ||
'replaced, optional member, optional call in a non-optional parent with an argument', | ||
], | ||
['parent?.text?.startsWith(abc);', 'parent?.text?.endsWith(abc);', 'replaced, optional member in an optional parent with an argument'], | ||
['parent?.text.startsWith?.(abc);', 'parent?.text.endsWith?.(abc);', 'replaced, optional call in an optional parent with an argument'], | ||
[ | ||
'parent?.text?.startsWith?.(abc);', | ||
'parent?.text?.endsWith?.(abc);', | ||
'replaced, optional member, optional call in an optional parent with an argument', | ||
], | ||
['text.trim(abc, def);', 'text;', 'removed, non-optional with multiple arguments'], | ||
['text?.trim(abc, def);', 'text;', 'removed, optional member with multiple arguments'], | ||
['text.trim?.(abc, def);', 'text;', 'removed, optional call with multiple arguments'], | ||
['text?.trim?.(abc, def);', 'text;', 'removed, optional member, optional call with multiple arguments'], | ||
['text.trim().length;', 'text.length;', 'removed in the middle of a chain'], | ||
['parent.text?.trim(abc, def);', 'parent.text;', 'removed, optional member in a non-optional parent with multiple arguments'], | ||
['parent.text.trim?.(abc, def);', 'parent.text;', 'removed, optional call in a non-optional parent with multiple arguments'], | ||
['parent.text?.trim?.(abc, def);', 'parent.text;', 'removed, optional member, optional call in a non-optional parent with multiple arguments'], | ||
['parent?.text?.trim(abc, def);', 'parent?.text;', 'removed, optional member in an optional parent with multiple arguments'], | ||
['parent?.text.trim?.(abc, def);', 'parent?.text;', 'removed, optional call in an optional parent with multiple arguments'], | ||
['parent?.text?.trim?.(abc, def);', 'parent?.text;', 'removed, optional member, optional call in an optional parent with multiple arguments'], | ||
['text.startsWith(abc, def);', 'text.endsWith(abc, def);', 'replaced, non-optional with multiple arguments'], | ||
['text?.startsWith(abc, def);', 'text?.endsWith(abc, def);', 'replaced, optional member with multiple arguments'], | ||
['text.startsWith?.(abc, def);', 'text.endsWith?.(abc, def);', 'replaced, optional call with multiple arguments'], | ||
['text?.startsWith?.(abc, def);', 'text?.endsWith?.(abc, def);', 'replaced, optional member, optional call with multiple arguments'], | ||
[ | ||
'parent.text?.startsWith(abc, def);', | ||
'parent.text?.endsWith(abc, def);', | ||
'replaced, optional member in a non-optional parent with multiple arguments', | ||
], | ||
[ | ||
'parent.text.startsWith?.(abc, def);', | ||
'parent.text.endsWith?.(abc, def);', | ||
'replaced, optional call in a non-optional parent with multiple arguments', | ||
], | ||
[ | ||
'parent.text?.startsWith?.(abc, def);', | ||
'parent.text?.endsWith?.(abc, def);', | ||
'replaced, optional member, optional call in a non-optional parent with multiple arguments', | ||
], | ||
[ | ||
'parent?.text?.startsWith(abc, def);', | ||
'parent?.text?.endsWith(abc, def);', | ||
'replaced, optional member in an optional parent with multiple arguments', | ||
], | ||
[ | ||
'parent?.text.startsWith?.(abc, def);', | ||
'parent?.text.endsWith?.(abc, def);', | ||
'replaced, optional call in an optional parent with multiple arguments', | ||
], | ||
[ | ||
'parent?.text?.startsWith?.(abc, def);', | ||
'parent?.text?.endsWith?.(abc, def);', | ||
'replaced, optional member, optional call in an optional parent with multiple arguments', | ||
], | ||
]) { | ||
it(`should be ${description}`, () => { | ||
expectJSMutation(sut, input, output); | ||
}); | ||
} | ||
|
||
for (const [key, value] of [ | ||
['endsWith', 'startsWith'], | ||
['every', 'some'], | ||
['toLocaleLowerCase', 'toLocaleUpperCase'], | ||
['toLowerCase', 'toUpperCase'], | ||
['trimEnd', 'trimStart'], | ||
]) { | ||
it(`should replace ${key} with ${value}`, () => { | ||
expectJSMutation(sut, `text.${key}();`, `text.${value}();`); | ||
}); | ||
|
||
it(`should replace ${value} with ${key}`, () => { | ||
expectJSMutation(sut, `text.${value}();`, `text.${key}();`); | ||
}); | ||
} | ||
|
||
for (const method of ['charAt', 'filter', 'reverse', 'slice', 'sort', 'substr', 'substring', 'trim']) { | ||
it(`should remove ${method}`, () => { | ||
expectJSMutation(sut, `text.${method}();`, 'text;'); | ||
}); | ||
} | ||
|
||
it('should ignore computed properties', () => { | ||
expectJSMutation(sut, "text['trim']();"); | ||
}); | ||
|
||
it('should ignore new expressions', () => { | ||
expectJSMutation(sut, 'new text.trim();'); | ||
}); | ||
}); | ||
}); |
8 changes: 8 additions & 0 deletions
8
packages/instrumenter/testResources/instrumenter/functional-chains.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
foo?.bar?.trim?.(); | ||
|
||
baz?.trim(); | ||
|
||
qux.trim().substring(3); | ||
|
||
quux.trim?.().substring(3); | ||
|
Oops, something went wrong.