@stylexjs/babel-plugin 0.1.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +16 -0
- package/__tests__/stylex-evaluation-test.js +164 -0
- package/__tests__/stylex-metadata-test.js +103 -0
- package/__tests__/stylex-transform-call-test.js +775 -0
- package/__tests__/stylex-transform-create-test.js +551 -0
- package/__tests__/stylex-transform-import-test.js +310 -0
- package/__tests__/stylex-transform-keyframes-test.js +135 -0
- package/__tests__/stylex-transform-logical-properties-test.js +736 -0
- package/__tests__/stylex-transform-logical-values-test.js +440 -0
- package/__tests__/stylex-transform-polyfills-test.js +65 -0
- package/__tests__/stylex-transform-value-normalize-test.js +287 -0
- package/__tests__/stylex-transform-variable-removal-test.js +112 -0
- package/__tests__/stylex-validation-create-test.ts +310 -0
- package/__tests__/stylex-validation-custom-properties-test.ts +122 -0
- package/__tests__/stylex-validation-declarations-test.ts +129 -0
- package/__tests__/stylex-validation-import-test.ts +57 -0
- package/__tests__/stylex-validation-keyframes-test.ts +109 -0
- package/jest.config.ts +20 -0
- package/package.json +26 -0
- package/rollup.config.mjs +30 -0
- package/tsconfig.json +103 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
'use strict';
|
|
10
|
+
|
|
11
|
+
jest.autoMockOff();
|
|
12
|
+
|
|
13
|
+
const { transformSync } = require('@babel/core');
|
|
14
|
+
const stylexPlugin = require('../src/index');
|
|
15
|
+
|
|
16
|
+
function transform(source, opts = {}) {
|
|
17
|
+
return transformSync(source, {
|
|
18
|
+
filename: opts.filename,
|
|
19
|
+
parserOpts: {
|
|
20
|
+
flow: {
|
|
21
|
+
all: true,
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
plugins: [[stylexPlugin, opts]],
|
|
25
|
+
}).code;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe('@stylexjs/babel-plugin', () => {
|
|
29
|
+
/**
|
|
30
|
+
* CSS value normalization
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
describe('[transform] CSS value normalization', () => {
|
|
34
|
+
test('normalize whitespace in CSS values', () => {
|
|
35
|
+
expect(
|
|
36
|
+
transform(`
|
|
37
|
+
import stylex from 'stylex';
|
|
38
|
+
const styles = stylex.create({
|
|
39
|
+
x: {
|
|
40
|
+
transform: ' rotate(10deg) translate3d( 0 , 0 , 0 ) '
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
`)
|
|
44
|
+
).toMatchInlineSnapshot(`
|
|
45
|
+
"import stylex from 'stylex';
|
|
46
|
+
stylex.inject(".x18qx21s{transform:rotate(10deg) translate3d(0,0,0)}", 1);"
|
|
47
|
+
`);
|
|
48
|
+
expect(
|
|
49
|
+
transform(`
|
|
50
|
+
import stylex from 'stylex';
|
|
51
|
+
const styles = stylex.create({ x: { color: 'rgba( 1, 222, 33 , 0.5)' } });
|
|
52
|
+
`)
|
|
53
|
+
).toMatchInlineSnapshot(`
|
|
54
|
+
"import stylex from 'stylex';
|
|
55
|
+
stylex.inject(".xe1l9yr{color:rgba(1,222,33,.5)}", 1);"
|
|
56
|
+
`);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('no dimensions for 0 values', () => {
|
|
60
|
+
expect(
|
|
61
|
+
transform(`
|
|
62
|
+
import stylex from 'stylex';
|
|
63
|
+
const styles = stylex.create({ x: {
|
|
64
|
+
margin: '0px',
|
|
65
|
+
marginLeft: '1px'
|
|
66
|
+
} });
|
|
67
|
+
`)
|
|
68
|
+
).toMatchInlineSnapshot(`
|
|
69
|
+
"import stylex from 'stylex';
|
|
70
|
+
stylex.inject(".xdj266r{margin-top:0}", 1);
|
|
71
|
+
stylex.inject(".x11i5rnm{margin-right:0}", 1, ".x11i5rnm{margin-left:0}");
|
|
72
|
+
stylex.inject(".xat24cr{margin-bottom:0}", 1);
|
|
73
|
+
stylex.inject(".x1mh8g0r{margin-left:0}", 1, ".x1mh8g0r{margin-right:0}");
|
|
74
|
+
stylex.inject(".xgsvwom{margin-left:1px}", 1.1);"
|
|
75
|
+
`);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('0 timings are all "0s"', () => {
|
|
79
|
+
expect(
|
|
80
|
+
transform(`
|
|
81
|
+
import stylex from 'stylex';
|
|
82
|
+
const styles = stylex.create({ x: { transitionDuration: '500ms' } });
|
|
83
|
+
`)
|
|
84
|
+
).toMatchInlineSnapshot(`
|
|
85
|
+
"import stylex from 'stylex';
|
|
86
|
+
stylex.inject(".x1wsgiic{transition-duration:.5s}", 1);"
|
|
87
|
+
`);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test('0 angles are all "0deg"', () => {
|
|
91
|
+
expect(
|
|
92
|
+
transform(`
|
|
93
|
+
import stylex from 'stylex';
|
|
94
|
+
const styles = stylex.create({
|
|
95
|
+
x: { transform: '0rad' },
|
|
96
|
+
y: { transform: '0turn' },
|
|
97
|
+
z: { transform: '0grad' }
|
|
98
|
+
});
|
|
99
|
+
`)
|
|
100
|
+
).toMatchInlineSnapshot(`
|
|
101
|
+
"import stylex from 'stylex';
|
|
102
|
+
stylex.inject(".x1jpfit1{transform:0deg}", 1);"
|
|
103
|
+
`);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test('calc() preserves spaces aroung "+" and "-"', () => {
|
|
107
|
+
expect(
|
|
108
|
+
transform(`
|
|
109
|
+
import stylex from 'stylex';
|
|
110
|
+
const styles = stylex.create({ x: { width: 'calc((100% + 3% - 100px) / 7)' } });
|
|
111
|
+
`)
|
|
112
|
+
).toMatchInlineSnapshot(`
|
|
113
|
+
"import stylex from 'stylex';
|
|
114
|
+
stylex.inject(".x1hauit9{width:calc((100% + 3% - 100px) / 7)}", 1);"
|
|
115
|
+
`);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test('strip leading zeros', () => {
|
|
119
|
+
expect(
|
|
120
|
+
transform(`
|
|
121
|
+
import stylex from 'stylex';
|
|
122
|
+
const styles = stylex.create({ x: {
|
|
123
|
+
transitionDuration: '0.01s',
|
|
124
|
+
transitionTimingFunction: 'cubic-bezier(.08,.52,.52,1)'
|
|
125
|
+
} });
|
|
126
|
+
`)
|
|
127
|
+
).toMatchInlineSnapshot(`
|
|
128
|
+
"import stylex from 'stylex';
|
|
129
|
+
stylex.inject(".xpvlhck{transition-duration:.01s}", 1);
|
|
130
|
+
stylex.inject(".xxziih7{transition-timing-function:cubic-bezier(.08,.52,.52,1)}", 1);"
|
|
131
|
+
`);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test('use double quotes in empty strings', () => {
|
|
135
|
+
expect(
|
|
136
|
+
transform(`
|
|
137
|
+
import stylex from 'stylex';
|
|
138
|
+
const styles = stylex.create({ x: { quotes: "''" } });
|
|
139
|
+
`)
|
|
140
|
+
).toMatchInlineSnapshot(`
|
|
141
|
+
"import stylex from 'stylex';
|
|
142
|
+
stylex.inject(".x169joja{quotes:\\"\\"}", 1);"
|
|
143
|
+
`);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test('timing values are converted to seconds unless < 10ms', () => {
|
|
147
|
+
expect(
|
|
148
|
+
transform(`
|
|
149
|
+
import stylex from 'stylex';
|
|
150
|
+
const styles = stylex.create({
|
|
151
|
+
x: { transitionDuration: '1234ms' },
|
|
152
|
+
y: { transitionDuration: '10ms' },
|
|
153
|
+
z: { transitionDuration: '1ms' }
|
|
154
|
+
});
|
|
155
|
+
`)
|
|
156
|
+
).toMatchInlineSnapshot(`
|
|
157
|
+
"import stylex from 'stylex';
|
|
158
|
+
stylex.inject(".xsa3hc2{transition-duration:1.234s}", 1);
|
|
159
|
+
stylex.inject(".xpvlhck{transition-duration:.01s}", 1);
|
|
160
|
+
stylex.inject(".xjd9b36{transition-duration:1ms}", 1);"
|
|
161
|
+
`);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test('transforms non-unitless property values', () => {
|
|
165
|
+
expect(
|
|
166
|
+
transform(`
|
|
167
|
+
import stylex from 'stylex';
|
|
168
|
+
const styles = stylex.create({
|
|
169
|
+
normalize: {
|
|
170
|
+
height: 500,
|
|
171
|
+
margin: 10,
|
|
172
|
+
width: 500
|
|
173
|
+
},
|
|
174
|
+
unitless: {
|
|
175
|
+
fontWeight: 500,
|
|
176
|
+
lineHeight: 1.5,
|
|
177
|
+
opacity: 0.5
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
`)
|
|
181
|
+
).toMatchInlineSnapshot(`
|
|
182
|
+
"import stylex from 'stylex';
|
|
183
|
+
stylex.inject(".x1egiwwb{height:500px}", 1);
|
|
184
|
+
stylex.inject(".x1anpbxc{margin-top:10px}", 1);
|
|
185
|
+
stylex.inject(".xmo9yow{margin-right:10px}", 1, ".xmo9yow{margin-left:10px}");
|
|
186
|
+
stylex.inject(".xyorhqc{margin-bottom:10px}", 1);
|
|
187
|
+
stylex.inject(".x17adc0v{margin-left:10px}", 1, ".x17adc0v{margin-right:10px}");
|
|
188
|
+
stylex.inject(".xvue9z{width:500px}", 1);
|
|
189
|
+
stylex.inject(".xk50ysn{font-weight:500}", 1);
|
|
190
|
+
stylex.inject(".x1evy7pa{line-height:1.5}", 1);
|
|
191
|
+
stylex.inject(".xbyyjgo{opacity:.5}", 1);"
|
|
192
|
+
`);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test('number values rounded down to four decimal points', () => {
|
|
196
|
+
expect(
|
|
197
|
+
transform(`
|
|
198
|
+
import stylex from 'stylex';
|
|
199
|
+
const styles = stylex.create({ x: { height: 100 / 3 } });
|
|
200
|
+
`)
|
|
201
|
+
).toMatchInlineSnapshot(`
|
|
202
|
+
"import stylex from 'stylex';
|
|
203
|
+
stylex.inject(".x1vvwc6p{height:33.3333px}", 1);"
|
|
204
|
+
`);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test('"content" property values are wrapped in quotes', () => {
|
|
208
|
+
expect(
|
|
209
|
+
transform(`
|
|
210
|
+
import stylex from 'stylex';
|
|
211
|
+
const styles = stylex.create({
|
|
212
|
+
default: {
|
|
213
|
+
content: '',
|
|
214
|
+
},
|
|
215
|
+
other: {
|
|
216
|
+
content: 'next',
|
|
217
|
+
},
|
|
218
|
+
withQuotes: {
|
|
219
|
+
content: '"prev"',
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
`)
|
|
223
|
+
).toMatchInlineSnapshot(`
|
|
224
|
+
"import stylex from 'stylex';
|
|
225
|
+
stylex.inject(".x14axycx{content:\\"\\"}", 1);
|
|
226
|
+
stylex.inject(".xmmpjw1{content:\\"next\\"}", 1);
|
|
227
|
+
stylex.inject(".x12vzfr8{content:\\"prev\\"}", 1);"
|
|
228
|
+
`);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test('[legacy] transforms font size from px to rem', () => {
|
|
232
|
+
expect(
|
|
233
|
+
transform(`
|
|
234
|
+
import stylex from 'stylex';
|
|
235
|
+
const styles = stylex.create({
|
|
236
|
+
foo: {
|
|
237
|
+
fontSize: '24px',
|
|
238
|
+
},
|
|
239
|
+
bar: {
|
|
240
|
+
fontSize: 18,
|
|
241
|
+
},
|
|
242
|
+
baz: {
|
|
243
|
+
fontSize: '1.25rem',
|
|
244
|
+
},
|
|
245
|
+
qux: {
|
|
246
|
+
fontSize: 'inherit',
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
`)
|
|
250
|
+
).toMatchInlineSnapshot(`
|
|
251
|
+
"import stylex from 'stylex';
|
|
252
|
+
stylex.inject(".xngnso2{font-size:1.5rem}", 1);
|
|
253
|
+
stylex.inject(".x1c3i2sq{font-size:1.125rem}", 1);
|
|
254
|
+
stylex.inject(".x1603h9y{font-size:1.25rem}", 1);
|
|
255
|
+
stylex.inject(".x1qlqyl8{font-size:inherit}", 1);"
|
|
256
|
+
`);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
test('[legacy] transforms font size from px to rem even with calc', () => {
|
|
260
|
+
expect(
|
|
261
|
+
transform(`
|
|
262
|
+
import stylex from 'stylex';
|
|
263
|
+
const styles = stylex.create({
|
|
264
|
+
foo: {
|
|
265
|
+
fontSize: 'calc(100% - 24px)',
|
|
266
|
+
},
|
|
267
|
+
});
|
|
268
|
+
`)
|
|
269
|
+
).toMatchInlineSnapshot(`
|
|
270
|
+
"import stylex from 'stylex';
|
|
271
|
+
stylex.inject(".x37c5sx{font-size:calc(100% - 1.5rem)}", 1);"
|
|
272
|
+
`);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
test('[legacy] no space before "!important"', () => {
|
|
276
|
+
expect(
|
|
277
|
+
transform(`
|
|
278
|
+
import stylex from 'stylex';
|
|
279
|
+
const styles = stylex.create({ x: { color: 'red !important' } });
|
|
280
|
+
`)
|
|
281
|
+
).toMatchInlineSnapshot(`
|
|
282
|
+
"import stylex from 'stylex';
|
|
283
|
+
stylex.inject(".xzw3067{color:red!important}", 1);"
|
|
284
|
+
`);
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
});
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
'use strict';
|
|
9
|
+
|
|
10
|
+
jest.autoMockOff();
|
|
11
|
+
|
|
12
|
+
const { transformSync } = require('@babel/core');
|
|
13
|
+
const stylexPlugin = require('../src/index');
|
|
14
|
+
|
|
15
|
+
function transform(source, opts = {}) {
|
|
16
|
+
const options = {
|
|
17
|
+
filename: opts.filename,
|
|
18
|
+
plugins: [[stylexPlugin, opts]],
|
|
19
|
+
};
|
|
20
|
+
const result = transformSync(source, options);
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe('[optimization] Removes `styles` variable when not needed', () => {
|
|
25
|
+
test('Keeps used styles', () => {
|
|
26
|
+
const result = transform(`
|
|
27
|
+
import stylex from 'stylex';
|
|
28
|
+
|
|
29
|
+
const styles = stylex.create({
|
|
30
|
+
default: {
|
|
31
|
+
backgroundColor: 'red',
|
|
32
|
+
color: 'blue',
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
styles;
|
|
36
|
+
`);
|
|
37
|
+
expect(result.code).toMatchInlineSnapshot(`
|
|
38
|
+
"import stylex from 'stylex';
|
|
39
|
+
stylex.inject(".xrkmrrc{background-color:red}", 1);
|
|
40
|
+
stylex.inject(".xju2f9n{color:blue}", 1);
|
|
41
|
+
const styles = {
|
|
42
|
+
default: {
|
|
43
|
+
backgroundColor: "xrkmrrc",
|
|
44
|
+
color: "xju2f9n"
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
styles;"
|
|
48
|
+
`);
|
|
49
|
+
expect(result.metadata).toMatchInlineSnapshot(`
|
|
50
|
+
{
|
|
51
|
+
"stylex": [
|
|
52
|
+
[
|
|
53
|
+
"xrkmrrc",
|
|
54
|
+
{
|
|
55
|
+
"ltr": ".xrkmrrc{background-color:red}",
|
|
56
|
+
"rtl": null,
|
|
57
|
+
},
|
|
58
|
+
1,
|
|
59
|
+
],
|
|
60
|
+
[
|
|
61
|
+
"xju2f9n",
|
|
62
|
+
{
|
|
63
|
+
"ltr": ".xju2f9n{color:blue}",
|
|
64
|
+
"rtl": null,
|
|
65
|
+
},
|
|
66
|
+
1,
|
|
67
|
+
],
|
|
68
|
+
],
|
|
69
|
+
}
|
|
70
|
+
`);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test('Removes unused styles', () => {
|
|
74
|
+
const result = transform(`
|
|
75
|
+
import stylex from 'stylex';
|
|
76
|
+
|
|
77
|
+
const styles = stylex.create({
|
|
78
|
+
default: {
|
|
79
|
+
backgroundColor: 'red',
|
|
80
|
+
color: 'blue',
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
`);
|
|
84
|
+
expect(result.code).toMatchInlineSnapshot(`
|
|
85
|
+
"import stylex from 'stylex';
|
|
86
|
+
stylex.inject(".xrkmrrc{background-color:red}", 1);
|
|
87
|
+
stylex.inject(".xju2f9n{color:blue}", 1);"
|
|
88
|
+
`);
|
|
89
|
+
expect(result.metadata).toMatchInlineSnapshot(`
|
|
90
|
+
{
|
|
91
|
+
"stylex": [
|
|
92
|
+
[
|
|
93
|
+
"xrkmrrc",
|
|
94
|
+
{
|
|
95
|
+
"ltr": ".xrkmrrc{background-color:red}",
|
|
96
|
+
"rtl": null,
|
|
97
|
+
},
|
|
98
|
+
1,
|
|
99
|
+
],
|
|
100
|
+
[
|
|
101
|
+
"xju2f9n",
|
|
102
|
+
{
|
|
103
|
+
"ltr": ".xju2f9n{color:blue}",
|
|
104
|
+
"rtl": null,
|
|
105
|
+
},
|
|
106
|
+
1,
|
|
107
|
+
],
|
|
108
|
+
],
|
|
109
|
+
}
|
|
110
|
+
`);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
jest.autoMockOff();
|
|
9
|
+
|
|
10
|
+
import { transformSync } from '@babel/core';
|
|
11
|
+
import { messages } from '@stylexjs/shared';
|
|
12
|
+
import stylexPlugin from '../src/index';
|
|
13
|
+
|
|
14
|
+
function transform(source: string, opts: { [key: string]: any } = {}) {
|
|
15
|
+
return transformSync(source, {
|
|
16
|
+
filename: opts.filename,
|
|
17
|
+
parserOpts: {
|
|
18
|
+
flow: {
|
|
19
|
+
all: true,
|
|
20
|
+
},
|
|
21
|
+
} as any,
|
|
22
|
+
plugins: [[stylexPlugin, opts]],
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
describe('@stylexjs/babel-plugin', () => {
|
|
27
|
+
/**
|
|
28
|
+
* stylex.create
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
describe('[validation] stylex.create()', () => {
|
|
32
|
+
test('must be bound to a variable', () => {
|
|
33
|
+
expect(() => {
|
|
34
|
+
transform(`
|
|
35
|
+
import stylex from 'stylex';
|
|
36
|
+
stylex.create({});
|
|
37
|
+
`);
|
|
38
|
+
}).toThrow(messages.UNBOUND_STYLEX_CALL_VALUE);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('must be called at top level', () => {
|
|
42
|
+
expect(() => {
|
|
43
|
+
transform(`
|
|
44
|
+
import stylex from 'stylex';
|
|
45
|
+
if (bar) {
|
|
46
|
+
const styles = stylex.create({});
|
|
47
|
+
}
|
|
48
|
+
`);
|
|
49
|
+
}).toThrow(messages.ONLY_TOP_LEVEL);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('its only argument must be a single object', () => {
|
|
53
|
+
expect(() => {
|
|
54
|
+
transform(`
|
|
55
|
+
import stylex from 'stylex';
|
|
56
|
+
const styles = stylex.create(genStyles());
|
|
57
|
+
`);
|
|
58
|
+
}).toThrow(messages.NON_OBJECT_FOR_STYLEX_CALL);
|
|
59
|
+
expect(() => {
|
|
60
|
+
transform(`
|
|
61
|
+
import stylex from 'stylex';
|
|
62
|
+
const styles = stylex.create();
|
|
63
|
+
`);
|
|
64
|
+
}).toThrow(messages.ILLEGAL_ARGUMENT_LENGTH);
|
|
65
|
+
expect(() => {
|
|
66
|
+
transform(`
|
|
67
|
+
import stylex from 'stylex';
|
|
68
|
+
const styles = stylex.create({}, {});
|
|
69
|
+
`);
|
|
70
|
+
}).toThrow(messages.ILLEGAL_ARGUMENT_LENGTH);
|
|
71
|
+
expect(() => {
|
|
72
|
+
transform(`
|
|
73
|
+
import stylex from 'stylex';
|
|
74
|
+
const styles = stylex.create({});
|
|
75
|
+
`);
|
|
76
|
+
}).not.toThrow();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('namespace keys must be a static value', () => {
|
|
80
|
+
expect(() => {
|
|
81
|
+
transform(`
|
|
82
|
+
import stylex from 'stylex';
|
|
83
|
+
const styles = stylex.create({
|
|
84
|
+
[root]: {
|
|
85
|
+
backgroundColor: 'red',
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
`);
|
|
89
|
+
}).toThrow(messages.NON_STATIC_VALUE);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('namespace values must be an object', () => {
|
|
93
|
+
expect(() => {
|
|
94
|
+
transform(`
|
|
95
|
+
import stylex from 'stylex';
|
|
96
|
+
const styles = stylex.create({
|
|
97
|
+
namespace: false,
|
|
98
|
+
});
|
|
99
|
+
`);
|
|
100
|
+
}).toThrow(messages.ILLEGAL_NAMESPACE_VALUE);
|
|
101
|
+
expect(() => {
|
|
102
|
+
transform(`
|
|
103
|
+
const styles = stylex.create({
|
|
104
|
+
namespace: {},
|
|
105
|
+
});
|
|
106
|
+
`);
|
|
107
|
+
}).not.toThrow();
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
/* Properties */
|
|
111
|
+
|
|
112
|
+
test('properties must be a static value', () => {
|
|
113
|
+
expect(() => {
|
|
114
|
+
transform(`
|
|
115
|
+
import stylex from 'stylex';
|
|
116
|
+
const styles = stylex.create({
|
|
117
|
+
root: {
|
|
118
|
+
[backgroundColor]: 'red',
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
`);
|
|
122
|
+
}).toThrow(messages.NON_STATIC_VALUE);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
/* Values */
|
|
126
|
+
|
|
127
|
+
test('values must be static (arrays of) number or string in stylex.create()', () => {
|
|
128
|
+
// number
|
|
129
|
+
expect(() => {
|
|
130
|
+
transform(`
|
|
131
|
+
import stylex from 'stylex';
|
|
132
|
+
const styles = stylex.create({
|
|
133
|
+
root: {
|
|
134
|
+
padding: 5,
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
`);
|
|
138
|
+
}).not.toThrow();
|
|
139
|
+
// string
|
|
140
|
+
expect(() => {
|
|
141
|
+
transform(`
|
|
142
|
+
import stylex from 'stylex';
|
|
143
|
+
const styles = stylex.create({
|
|
144
|
+
root: {
|
|
145
|
+
backgroundColor: 'red',
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
`);
|
|
149
|
+
}).not.toThrow();
|
|
150
|
+
// array of numbers
|
|
151
|
+
expect(() => {
|
|
152
|
+
transform(`
|
|
153
|
+
import stylex from 'stylex';
|
|
154
|
+
const styles = stylex.create({
|
|
155
|
+
default: {
|
|
156
|
+
transitionDuration: [500],
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
`);
|
|
160
|
+
}).not.toThrow();
|
|
161
|
+
// array of strings
|
|
162
|
+
expect(() => {
|
|
163
|
+
transform(`
|
|
164
|
+
import stylex from 'stylex';
|
|
165
|
+
const styles = stylex.create({
|
|
166
|
+
default: {
|
|
167
|
+
transitionDuration: ['0.5s'],
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
`);
|
|
171
|
+
}).not.toThrow();
|
|
172
|
+
// not string or number
|
|
173
|
+
expect(() => {
|
|
174
|
+
transform(`
|
|
175
|
+
import stylex from 'stylex';
|
|
176
|
+
const styles = stylex.create({
|
|
177
|
+
default: {
|
|
178
|
+
transitionDuration: [[], {}],
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
`);
|
|
182
|
+
}).toThrow(messages.ILLEGAL_PROP_ARRAY_VALUE);
|
|
183
|
+
expect(() => {
|
|
184
|
+
transform(`
|
|
185
|
+
import stylex from 'stylex';
|
|
186
|
+
const styles = stylex.create({
|
|
187
|
+
default: {
|
|
188
|
+
color: true,
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
`);
|
|
192
|
+
}).toThrow(messages.ILLEGAL_PROP_VALUE);
|
|
193
|
+
// not static
|
|
194
|
+
expect(() => {
|
|
195
|
+
transform(`
|
|
196
|
+
import stylex from 'stylex';
|
|
197
|
+
const styles = stylex.create({
|
|
198
|
+
root: {
|
|
199
|
+
backgroundColor: backgroundColor,
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
`);
|
|
203
|
+
}).toThrow(messages.NON_STATIC_VALUE);
|
|
204
|
+
expect(() => {
|
|
205
|
+
transform(`
|
|
206
|
+
import stylex from 'stylex';
|
|
207
|
+
const styles = stylex.create({
|
|
208
|
+
root: {
|
|
209
|
+
backgroundColor: generateBg(),
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
`);
|
|
213
|
+
}).toThrow(messages.NON_STATIC_VALUE);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test('values can reference local bindings in stylex.create()', () => {
|
|
217
|
+
expect(() => {
|
|
218
|
+
transform(`
|
|
219
|
+
import stylex from 'stylex';
|
|
220
|
+
const bg = '#eee';
|
|
221
|
+
const styles = stylex.create({
|
|
222
|
+
root: {
|
|
223
|
+
backgroundColor: bg,
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
`);
|
|
227
|
+
}).not.toThrow();
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test('values can be pure complex expressions in stylex.create()', () => {
|
|
231
|
+
expect(() => {
|
|
232
|
+
transform(`
|
|
233
|
+
import stylex from 'stylex';
|
|
234
|
+
const borderRadius = 2;
|
|
235
|
+
const styles = stylex.create({
|
|
236
|
+
root: {
|
|
237
|
+
borderRadius: borderRadius * 2,
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
`);
|
|
241
|
+
}).not.toThrow();
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test('values can be template literal expressions in stylex.create()', () => {
|
|
245
|
+
expect(() => {
|
|
246
|
+
transform(`
|
|
247
|
+
import stylex from 'stylex';
|
|
248
|
+
const borderSize = 2;
|
|
249
|
+
const styles = stylex.create({
|
|
250
|
+
root: {
|
|
251
|
+
borderRadius: \`\${borderSize * 2}px\`,
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
`);
|
|
255
|
+
}).not.toThrow();
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
/* Complex selectors */
|
|
259
|
+
|
|
260
|
+
test('pseudo-classes must start with ":" character', () => {
|
|
261
|
+
expect(() => {
|
|
262
|
+
transform(`
|
|
263
|
+
import stylex from 'stylex';
|
|
264
|
+
const styles = stylex.create({
|
|
265
|
+
default: {
|
|
266
|
+
':hover': {},
|
|
267
|
+
},
|
|
268
|
+
});
|
|
269
|
+
`);
|
|
270
|
+
}).not.toThrow();
|
|
271
|
+
|
|
272
|
+
expect(() => {
|
|
273
|
+
transform(`
|
|
274
|
+
import stylex from 'stylex';
|
|
275
|
+
const styles = stylex.create({
|
|
276
|
+
default: {
|
|
277
|
+
'hover': {},
|
|
278
|
+
},
|
|
279
|
+
});
|
|
280
|
+
`);
|
|
281
|
+
}).toThrow(messages.INVALID_PSEUDO);
|
|
282
|
+
|
|
283
|
+
expect(() => {
|
|
284
|
+
transform(`
|
|
285
|
+
import stylex from 'stylex';
|
|
286
|
+
const styles = stylex.create({
|
|
287
|
+
default: {
|
|
288
|
+
'&:hover': {},
|
|
289
|
+
},
|
|
290
|
+
});
|
|
291
|
+
`);
|
|
292
|
+
}).toThrow(messages.INVALID_PSEUDO);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
test('pseudo-classes cannot be nested', () => {
|
|
296
|
+
expect(() => {
|
|
297
|
+
transform(`
|
|
298
|
+
import stylex from 'stylex';
|
|
299
|
+
const styles = stylex.create({
|
|
300
|
+
default: {
|
|
301
|
+
':hover': {
|
|
302
|
+
':active': {},
|
|
303
|
+
},
|
|
304
|
+
},
|
|
305
|
+
});
|
|
306
|
+
`);
|
|
307
|
+
}).toThrow(messages.ILLEGAL_NESTED_PSEUDO);
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
});
|