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

Don't mutate string concatenations with the aritmatic operator #2646

Closed
nicojs opened this issue Dec 7, 2020 · 3 comments
Closed

Don't mutate string concatenations with the aritmatic operator #2646

nicojs opened this issue Dec 7, 2020 · 3 comments
Labels
🚀 Feature request New feature request

Comments

@nicojs
Copy link
Member

nicojs commented Dec 7, 2020

Is your feature request related to a problem? Please describe.
Currently, we mutate string concatenations with the arithmetic operator mutator.

"foo" + "bar"

Is mutated to:

"foo" - "bar"

This is all in all not that helpful, as such expressions result in NaN. You do want to mutate these strings, as you want to make sure they are tested. However, the string literal mutator already does a great job at it.

Describe the solution you'd like

As soon as 1 of the 2 operands is a string literal, don't mutate + into -,

image

Describe alternatives you've considered

Additional context

@Garethp
Copy link
Contributor

Garethp commented Dec 7, 2020

As far as I'm aware I think you only need to traverse your way down the left path of the tree. If there's a string on the right hand side, but nothing on the left hand then it could still be an arithmetic operator. For example:

'a' + 3 + 2 will result in a32, but 3 + 2 + 'a' will result in 5a. It leaves a gap when you have instances of

const a = 'a';
const b = 'b';
console.log(a + b + 'c');

where you won't be able to tell if a + b is a string concatenation or an arithmetic operation, but it'll still help to optomise cases where we can

@nicojs
Copy link
Member Author

nicojs commented Dec 7, 2020

As far as I'm aware I think you only need to traverse your way down the left path of the tree

I don't think so. What I propose here, is a 'shallow' search. In other words: 'a' + 3 + 2 would still be mutated in 'a' + 3 - 2.

where you won't be able to tell if a + b is a string concatenation or an arithmetic operation

Yes, you are correct. This is why we originally decided not to differentiate.

I don't want to have too much logic inside mutators. What I propose here, is "best-effort". If it is not immediately visible if it's a string concatenation or not, then we mutate. It is at least better than what we have now, and we can add more exceptions if they occur in actual production code.

@Garethp
Copy link
Contributor

Garethp commented Dec 11, 2020

This should be resolve by #2648. We can re-open this if we find that solution isn't suitable

@Garethp Garethp closed this as completed Dec 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚀 Feature request New feature request
Projects
None yet
Development

No branches or pull requests

2 participants