Skip to content
This repository has been archived by the owner on Sep 4, 2023. It is now read-only.

Commit

Permalink
feat(core): handle PrefixUnaryExpression.MinusToken
Browse files Browse the repository at this point in the history
  • Loading branch information
astahmer committed Feb 28, 2023
1 parent f550ed7 commit 83bfd0a
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-nails-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@box-extractor/core": patch
---

feat(core): handle PrefixUnaryExpression.MinusToken
11 changes: 11 additions & 0 deletions packages/box-extractor/src/extractor/maybeBoxNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ export function maybeBoxNode(node: Node, stack: Node[]): MaybeBoxNodeReturn {
return cache(box.literal(node.getLiteralValue(), node, stack));
}

if (Node.isPrefixUnaryExpression(node)) {
const operand = node.getOperand();
const operator = node.getOperatorToken();
const maybeBox = maybeBoxNode(operand, stack);
if (!maybeBox || !(maybeBox.isLiteral() && maybeBox.kind === "number")) return;

return cache(
operator === ts.SyntaxKind.MinusToken ? box.literal(-(maybeBox.value as number), node, stack) : maybeBox
);
}

// <ColorBox bool={true} falsy={false} />
if (Node.isTrueLiteral(node) || Node.isFalseLiteral(node)) {
return cache(box.literal(node.getLiteralValue(), node, stack));
Expand Down
84 changes: 84 additions & 0 deletions packages/box-extractor/tests/extract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6482,6 +6482,90 @@ it("extract JsxAttribute > JsxExpression > NumericLiteral", () => {
`);
});

it("extract JsxAttribute > JsxExpression > NumericLiteral > PrefixUnaryExpression", () => {
expect(
extractFromCode(
`
<ThreeBox zIndex={1} position={[
-1.2466866852487384, 0.3325255778835592, -0.6517939595349769,
]} scale={+1.25} someProp={-2}></ThreeBox>
`,
{ components: ["ThreeBox"] }
)
).toMatchInlineSnapshot(`
[
[
"ThreeBox",
[
["zIndex", 1],
["position", [-1.2466866852487384, 0.3325255778835592, -0.6517939595349769]],
["scale", 1.25],
["someProp", -2],
],
{
zIndex: [
{
stack: ["JsxAttribute", "JsxExpression", "NumericLiteral"],
type: "literal",
node: "NumericLiteral",
value: 1,
kind: "number",
},
],
position: [
{
stack: ["JsxAttribute", "JsxExpression", "ArrayLiteralExpression"],
type: "list",
node: "ArrayLiteralExpression",
value: [
{
stack: ["JsxAttribute", "JsxExpression", "ArrayLiteralExpression"],
type: "literal",
node: "PrefixUnaryExpression",
value: -1.2466866852487384,
kind: "number",
},
{
stack: ["JsxAttribute", "JsxExpression", "ArrayLiteralExpression"],
type: "literal",
node: "NumericLiteral",
value: 0.3325255778835592,
kind: "number",
},
{
stack: ["JsxAttribute", "JsxExpression", "ArrayLiteralExpression"],
type: "literal",
node: "PrefixUnaryExpression",
value: -0.6517939595349769,
kind: "number",
},
],
},
],
scale: [
{
stack: ["JsxAttribute", "JsxExpression", "PrefixUnaryExpression"],
type: "literal",
node: "NumericLiteral",
value: 1.25,
kind: "number",
},
],
someProp: [
{
stack: ["JsxAttribute", "JsxExpression", "PrefixUnaryExpression"],
type: "literal",
node: "PrefixUnaryExpression",
value: -2,
kind: "number",
},
],
},
],
]
`);
});

it("extract JsxAttribute > JsxExpression > Identifier > NumericLiteral", () => {
expect(
extractFromCode(`
Expand Down

0 comments on commit 83bfd0a

Please sign in to comment.