Skip to content

Commit

Permalink
Fix: Remove usage of ?? operator (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
nmn authored Nov 29, 2023
1 parent 9cf6d83 commit 7c39dc1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,26 @@ describe('custom path evaluation works as expected', () => {
"async": false,
"body": {
"alternate": {
"left": {
"alternate": {
"type": "StringLiteral",
"value": "initial",
},
"consequent": {
"name": "val",
"type": "Identifier",
},
"operator": "??",
"right": {
"type": "StringLiteral",
"value": "initial",
"test": {
"left": {
"name": "val",
"type": "Identifier",
},
"operator": "!=",
"right": {
"type": "NullLiteral",
},
"type": "BinaryExpression",
},
"type": "LogicalExpression",
"type": "ConditionalExpression",
},
"consequent": {
"left": {
Expand Down Expand Up @@ -257,16 +267,26 @@ describe('custom path evaluation works as expected', () => {
"async": false,
"body": {
"alternate": {
"left": {
"alternate": {
"type": "StringLiteral",
"value": "initial",
},
"consequent": {
"name": "val",
"type": "Identifier",
},
"operator": "??",
"right": {
"type": "StringLiteral",
"value": "initial",
"test": {
"left": {
"name": "val",
"type": "Identifier",
},
"operator": "!=",
"right": {
"type": "NullLiteral",
},
"type": "BinaryExpression",
},
"type": "LogicalExpression",
"type": "ConditionalExpression",
},
"consequent": {
"left": {
Expand Down
16 changes: 8 additions & 8 deletions packages/babel-plugin/__tests__/stylex-transform-create-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ describe('@stylexjs/babel-plugin', () => {
color: "x19dipnz",
$$css: true
}, {
"--color": color ?? "initial"
"--color": color != null ? color : "initial"
}]
};"
`);
Expand All @@ -1085,7 +1085,7 @@ describe('@stylexjs/babel-plugin', () => {
width: "x17fnjtu",
$$css: true
}, {
"--width": (val => typeof val === "number" ? val + "px" : val ?? "initial")(width)
"--width": (val => typeof val === "number" ? val + "px" : val != null ? val : "initial")(width)
}]
};"
`);
Expand Down Expand Up @@ -1116,7 +1116,7 @@ describe('@stylexjs/babel-plugin', () => {
color: "x19dipnz",
$$css: true
}, {
"--color": color ?? "initial"
"--color": color != null ? color : "initial"
}],
mono: {
color: "x1mqxbix",
Expand Down Expand Up @@ -1144,7 +1144,7 @@ describe('@stylexjs/babel-plugin', () => {
"--background-color": "xyv4n8w",
$$css: true
}, {
"----background-color": bgColor ?? "initial"
"----background-color": bgColor != null ? bgColor : "initial"
}]
};"
`);
Expand Down Expand Up @@ -1172,7 +1172,7 @@ describe('@stylexjs/babel-plugin', () => {
":hover_color": "x11bf1mc",
$$css: true
}, {
"--1ijzsae": color ?? "initial"
"--1ijzsae": color != null ? color : "initial"
}]
};"
`);
Expand Down Expand Up @@ -1202,7 +1202,7 @@ describe('@stylexjs/babel-plugin', () => {
color: "x19dipnz",
$$css: true
}, {
"--color": color ?? "initial"
"--color": color != null ? color : "initial"
}],
mono: {
color: "x1mqxbix",
Expand Down Expand Up @@ -1230,7 +1230,7 @@ describe('@stylexjs/babel-plugin', () => {
"--background-color": "xyv4n8w",
$$css: true
}, {
"----background-color": bgColor ?? "initial"
"----background-color": bgColor != null ? bgColor : "initial"
}]
};"
`);
Expand Down Expand Up @@ -1258,7 +1258,7 @@ describe('@stylexjs/babel-plugin', () => {
":hover_color": "x11bf1mc",
$$css: true
}, {
"--1ijzsae": color ?? "initial"
"--1ijzsae": color != null ? color : "initial"
}]
};"
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,21 @@ function evaluatePartialObjectRecursively(
t.identifier('val'),
t.stringLiteral(unit),
),
t.logicalExpression(
'??',
t.conditionalExpression(
t.binaryExpression(
'!=',
t.identifier('val'),
t.nullLiteral(),
),
t.identifier('val'),
t.stringLiteral('initial'),
),
),
),
[(expression: t.Expression)],
)
: t.logicalExpression(
'??',
: t.conditionalExpression(
t.binaryExpression('!=', expression, t.nullLiteral()),
expression,
t.stringLiteral('initial'),
);
Expand Down

0 comments on commit 7c39dc1

Please sign in to comment.