Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't add units number values used for variables #694

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 54 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 38 additions & 1 deletion packages/babel-plugin/__tests__/stylex-transform-create-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@ function transform(source, opts = {}) {
parserOpts: {
flow: 'all',
},
plugins: [flowPlugin, [stylexPlugin, { runtimeInjection: true, ...opts }]],
babelrc: false,
plugins: [
flowPlugin,
[
stylexPlugin,
{
runtimeInjection: true,
unstable_moduleResolution: { type: 'haste' },
...opts,
},
],
],
}).code;
}

Expand Down Expand Up @@ -163,6 +174,32 @@ describe('@stylexjs/babel-plugin', () => {
`);
});

test('does not add unit when setting variable value', () => {
expect(
transform(
`
import * as stylex from '@stylexjs/stylex';
import {vars} from 'myTheme.stylex.js';

const styles = stylex.create({
default: {
[vars.foo]: 500,
},
});
`,
{
filename: 'MyComponent.js',
},
),
).toMatchInlineSnapshot(`
"import _inject from "@stylexjs/stylex/lib/stylex-inject";
var _inject2 = _inject;
import * as stylex from '@stylexjs/stylex';
import { vars } from 'myTheme.stylex.js';
_inject2(".x4b9xku{--x1w7bng0:500}", 1);"
`);
});

test('handles camelCased transition properties', () => {
const camelCased = transform(`
import stylex from 'stylex';
Expand Down
26 changes: 26 additions & 0 deletions packages/shared/__tests__/stylex-create-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,32 @@ describe('stylex-create-test', () => {
`);
});

test('does not add units to variable value', () => {
expect(
styleXCreate({
default: {
'--foo': 500,
},
}),
).toMatchInlineSnapshot(`
[
{
"default": {
"$$css": true,
"--foo": "xwzgxvi",
},
},
{
"xwzgxvi": {
"ltr": ".xwzgxvi{--foo:500}",
"priority": 1,
"rtl": null,
},
},
]
`);
});

test('transforms nested pseudo-class to CSS', () => {
expect(
styleXCreate({
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/transform-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default function transformValue(
}

export function getNumberSuffix(key: string): string {
if (unitlessNumberProperties.has(key)) {
if (unitlessNumberProperties.has(key) || key.startsWith('--')) {
return '';
}
if (!(key in numberPropertySuffixes)) {
Expand Down