Skip to content

Commit

Permalink
fix: unboxing of 0 values in conditionals (#2212)
Browse files Browse the repository at this point in the history
fix: unboxing of 0 values
  • Loading branch information
Julien-R44 authored Feb 16, 2024
1 parent 78145d8 commit ec93e34
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
72 changes: 72 additions & 0 deletions packages/extractor/__tests__/extract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1759,6 +1759,78 @@ it('extract JsxAttribute > JsxExpression > ConditonalExpression > Identifier|Val
`)
})

it('extract JsxAttribute > JsxExpression > ConditionalExpression > when true is a 0', () => {
expect(
extractFromCode(
`
<div className={css({ opacity: isHovered ? 1 : 0 })}></div>
`,
{
functionNameList: ['css'],
},
),
).toMatchInlineSnapshot(`
{
"css": [
{
"conditions": [
{
"0": {
"opacity": 1,
},
},
{
"0": {
"opacity": 0,
},
},
],
"raw": [
{},
],
"spreadConditions": [],
},
],
}
`)
})

it('extract JsxAttribute > JsxExpression > ConditionalExpression > when false is a 0', () => {
expect(
extractFromCode(
`
<div className={css({ opacity: isHovered ? 1 : 0 })}></div>
`,
{
functionNameList: ['css'],
},
),
).toMatchInlineSnapshot(`
{
"css": [
{
"conditions": [
{
"0": {
"opacity": 1,
},
},
{
"0": {
"opacity": 0,
},
},
],
"raw": [
{},
],
"spreadConditions": [],
},
],
}
`)
})

it('extract JsxAttribute > JsxExpression > ElementAccessExpression', () => {
expect(
extractFromCode(`
Expand Down
7 changes: 7 additions & 0 deletions packages/extractor/__tests__/unbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4350,6 +4350,7 @@ test('unbox with conditions', () => {
const className = css({
px: 4,
color: isDark ? 'blue.100' : 'blue.200',
opacity: isHovered ? 1 : 0,
...(isKnown && {
backgroundColor: 'red.100',
padding: false ? 2 : 8,
Expand Down Expand Up @@ -4386,6 +4387,12 @@ test('unbox with conditions', () => {
{
"color": "blue.200",
},
{
"opacity": 1,
},
{
"opacity": 0,
},
{
"fontSize": "2xl",
},
Expand Down
6 changes: 3 additions & 3 deletions packages/extractor/src/unbox.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { box } from './box'
import type { BoxNode } from './box-factory'
import type { LiteralObject, LiteralValue } from './types'
import { isNotNullish } from './utils'
import { isNotNullish, isTruthyOrZero } from './utils'
import { Node } from 'ts-morph'

const makeObjAt = (path: string[], value: unknown) => {
Expand Down Expand Up @@ -52,10 +52,10 @@ const getLiteralValue = (node: BoxNode | undefined, ctx: UnboxContext): LiteralV
return undefined
}

if (whenTrue) {
if (isTruthyOrZero(whenTrue)) {
ctx.conditions.push(makeObjAt(path, whenTrue))
}
if (whenFalse) {
if (isTruthyOrZero(whenFalse)) {
ctx.conditions.push(makeObjAt(path, whenFalse))
}
return undefined
Expand Down
1 change: 1 addition & 0 deletions packages/extractor/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type Nullable<T> = T | null | undefined

export const isNotNullish = <T>(element: Nullable<T>): element is T => element != null
export const isNullish = <T>(element: Nullable<T>): element is null | undefined => element == null
export const isTruthyOrZero = <T>(element: T): element is T => !!element || element === 0

/** Returns true if typeof value is object && not null */
export const isObject = (value: any): value is object => value != null && typeof value === 'object'
Expand Down

0 comments on commit ec93e34

Please sign in to comment.