Skip to content

Commit

Permalink
feat(mutators): mutate nullish coalescing operator (#2884)
Browse files Browse the repository at this point in the history
Allow the logical operator mutator to mutate [nullish coalescing operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator) to a logical [AND (&&) operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND):

```diff
-foo ?? bar
+foo && bar
```
  • Loading branch information
nicojs committed May 11, 2021
1 parent b578b22 commit 021a419
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Expand Up @@ -8,6 +8,7 @@ import { NodeMutator } from '.';
enum LogicalOperators {
'&&' = '||',
'||' = '&&',
'??' = '&&',
}

export class LogicalOperatorMutator implements NodeMutator {
Expand Down
Expand Up @@ -13,8 +13,20 @@ describe(LogicalOperatorMutator.name, () => {
expect(sut.name).eq('LogicalOperator');
});

it('should mutate && and ||', () => {
it('should mutate &&', () => {
expectJSMutation(sut, 'a && b', 'a || b');
});

it('should mutate ||', () => {
expectJSMutation(sut, 'a || b', 'a && b');
});

it('should not mutate & and |', () => {
expectJSMutation(sut, 'a & b');
expectJSMutation(sut, 'a | b');
});

it('should mutate ?? to &&', () => {
expectJSMutation(sut, 'a ?? b', 'a && b');
});
});

0 comments on commit 021a419

Please sign in to comment.