Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(instrumentor): Do not run the arithmetic operator mutator on obvious string concatenations #2648

Merged
merged 4 commits into from Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions packages/instrumenter/src/mutators/arithmetic-operator-mutator.ts
Expand Up @@ -18,7 +18,7 @@ export class ArithmeticOperatorMutator implements NodeMutator {
public name = 'ArithmeticOperator';

public mutate(path: NodePath): NodeMutation[] {
if (path.isBinaryExpression() && this.isSupported(path.node.operator)) {
if (path.isBinaryExpression() && this.isSupported(path.node.operator, path.node)) {
const mutatedOperator = this.operators[path.node.operator];
const replacement = types.cloneNode(path.node, false);
replacement.operator = mutatedOperator;
Expand All @@ -28,7 +28,17 @@ export class ArithmeticOperatorMutator implements NodeMutator {
return [];
}

private isSupported(operator: string): operator is keyof typeof ArithmeticOperators {
return Object.keys(this.operators).includes(operator);
private isSupported(operator: string, node: types.BinaryExpression): operator is keyof typeof ArithmeticOperators {
if (!Object.keys(this.operators).includes(operator)) {
return false;
}

const stringTypes = ['StringLiteral', 'TemplateLiteral'];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On further thought, this should probably be a whitelist not a blacklist. It's a bit weird, but something like:

const log = (thingToLog) => console.log(thingToLog);
const concat = 1 + log;
console.log(concat)

Will result in "1(thingToLog) => console.log(thingToLog)". What do you think @nicojs, should we do a whitelist of things that actually make sense for an ArithmeticOperator, or keep it simple and only make exceptions for strings and template strings?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a whitelist can become big and hard(er) to maintain. I personally think this is fine to start with. It's better than what we have now.


if (stringTypes.includes(node.right.type) || stringTypes.includes(node.left.type)) {
return false;
}

return true;
}
}
Expand Up @@ -23,4 +23,14 @@ describe(ArithmeticOperatorMutator.name, () => {
expectJSMutation(sut, 'a / b', 'a * b');
expectJSMutation(sut, 'a % b', 'a * b');
});

it('should not mutate string literal concatenation', () => {
expectJSMutation(sut, '"a" + "b"');
expectJSMutation(sut, 'const a = 1; "a" + a');
expectJSMutation(sut, '3 + "a"');

expectJSMutation(sut, '`a` + `b`');
expectJSMutation(sut, 'const a = 1; `a` + a');
expectJSMutation(sut, '3 + `a`');
});
});