Skip to content

Commit

Permalink
fix: parsing values of tokens that look like numbers but arent (start… (
Browse files Browse the repository at this point in the history
#2245)

fix: parsing values of tokens that look like numbers but arent (starts with 0)
  • Loading branch information
astahmer authored Feb 23, 2024
1 parent 6d8c884 commit 31071ba
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .changeset/funny-plants-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
'@pandacss/parser': patch
'@pandacss/core': patch
---

Fix an issue for token names starting with '0'

```ts
import { defineConfig } from '@pandacss/dev'

export default defineConfig({
theme: {
tokens: {
spacing: {
'025': {
value: '0.125rem',
},
},
},
},
})
```

and then using it like

```ts
css({ margin: '025' })
```

This would not generate the expected CSS because the parser would try to parse `025` as a number (`25`) instead of
keeping it as a string.
7 changes: 7 additions & 0 deletions packages/core/src/style-decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,14 @@ const getEntryFromHash = (hash: string) => {
return entry
}

const startsWithZero = /^0\d+$/
const parseValue = (value: string) => {
// Check if value starts with '0' and is followed by a number
// like '01', '02', etc. If so, return the value as is, it's meant to be a string
if (startsWithZero.test(value)) {
return value
}

const asNumber = Number(value)
if (!Number.isNaN(asNumber)) return asNumber
return castBoolean(value)
Expand Down
40 changes: 40 additions & 0 deletions packages/parser/__tests__/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3596,4 +3596,44 @@ describe('extract to css output pipeline', () => {
}"
`)
})

test('tokens starting with 0', () => {
const code = `
import { css } from "styled-system/css"
css({ margin: "025" })
`
const result = parseAndExtract(code, {
theme: {
tokens: {
spacing: {
'025': {
value: '0.125rem',
},
},
},
},
})
expect(result.json).toMatchInlineSnapshot(`
[
{
"data": [
{
"margin": "025",
},
],
"name": "css",
"type": "css",
},
]
`)

expect(result.css).toMatchInlineSnapshot(`
"@layer utilities {
.m_025 {
margin: var(--spacing-025);
}
}"
`)
})
})

0 comments on commit 31071ba

Please sign in to comment.