clava 0.5.0 → 0.6.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/CHANGELOG.md +56 -3
- package/README.md +68 -24
- package/dist/index.d.ts +215 -23
- package/dist/index.js +385 -598
- package/dist/index.js.map +1 -1
- package/package.json +17 -2
- package/src/index.ts +364 -642
- package/src/refine-warning.ts +4 -11
- package/src/types.ts +129 -20
- package/src/utils.ts +123 -99
- package/rolldown.config.ts +0 -15
- package/tests/_utils.ts +0 -178
- package/tests/build.test.ts +0 -93
- package/tests/class-style.test.ts +0 -341
- package/tests/component-api.test.ts +0 -548
- package/tests/extend.test.ts +0 -421
- package/tests/function-variants.test.ts +0 -378
- package/tests/language-service.test.ts +0 -181
- package/tests/prototype-pollution.test.ts +0 -46
- package/tests/react.test.ts +0 -40
- package/tests/refine-warning.test.ts +0 -28
- package/tests/refine.test.ts +0 -1542
- package/tests/solid.test.ts +0 -64
- package/tests/split-props.test.ts +0 -662
- package/tests/variants-inference.test.ts +0 -333
- package/tests/variants.test.ts +0 -408
- package/tsconfig.json +0 -7
|
@@ -1,378 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
CONFIGS,
|
|
4
|
-
createCVFromConfig,
|
|
5
|
-
getConfigDescription,
|
|
6
|
-
getConfigMode,
|
|
7
|
-
getConfigTransformClass,
|
|
8
|
-
getModeComponent,
|
|
9
|
-
getStyleClass,
|
|
10
|
-
} from "./_utils.ts";
|
|
11
|
-
|
|
12
|
-
for (const config of Object.values(CONFIGS)) {
|
|
13
|
-
const mode = getConfigMode(config);
|
|
14
|
-
const cv = createCVFromConfig(config);
|
|
15
|
-
const cls = getConfigTransformClass(config);
|
|
16
|
-
|
|
17
|
-
describe(getConfigDescription(config), () => {
|
|
18
|
-
test("function variant", () => {
|
|
19
|
-
const component = getModeComponent(
|
|
20
|
-
mode,
|
|
21
|
-
cv({
|
|
22
|
-
variants: {
|
|
23
|
-
size: (value: "sm" | "lg") => (value === "sm" ? "small" : "large"),
|
|
24
|
-
},
|
|
25
|
-
}),
|
|
26
|
-
);
|
|
27
|
-
const props = component({ size: "lg" });
|
|
28
|
-
expect(getStyleClass(props)).toEqual({ class: cls("large") });
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test("function variant with style", () => {
|
|
32
|
-
const component = getModeComponent(
|
|
33
|
-
mode,
|
|
34
|
-
cv({
|
|
35
|
-
variants: {
|
|
36
|
-
size: (value: "sm" | "lg") => ({
|
|
37
|
-
class: value === "sm" ? "small" : "large",
|
|
38
|
-
style: { fontSize: value === "sm" ? "12px" : "16px" },
|
|
39
|
-
}),
|
|
40
|
-
},
|
|
41
|
-
}),
|
|
42
|
-
);
|
|
43
|
-
const props = component({ size: "lg" });
|
|
44
|
-
expect(getStyleClass(props)).toEqual({
|
|
45
|
-
class: cls("large"),
|
|
46
|
-
fontSize: "16px",
|
|
47
|
-
});
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test("function variant can return another component default result", () => {
|
|
51
|
-
const button = cv({
|
|
52
|
-
variants: {
|
|
53
|
-
size: {
|
|
54
|
-
sm: { class: "button-sm", style: { fontSize: "12px" } },
|
|
55
|
-
lg: { class: "button-lg", style: { fontSize: "16px" } },
|
|
56
|
-
},
|
|
57
|
-
},
|
|
58
|
-
});
|
|
59
|
-
const component = getModeComponent(
|
|
60
|
-
mode,
|
|
61
|
-
cv({
|
|
62
|
-
variants: {
|
|
63
|
-
size: (value: "sm" | "lg") => {
|
|
64
|
-
return button({ size: value });
|
|
65
|
-
},
|
|
66
|
-
},
|
|
67
|
-
}),
|
|
68
|
-
);
|
|
69
|
-
const props = component({ size: "lg" });
|
|
70
|
-
expect(getStyleClass(props)).toEqual({
|
|
71
|
-
class: cls("button-lg"),
|
|
72
|
-
fontSize: "16px",
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
test("function variant overrides extended object variant", () => {
|
|
77
|
-
const base = cv({
|
|
78
|
-
variants: {
|
|
79
|
-
size: {
|
|
80
|
-
sm: { class: "base-sm", style: { fontSize: "12px" } },
|
|
81
|
-
lg: { class: "base-lg", style: { fontSize: "16px" } },
|
|
82
|
-
},
|
|
83
|
-
},
|
|
84
|
-
});
|
|
85
|
-
const component = getModeComponent(
|
|
86
|
-
mode,
|
|
87
|
-
cv({
|
|
88
|
-
extend: [base],
|
|
89
|
-
variants: {
|
|
90
|
-
size: (value: "sm" | "lg") => ({
|
|
91
|
-
class: value === "sm" ? "extended-sm" : "extended-lg",
|
|
92
|
-
style: {
|
|
93
|
-
backgroundColor: value === "sm" ? "lightgray" : "gray",
|
|
94
|
-
},
|
|
95
|
-
}),
|
|
96
|
-
},
|
|
97
|
-
}),
|
|
98
|
-
);
|
|
99
|
-
const props = component({ size: "lg" });
|
|
100
|
-
expect(getStyleClass(props)).toEqual({
|
|
101
|
-
class: cls("extended-lg"),
|
|
102
|
-
backgroundColor: "gray",
|
|
103
|
-
});
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
test("function variant overrides extended function variant", () => {
|
|
107
|
-
const base = cv({
|
|
108
|
-
variants: {
|
|
109
|
-
size: (value: "sm" | "lg") => ({
|
|
110
|
-
class: value === "sm" ? "base-sm" : "base-lg",
|
|
111
|
-
style: { fontSize: value === "sm" ? "12px" : "16px" },
|
|
112
|
-
}),
|
|
113
|
-
},
|
|
114
|
-
});
|
|
115
|
-
const component = getModeComponent(
|
|
116
|
-
mode,
|
|
117
|
-
cv({
|
|
118
|
-
extend: [base],
|
|
119
|
-
variants: {
|
|
120
|
-
size: (value: "sm" | "lg") => ({
|
|
121
|
-
class: value === "sm" ? "extended-sm" : "extended-lg",
|
|
122
|
-
style: {
|
|
123
|
-
backgroundColor: value === "sm" ? "lightgray" : "gray",
|
|
124
|
-
},
|
|
125
|
-
}),
|
|
126
|
-
},
|
|
127
|
-
}),
|
|
128
|
-
);
|
|
129
|
-
const props = component({ size: "lg" });
|
|
130
|
-
expect(getStyleClass(props)).toEqual({
|
|
131
|
-
class: cls("extended-lg"),
|
|
132
|
-
backgroundColor: "gray",
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
test("function variant style does not accept numbers", () => {
|
|
137
|
-
const component = getModeComponent(
|
|
138
|
-
mode,
|
|
139
|
-
cv({
|
|
140
|
-
variants: {
|
|
141
|
-
// @ts-expect-error
|
|
142
|
-
size: (value: "sm" | "lg") => ({
|
|
143
|
-
class: value === "sm" ? "small" : "large",
|
|
144
|
-
style: { fontSize: value === "sm" ? 12 : 16 },
|
|
145
|
-
}),
|
|
146
|
-
},
|
|
147
|
-
}),
|
|
148
|
-
);
|
|
149
|
-
const props = component({ size: "lg" });
|
|
150
|
-
expect(getStyleClass(props)).toEqual({
|
|
151
|
-
class: cls("large"),
|
|
152
|
-
fontSize: expect.toBeOneOf(["16", "16px"]),
|
|
153
|
-
});
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
test("function variant changes extended boolean variant to string", () => {
|
|
157
|
-
const base = cv({
|
|
158
|
-
variants: { disabled: { true: "disabled", false: "enabled" } },
|
|
159
|
-
});
|
|
160
|
-
const component = getModeComponent(
|
|
161
|
-
mode,
|
|
162
|
-
cv({
|
|
163
|
-
extend: [base],
|
|
164
|
-
variants: {
|
|
165
|
-
disabled: (value: "yes" | "no" | "maybe") => {
|
|
166
|
-
if (value === "yes") return "state-disabled";
|
|
167
|
-
if (value === "no") return "state-enabled";
|
|
168
|
-
return "state-pending";
|
|
169
|
-
},
|
|
170
|
-
},
|
|
171
|
-
}),
|
|
172
|
-
);
|
|
173
|
-
component({
|
|
174
|
-
// @ts-expect-error
|
|
175
|
-
disabled: true,
|
|
176
|
-
});
|
|
177
|
-
const props = component({ disabled: "maybe" });
|
|
178
|
-
expect(getStyleClass(props)).toEqual({ class: cls("state-pending") });
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
test("function variant changes extended string variant to boolean", () => {
|
|
182
|
-
const base = cv({ variants: { size: { sm: "sm", md: "md", lg: "lg" } } });
|
|
183
|
-
const component = getModeComponent(
|
|
184
|
-
mode,
|
|
185
|
-
cv({
|
|
186
|
-
extend: [base],
|
|
187
|
-
variants: {
|
|
188
|
-
size: (value: boolean) => (value ? "size-large" : "size-small"),
|
|
189
|
-
},
|
|
190
|
-
}),
|
|
191
|
-
);
|
|
192
|
-
component({
|
|
193
|
-
// @ts-expect-error
|
|
194
|
-
size: "sm",
|
|
195
|
-
});
|
|
196
|
-
const propsTrue = component({ size: true });
|
|
197
|
-
expect(getStyleClass(propsTrue)).toEqual({ class: cls("size-large") });
|
|
198
|
-
const propsFalse = component({ size: false });
|
|
199
|
-
expect(getStyleClass(propsFalse)).toEqual({ class: cls("size-small") });
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
test("function variant with number type", () => {
|
|
203
|
-
const component = getModeComponent(
|
|
204
|
-
mode,
|
|
205
|
-
cv({
|
|
206
|
-
variants: {
|
|
207
|
-
columns: (value: number) => ({
|
|
208
|
-
class: `grid-cols-${value}`,
|
|
209
|
-
style: { "--grid-columns": `${value}` },
|
|
210
|
-
}),
|
|
211
|
-
},
|
|
212
|
-
}),
|
|
213
|
-
);
|
|
214
|
-
const props = component({ columns: 3 });
|
|
215
|
-
expect(getStyleClass(props)).toEqual({
|
|
216
|
-
class: cls("grid-cols-3"),
|
|
217
|
-
"--grid-columns": "3",
|
|
218
|
-
});
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
test("function variant with number type returns dynamic styles", () => {
|
|
222
|
-
const component = getModeComponent(
|
|
223
|
-
mode,
|
|
224
|
-
cv({
|
|
225
|
-
variants: {
|
|
226
|
-
gap: (value: number) => ({
|
|
227
|
-
style: { "--gap": `${value * 4}px` },
|
|
228
|
-
}),
|
|
229
|
-
padding: (value: number) => ({
|
|
230
|
-
style: {
|
|
231
|
-
"--padding-x": `${value}px`,
|
|
232
|
-
"--padding-y": `${value * 0.5}px`,
|
|
233
|
-
},
|
|
234
|
-
}),
|
|
235
|
-
},
|
|
236
|
-
}),
|
|
237
|
-
);
|
|
238
|
-
const props = component({ gap: 4, padding: 16 });
|
|
239
|
-
expect(getStyleClass(props)).toEqual({
|
|
240
|
-
class: "",
|
|
241
|
-
"--gap": "16px",
|
|
242
|
-
"--padding-x": "16px",
|
|
243
|
-
"--padding-y": "8px",
|
|
244
|
-
});
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
test("function variant with nullable type", () => {
|
|
248
|
-
const component = getModeComponent(
|
|
249
|
-
mode,
|
|
250
|
-
cv({
|
|
251
|
-
variants: {
|
|
252
|
-
color: (value: string | null) => ({
|
|
253
|
-
class: value ? `color-${value}` : "color-default",
|
|
254
|
-
style: { "--color": value ?? "inherit" },
|
|
255
|
-
}),
|
|
256
|
-
},
|
|
257
|
-
}),
|
|
258
|
-
);
|
|
259
|
-
const propsWithValue = component({ color: "red" });
|
|
260
|
-
expect(getStyleClass(propsWithValue)).toEqual({
|
|
261
|
-
class: cls("color-red"),
|
|
262
|
-
"--color": "red",
|
|
263
|
-
});
|
|
264
|
-
const propsWithNull = component({ color: null });
|
|
265
|
-
expect(getStyleClass(propsWithNull)).toEqual({
|
|
266
|
-
class: cls("color-default"),
|
|
267
|
-
"--color": "inherit",
|
|
268
|
-
});
|
|
269
|
-
});
|
|
270
|
-
|
|
271
|
-
test("function variant changes extended variant from string to number", () => {
|
|
272
|
-
const base = cv({
|
|
273
|
-
variants: { size: { sm: "text-sm", md: "text-md", lg: "text-lg" } },
|
|
274
|
-
});
|
|
275
|
-
const component = getModeComponent(
|
|
276
|
-
mode,
|
|
277
|
-
cv({
|
|
278
|
-
extend: [base],
|
|
279
|
-
variants: {
|
|
280
|
-
size: (value: number) => ({
|
|
281
|
-
class: "text-custom",
|
|
282
|
-
style: { fontSize: `${value}px` },
|
|
283
|
-
}),
|
|
284
|
-
},
|
|
285
|
-
}),
|
|
286
|
-
);
|
|
287
|
-
component({
|
|
288
|
-
// @ts-expect-error
|
|
289
|
-
size: "sm",
|
|
290
|
-
});
|
|
291
|
-
const props = component({ size: 18 });
|
|
292
|
-
expect(getStyleClass(props)).toEqual({
|
|
293
|
-
class: cls("text-custom"),
|
|
294
|
-
fontSize: "18px",
|
|
295
|
-
});
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
test("static and function variants combine within the same component", () => {
|
|
299
|
-
const component = getModeComponent(
|
|
300
|
-
mode,
|
|
301
|
-
cv({
|
|
302
|
-
variants: {
|
|
303
|
-
size: { sm: "size-sm", lg: "size-lg" },
|
|
304
|
-
columns: (value: number) => `cols-${value}`,
|
|
305
|
-
},
|
|
306
|
-
}),
|
|
307
|
-
);
|
|
308
|
-
const props = component({ size: "lg", columns: 3 });
|
|
309
|
-
expect(getStyleClass(props)).toEqual({
|
|
310
|
-
class: cls("size-lg cols-3"),
|
|
311
|
-
});
|
|
312
|
-
});
|
|
313
|
-
|
|
314
|
-
test("object variant in child replaces extended function variant at runtime", () => {
|
|
315
|
-
const base = cv({
|
|
316
|
-
variants: {
|
|
317
|
-
// The function is typed for `number`; if it ran with a child string,
|
|
318
|
-
// it would emit garbage like `base-fn-sm`. The merged-variant type
|
|
319
|
-
// says the child's object replaces the parent function entirely, so
|
|
320
|
-
// the runtime must skip the parent's function for this key.
|
|
321
|
-
size: (value: number) => `base-fn-${value}`,
|
|
322
|
-
},
|
|
323
|
-
});
|
|
324
|
-
const component = getModeComponent(
|
|
325
|
-
mode,
|
|
326
|
-
cv({
|
|
327
|
-
extend: [base],
|
|
328
|
-
variants: {
|
|
329
|
-
size: { sm: "child-sm", lg: "child-lg" },
|
|
330
|
-
},
|
|
331
|
-
}),
|
|
332
|
-
);
|
|
333
|
-
const propsSm = component({ size: "sm" });
|
|
334
|
-
expect(getStyleClass(propsSm)).toEqual({ class: cls("child-sm") });
|
|
335
|
-
const propsLg = component({ size: "lg" });
|
|
336
|
-
expect(getStyleClass(propsLg)).toEqual({ class: cls("child-lg") });
|
|
337
|
-
});
|
|
338
|
-
|
|
339
|
-
test("object variant in grandchild replaces grandparent function variant at runtime", () => {
|
|
340
|
-
const base = cv({
|
|
341
|
-
variants: { size: (value: number) => `base-fn-${value}` },
|
|
342
|
-
});
|
|
343
|
-
const middle = cv({ extend: [base], class: "middle" });
|
|
344
|
-
const component = getModeComponent(
|
|
345
|
-
mode,
|
|
346
|
-
cv({
|
|
347
|
-
extend: [middle],
|
|
348
|
-
variants: { size: { sm: "gc-sm", lg: "gc-lg" } },
|
|
349
|
-
}),
|
|
350
|
-
);
|
|
351
|
-
const props = component({ size: "sm" });
|
|
352
|
-
expect(getStyleClass(props)).toEqual({ class: cls("middle gc-sm") });
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
test("intermediate object variant hides grandparent function from later extends", () => {
|
|
356
|
-
const base = cv({
|
|
357
|
-
variants: { size: (value: number) => `base-fn-${value}` },
|
|
358
|
-
});
|
|
359
|
-
const middle = cv({
|
|
360
|
-
extend: [base],
|
|
361
|
-
variants: { size: { sm: "middle-sm", md: "middle-md" } },
|
|
362
|
-
});
|
|
363
|
-
const component = getModeComponent(
|
|
364
|
-
mode,
|
|
365
|
-
cv({
|
|
366
|
-
extend: [middle],
|
|
367
|
-
variants: { size: { sm: "child-sm", lg: "child-lg" } },
|
|
368
|
-
}),
|
|
369
|
-
);
|
|
370
|
-
// Middle already replaced the grandparent function with an object, so
|
|
371
|
-
// child sees only objects in the chain — both objects merge by value.
|
|
372
|
-
const props = component({ size: "sm" });
|
|
373
|
-
expect(getStyleClass(props)).toEqual({
|
|
374
|
-
class: cls("middle-sm child-sm"),
|
|
375
|
-
});
|
|
376
|
-
});
|
|
377
|
-
});
|
|
378
|
-
}
|
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
mkdirSync,
|
|
3
|
-
mkdtempSync,
|
|
4
|
-
readFileSync,
|
|
5
|
-
rmSync,
|
|
6
|
-
writeFileSync,
|
|
7
|
-
} from "node:fs";
|
|
8
|
-
import { dirname, join, resolve } from "node:path";
|
|
9
|
-
import { fileURLToPath } from "node:url";
|
|
10
|
-
import ts from "typescript";
|
|
11
|
-
import { afterEach, describe, expect, test } from "vitest";
|
|
12
|
-
|
|
13
|
-
const testDir = dirname(fileURLToPath(import.meta.url));
|
|
14
|
-
const packageDir = resolve(testDir, "..");
|
|
15
|
-
const sourceDir = resolve(packageDir, "src");
|
|
16
|
-
const workspaceDir = resolve(packageDir, "../..");
|
|
17
|
-
const tempDirs: string[] = [];
|
|
18
|
-
|
|
19
|
-
interface LanguageServiceFixture {
|
|
20
|
-
consumerFile: string;
|
|
21
|
-
consumerSource: string;
|
|
22
|
-
service: ts.LanguageService;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function createCompilerOptions(): ts.CompilerOptions {
|
|
26
|
-
return {
|
|
27
|
-
target: ts.ScriptTarget.ES2020,
|
|
28
|
-
module: ts.ModuleKind.NodeNext,
|
|
29
|
-
moduleResolution: ts.ModuleResolutionKind.NodeNext,
|
|
30
|
-
// Match the repo's Node-aware TS environment so language-service
|
|
31
|
-
// navigation in the fixture reflects real editor behavior.
|
|
32
|
-
types: ["node"],
|
|
33
|
-
allowImportingTsExtensions: true,
|
|
34
|
-
strict: true,
|
|
35
|
-
skipLibCheck: true,
|
|
36
|
-
esModuleInterop: true,
|
|
37
|
-
resolveJsonModule: true,
|
|
38
|
-
lib: ["ES2020", "DOM", "DOM.Iterable"],
|
|
39
|
-
customConditions: ["source"],
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function getFixturePosition(source: string, pattern: string): number {
|
|
44
|
-
const position = source.indexOf(pattern);
|
|
45
|
-
if (position === -1) {
|
|
46
|
-
throw new Error(`Pattern not found in fixture source: ${pattern}`);
|
|
47
|
-
}
|
|
48
|
-
return position;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function createLanguageServiceHost(
|
|
52
|
-
scriptFileNames: string[],
|
|
53
|
-
currentDirectory: string,
|
|
54
|
-
options: ts.CompilerOptions,
|
|
55
|
-
): ts.LanguageServiceHost {
|
|
56
|
-
return {
|
|
57
|
-
getCompilationSettings: () => options,
|
|
58
|
-
getScriptFileNames: () => scriptFileNames,
|
|
59
|
-
getScriptVersion: () => "0",
|
|
60
|
-
getScriptSnapshot: (fileName) => {
|
|
61
|
-
if (!ts.sys.fileExists(fileName)) return;
|
|
62
|
-
return ts.ScriptSnapshot.fromString(readFileSync(fileName, "utf8"));
|
|
63
|
-
},
|
|
64
|
-
getCurrentDirectory: () => currentDirectory,
|
|
65
|
-
getDefaultLibFileName: ts.getDefaultLibFilePath,
|
|
66
|
-
fileExists: (fileName) => ts.sys.fileExists(fileName),
|
|
67
|
-
readFile: (fileName) => ts.sys.readFile(fileName),
|
|
68
|
-
readDirectory: (...args) => ts.sys.readDirectory(...args),
|
|
69
|
-
directoryExists: (directoryName) => ts.sys.directoryExists(directoryName),
|
|
70
|
-
getDirectories: (directoryName) => ts.sys.getDirectories(directoryName),
|
|
71
|
-
resolveModuleNames: (moduleNames, containingFile) => {
|
|
72
|
-
return moduleNames.map((moduleName) => {
|
|
73
|
-
return ts.resolveModuleName(moduleName, containingFile, options, ts.sys)
|
|
74
|
-
.resolvedModule;
|
|
75
|
-
});
|
|
76
|
-
},
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function createLanguageServiceFixture(
|
|
81
|
-
consumerSource: string,
|
|
82
|
-
): LanguageServiceFixture {
|
|
83
|
-
const tempDir = mkdtempSync(join(workspaceDir, ".tmp-language-service-"));
|
|
84
|
-
tempDirs.push(tempDir);
|
|
85
|
-
|
|
86
|
-
const fixtureSourceDir = join(tempDir, "src");
|
|
87
|
-
mkdirSync(fixtureSourceDir, { recursive: true });
|
|
88
|
-
|
|
89
|
-
for (const fileName of ["index.ts", "types.ts", "utils.ts"]) {
|
|
90
|
-
writeFileSync(
|
|
91
|
-
join(fixtureSourceDir, fileName),
|
|
92
|
-
readFileSync(join(sourceDir, fileName), "utf8"),
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const consumerFile = join(tempDir, "consumer.ts");
|
|
97
|
-
writeFileSync(consumerFile, consumerSource);
|
|
98
|
-
|
|
99
|
-
const options = createCompilerOptions();
|
|
100
|
-
const scriptFileNames = [
|
|
101
|
-
consumerFile,
|
|
102
|
-
join(fixtureSourceDir, "index.ts"),
|
|
103
|
-
join(fixtureSourceDir, "types.ts"),
|
|
104
|
-
join(fixtureSourceDir, "utils.ts"),
|
|
105
|
-
];
|
|
106
|
-
|
|
107
|
-
const host = createLanguageServiceHost(scriptFileNames, tempDir, options);
|
|
108
|
-
const service = ts.createLanguageService(host);
|
|
109
|
-
|
|
110
|
-
return { consumerFile, consumerSource, service };
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function getVariantFixtureSource() {
|
|
114
|
-
return `import { cv } from "./src/index.ts";
|
|
115
|
-
|
|
116
|
-
const button = cv({
|
|
117
|
-
variants: {
|
|
118
|
-
size: { sm: "sm", lg: "lg" },
|
|
119
|
-
},
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
button({
|
|
123
|
-
size: "sm",
|
|
124
|
-
});
|
|
125
|
-
`;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
afterEach(() => {
|
|
129
|
-
while (tempDirs.length) {
|
|
130
|
-
const tempDir = tempDirs.pop();
|
|
131
|
-
if (!tempDir) continue;
|
|
132
|
-
rmSync(tempDir, { recursive: true, force: true });
|
|
133
|
-
}
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
describe("TypeScript language service", () => {
|
|
137
|
-
test("goes to the local variant definition from a variant prop usage", () => {
|
|
138
|
-
const fixture = createLanguageServiceFixture(getVariantFixtureSource());
|
|
139
|
-
const definitionStart = getFixturePosition(
|
|
140
|
-
fixture.consumerSource,
|
|
141
|
-
"size: { sm",
|
|
142
|
-
);
|
|
143
|
-
const usageStart = getFixturePosition(fixture.consumerSource, 'size: "sm"');
|
|
144
|
-
const definitions =
|
|
145
|
-
fixture.service.getDefinitionAtPosition(
|
|
146
|
-
fixture.consumerFile,
|
|
147
|
-
usageStart + 1,
|
|
148
|
-
) ?? [];
|
|
149
|
-
|
|
150
|
-
expect(definitions).toHaveLength(1);
|
|
151
|
-
expect(definitions[0]).toMatchObject({
|
|
152
|
-
fileName: fixture.consumerFile,
|
|
153
|
-
textSpan: { start: definitionStart, length: 4 },
|
|
154
|
-
});
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
test("renames variant prop usages when renaming the variant definition", () => {
|
|
158
|
-
const fixture = createLanguageServiceFixture(getVariantFixtureSource());
|
|
159
|
-
const definitionStart = getFixturePosition(
|
|
160
|
-
fixture.consumerSource,
|
|
161
|
-
"size: { sm",
|
|
162
|
-
);
|
|
163
|
-
const usageStart = getFixturePosition(fixture.consumerSource, 'size: "sm"');
|
|
164
|
-
const renameLocations =
|
|
165
|
-
fixture.service.findRenameLocations(
|
|
166
|
-
fixture.consumerFile,
|
|
167
|
-
definitionStart + 1,
|
|
168
|
-
false,
|
|
169
|
-
false,
|
|
170
|
-
{ providePrefixAndSuffixTextForRename: true },
|
|
171
|
-
) ?? [];
|
|
172
|
-
|
|
173
|
-
const renameStarts = renameLocations
|
|
174
|
-
.filter((location) => location.fileName === fixture.consumerFile)
|
|
175
|
-
.map((location) => location.textSpan.start);
|
|
176
|
-
|
|
177
|
-
expect(renameStarts).toHaveLength(2);
|
|
178
|
-
expect(renameStarts).toContain(definitionStart);
|
|
179
|
-
expect(renameStarts).toContain(usageStart);
|
|
180
|
-
});
|
|
181
|
-
});
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { afterEach, expect, test } from "vitest";
|
|
2
|
-
import { cv } from "../src/index.ts";
|
|
3
|
-
|
|
4
|
-
const proto = Object.prototype as Record<string, unknown>;
|
|
5
|
-
|
|
6
|
-
afterEach(() => {
|
|
7
|
-
for (const key of Object.keys(proto)) {
|
|
8
|
-
delete proto[key];
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
test("getVariants ignores keys inherited from Object.prototype", () => {
|
|
13
|
-
proto.size = "lg";
|
|
14
|
-
const component = cv({
|
|
15
|
-
variants: { size: { sm: "sm", lg: "lg" } },
|
|
16
|
-
defaultVariants: { size: "sm" },
|
|
17
|
-
});
|
|
18
|
-
expect(component.getVariants({})).toEqual({ size: "sm" });
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test("render ignores keys inherited from Object.prototype", () => {
|
|
22
|
-
proto.size = "lg";
|
|
23
|
-
const component = cv({
|
|
24
|
-
variants: { size: { sm: "sm", lg: "lg" } },
|
|
25
|
-
defaultVariants: { size: "sm" },
|
|
26
|
-
});
|
|
27
|
-
expect(component({}).class).toBe("sm");
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test("extend's resolver ignores polluted prototype on parent's refine", () => {
|
|
31
|
-
// Base's refine branches on its own variants.size — if the polluted "size"
|
|
32
|
-
// key leaks into resolved variants, the refine callback would see size =
|
|
33
|
-
// "lg" instead of the staticDefault "sm" and emit the lg-specific class.
|
|
34
|
-
proto.size = "lg";
|
|
35
|
-
const base = cv({
|
|
36
|
-
variants: { size: { sm: "sm", lg: "lg" } },
|
|
37
|
-
defaultVariants: { size: "sm" },
|
|
38
|
-
refine: ({ variants, addClass }) => {
|
|
39
|
-
if (variants.size === "lg") {
|
|
40
|
-
addClass("base-lg-detected");
|
|
41
|
-
}
|
|
42
|
-
},
|
|
43
|
-
});
|
|
44
|
-
const child = cv({ extend: [base], class: "child" });
|
|
45
|
-
expect(child({}).class).not.toContain("base-lg-detected");
|
|
46
|
-
});
|
package/tests/react.test.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import type { CSSProperties, ComponentProps } from "react";
|
|
2
|
-
import { expect, expectTypeOf, test } from "vitest";
|
|
3
|
-
import { type VariantProps, cv, splitProps } from "../src/index.ts";
|
|
4
|
-
import type { JSXProps } from "../src/types.ts";
|
|
5
|
-
|
|
6
|
-
test("splitProps", () => {
|
|
7
|
-
const component = cv({ variants: { size: { sm: "sm", md: "md" } } });
|
|
8
|
-
|
|
9
|
-
interface Props
|
|
10
|
-
extends ComponentProps<"div">, VariantProps<typeof component> {}
|
|
11
|
-
const props: Props = { className: "custom", size: "md", id: "my-div" };
|
|
12
|
-
|
|
13
|
-
const [variantProps, rest] = splitProps(props, component);
|
|
14
|
-
expectTypeOf(variantProps.style).toEqualTypeOf<CSSProperties | undefined>();
|
|
15
|
-
expectTypeOf(variantProps.className).toEqualTypeOf<string | undefined>();
|
|
16
|
-
expect(variantProps.className).toBe("custom");
|
|
17
|
-
expect(variantProps).toEqual({ size: "md", className: "custom" });
|
|
18
|
-
expect(
|
|
19
|
-
// @ts-expect-error rest props should not have className
|
|
20
|
-
rest.className,
|
|
21
|
-
).toBeUndefined();
|
|
22
|
-
expect(
|
|
23
|
-
// @ts-expect-error rest props should not have style
|
|
24
|
-
rest.style,
|
|
25
|
-
).toBeUndefined();
|
|
26
|
-
expect(rest).toEqual({ id: "my-div" });
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
test("component props", () => {
|
|
30
|
-
const component = cv({
|
|
31
|
-
style: { fontSize: "16px" },
|
|
32
|
-
variants: { size: { sm: "sm", md: "md" } },
|
|
33
|
-
}).jsx;
|
|
34
|
-
const props = component({ size: "sm", className: "custom" });
|
|
35
|
-
expectTypeOf(props).toEqualTypeOf<JSXProps>();
|
|
36
|
-
expect(props).toEqual({
|
|
37
|
-
className: "sm custom",
|
|
38
|
-
style: { fontSize: "16px" },
|
|
39
|
-
});
|
|
40
|
-
});
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { expect, test } from "vitest";
|
|
2
|
-
import { formatCreationStack } from "../src/refine-warning.ts";
|
|
3
|
-
|
|
4
|
-
test("formatCreationStack returns the first app frame", () => {
|
|
5
|
-
const stack = [
|
|
6
|
-
"Error",
|
|
7
|
-
" at captureCreationFrame (/repo/packages/clava/src/refine-warning.ts:31:10)",
|
|
8
|
-
" at cv (/repo/packages/clava/src/index.ts:734:9)",
|
|
9
|
-
" at eval (/repo/app/src/button.ts:12:21)",
|
|
10
|
-
" at ModuleJob.run (node:internal/modules/esm/module_job:343:25)",
|
|
11
|
-
].join("\n");
|
|
12
|
-
|
|
13
|
-
expect(formatCreationStack({ stack })).toBe(
|
|
14
|
-
" at eval (/repo/app/src/button.ts:12:21)",
|
|
15
|
-
);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
test("formatCreationStack omits dependency and runtime frames", () => {
|
|
19
|
-
const stack = [
|
|
20
|
-
"Error",
|
|
21
|
-
" at captureCreationFrame (/repo/packages/clava/src/refine-warning.ts:31:10)",
|
|
22
|
-
" at cv(/repo/packages/clava/src/index.ts:734:9)",
|
|
23
|
-
" at eval (/repo/node_modules/package/index.js:1:1)",
|
|
24
|
-
" at ModuleJob.run (node:internal/modules/esm/module_job:343:25)",
|
|
25
|
-
].join("\n");
|
|
26
|
-
|
|
27
|
-
expect(formatCreationStack({ stack })).toBeUndefined();
|
|
28
|
-
});
|