Skip to content

Commit 74e1a0d

Browse files
committed
Thanks De Morgan
1 parent ab49971 commit 74e1a0d

File tree

1 file changed

+74
-46
lines changed

1 file changed

+74
-46
lines changed

transform.js

Lines changed: 74 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,62 @@ const calls = [
55
'lowPriorityWarningWithoutStack',
66
];
77

8+
function simplify(thing) {
9+
if (!thing) {
10+
return thing;
11+
}
12+
if (thing.type === 'UnaryExpression' && thing.operator === '!') {
13+
if (
14+
thing.argument.type === 'UnaryExpression' &&
15+
thing.argument.operator === '!'
16+
) {
17+
return thing.argument.argument;
18+
}
19+
if (thing.argument.type === 'BinaryExpression') {
20+
let newOperator;
21+
switch (thing.argument.operator) {
22+
case '===': {
23+
newOperator = '!==';
24+
break;
25+
}
26+
case '!==': {
27+
newOperator = '===';
28+
break;
29+
}
30+
case '==': {
31+
newOperator = '!=';
32+
break;
33+
}
34+
case '!=': {
35+
newOperator = '==';
36+
break;
37+
}
38+
case '<=': {
39+
newOperator = '>';
40+
break;
41+
}
42+
case '<': {
43+
newOperator = '>=';
44+
break;
45+
}
46+
case '>': {
47+
newOperator = '<=';
48+
break;
49+
}
50+
case '>=': {
51+
newOperator = '<';
52+
break;
53+
}
54+
default: {
55+
throw thing.argument.operator;
56+
}
57+
}
58+
return {...thing.argument, operator: newOperator};
59+
}
60+
}
61+
return thing;
62+
}
63+
864
module.exports = function(fileInfo, {jscodeshift: j}) {
965
return j(fileInfo.source)
1066
.find('CallExpression')
@@ -18,58 +74,30 @@ module.exports = function(fileInfo, {jscodeshift: j}) {
1874
}
1975

2076
let returnable = j.unaryExpression('!', conditional);
21-
if (
22-
conditional.type === 'BinaryExpression'
23-
) {
24-
let newOperator;
77+
if (conditional.type === 'LogicalExpression') {
2578
switch (conditional.operator) {
26-
case '===': {
27-
newOperator = '!==';
28-
break;
29-
}
30-
case '!==': {
31-
newOperator = '===';
32-
break;
33-
}
34-
case '==': {
35-
newOperator = '!=';
36-
break;
37-
}
38-
case '!=': {
39-
newOperator = '==';
40-
break;
41-
}
42-
case '<=': {
43-
newOperator = '>';
79+
case '||':
80+
if (conditional.left.type !== 'LogicalExpression') {
81+
returnable = j.logicalExpression(
82+
'&&',
83+
simplify(j.unaryExpression('!', conditional.left)),
84+
simplify(j.unaryExpression('!', conditional.right))
85+
);
86+
}
4487
break;
45-
}
46-
case '<': {
47-
newOperator = '>=';
88+
case '&&':
89+
if (conditional.left.type !== 'LogicalExpression') {
90+
returnable = j.logicalExpression(
91+
'||',
92+
simplify(j.unaryExpression('!', conditional.left)),
93+
simplify(j.unaryExpression('!', conditional.right))
94+
);
95+
}
4896
break;
49-
}
50-
case '>': {
51-
newOperator = '<=';
52-
break;
53-
}
54-
case '>=': {
55-
newOperator = '<';
56-
break;
57-
}
58-
default: {
59-
throw conditional.operator;
60-
}
6197
}
62-
returnable = {...conditional, operator: newOperator};
6398
}
64-
if (
65-
conditional.type === 'UnaryExpression' &&
66-
conditional.operator === '!'
67-
) {
68-
returnable = conditional.argument;
69-
}
70-
7199
return j.ifStatement(
72-
returnable,
100+
simplify(returnable),
73101
j.blockStatement([j.expressionStatement(newFunction)])
74102
);
75103
})

0 commit comments

Comments
 (0)