@mgcrea/react-native-tailwind 0.7.0 → 0.8.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/README.md +2 -1
- package/dist/babel/index.cjs +334 -196
- package/dist/babel/index.d.ts +4 -40
- package/dist/babel/index.test.ts +214 -1
- package/dist/babel/index.ts +4 -1169
- package/dist/babel/plugin.d.ts +42 -0
- package/{src/babel/index.test.ts → dist/babel/plugin.test.ts} +216 -2
- package/dist/babel/plugin.ts +491 -0
- package/dist/babel/utils/attributeMatchers.d.ts +23 -0
- package/dist/babel/utils/attributeMatchers.test.ts +294 -0
- package/dist/babel/utils/attributeMatchers.ts +71 -0
- package/dist/babel/utils/componentSupport.d.ts +18 -0
- package/dist/babel/utils/componentSupport.test.ts +426 -0
- package/dist/babel/utils/componentSupport.ts +68 -0
- package/dist/babel/utils/dynamicProcessing.d.ts +32 -0
- package/dist/babel/utils/dynamicProcessing.ts +223 -0
- package/dist/babel/utils/modifierProcessing.d.ts +26 -0
- package/dist/babel/utils/modifierProcessing.ts +118 -0
- package/dist/babel/utils/styleInjection.d.ts +15 -0
- package/dist/babel/utils/styleInjection.ts +80 -0
- package/dist/babel/utils/styleTransforms.d.ts +39 -0
- package/dist/babel/utils/styleTransforms.test.ts +349 -0
- package/dist/babel/utils/styleTransforms.ts +258 -0
- package/dist/babel/utils/twProcessing.d.ts +28 -0
- package/dist/babel/utils/twProcessing.ts +124 -0
- package/dist/components/TextInput.d.ts +171 -14
- package/dist/config/tailwind.d.ts +302 -0
- package/dist/config/tailwind.js +1 -0
- package/dist/index.d.ts +5 -4
- package/dist/index.js +1 -1
- package/dist/parser/colors.js +1 -1
- package/dist/parser/colors.test.js +1 -1
- package/dist/parser/index.d.ts +1 -0
- package/dist/parser/index.js +1 -1
- package/dist/parser/modifiers.d.ts +2 -2
- package/dist/parser/modifiers.js +1 -1
- package/dist/parser/placeholder.d.ts +36 -0
- package/dist/parser/placeholder.js +1 -0
- package/dist/parser/placeholder.test.js +1 -0
- package/dist/parser/typography.d.ts +1 -0
- package/dist/parser/typography.js +1 -1
- package/dist/parser/typography.test.js +1 -1
- package/dist/runtime.cjs +1 -1
- package/dist/runtime.cjs.map +4 -4
- package/dist/runtime.d.ts +1 -14
- package/dist/runtime.js +1 -1
- package/dist/runtime.js.map +4 -4
- package/dist/stubs/tw.d.ts +1 -14
- package/dist/types/core.d.ts +40 -0
- package/dist/types/core.js +0 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.js +1 -0
- package/dist/types/runtime.d.ts +15 -0
- package/dist/types/runtime.js +1 -0
- package/dist/types/util.d.ts +3 -0
- package/dist/types/util.js +0 -0
- package/dist/utils/flattenColors.d.ts +1 -0
- package/dist/utils/flattenColors.js +1 -1
- package/dist/utils/flattenColors.test.js +1 -1
- package/package.json +1 -1
- package/src/babel/index.ts +4 -1169
- package/src/babel/plugin.test.ts +482 -0
- package/src/babel/plugin.ts +491 -0
- package/src/babel/utils/attributeMatchers.test.ts +294 -0
- package/src/babel/utils/attributeMatchers.ts +71 -0
- package/src/babel/utils/componentSupport.test.ts +426 -0
- package/src/babel/utils/componentSupport.ts +68 -0
- package/src/babel/utils/dynamicProcessing.ts +223 -0
- package/src/babel/utils/modifierProcessing.ts +118 -0
- package/src/babel/utils/styleInjection.ts +80 -0
- package/src/babel/utils/styleTransforms.test.ts +349 -0
- package/src/babel/utils/styleTransforms.ts +258 -0
- package/src/babel/utils/twProcessing.ts +124 -0
- package/src/components/TextInput.tsx +17 -14
- package/src/config/{palettes.ts → tailwind.ts} +2 -2
- package/src/index.ts +6 -3
- package/src/parser/colors.test.ts +32 -0
- package/src/parser/colors.ts +2 -2
- package/src/parser/index.ts +2 -1
- package/src/parser/modifiers.ts +10 -4
- package/src/parser/placeholder.test.ts +105 -0
- package/src/parser/placeholder.ts +78 -0
- package/src/parser/typography.test.ts +11 -0
- package/src/parser/typography.ts +20 -2
- package/src/runtime.ts +1 -16
- package/src/stubs/tw.ts +1 -16
- package/src/{types.ts → types/core.ts} +0 -4
- package/src/types/index.ts +2 -0
- package/src/types/runtime.ts +17 -0
- package/src/types/util.ts +1 -0
- package/src/utils/flattenColors.test.ts +100 -0
- package/src/utils/flattenColors.ts +3 -1
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
import { parseSync } from "@babel/core";
|
|
2
|
+
import * as t from "@babel/types";
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
import { getComponentModifierSupport, getStatePropertyForModifier } from "./componentSupport";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Helper to create a JSXOpeningElement from JSX code
|
|
8
|
+
*/
|
|
9
|
+
function createJSXElement(code: string): t.JSXOpeningElement {
|
|
10
|
+
const ast = parseSync(code, {
|
|
11
|
+
sourceType: "module",
|
|
12
|
+
plugins: [["@babel/plugin-syntax-jsx", {}]],
|
|
13
|
+
filename: "test.tsx",
|
|
14
|
+
configFile: false,
|
|
15
|
+
babelrc: false,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
if (!ast) {
|
|
19
|
+
throw new Error(`Failed to parse: ${code}`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Find the JSXOpeningElement in the AST
|
|
23
|
+
let element: t.JSXOpeningElement | null = null;
|
|
24
|
+
|
|
25
|
+
const traverse = (node: t.Node) => {
|
|
26
|
+
if (t.isJSXOpeningElement(node)) {
|
|
27
|
+
element = node;
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
for (const key in node) {
|
|
31
|
+
// @ts-expect-error - Dynamic key access
|
|
32
|
+
if (node[key] && typeof node[key] === "object") {
|
|
33
|
+
// @ts-expect-error - Dynamic key access
|
|
34
|
+
traverse(node[key] as t.Node);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
traverse(ast);
|
|
40
|
+
|
|
41
|
+
if (!element) {
|
|
42
|
+
throw new Error(`Could not find JSXOpeningElement in: ${code}`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return element;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
describe("getComponentModifierSupport", () => {
|
|
49
|
+
describe("Supported components", () => {
|
|
50
|
+
it("should recognize Pressable component", () => {
|
|
51
|
+
const element = createJSXElement("<Pressable />");
|
|
52
|
+
const result = getComponentModifierSupport(element, t);
|
|
53
|
+
|
|
54
|
+
expect(result).toEqual({
|
|
55
|
+
component: "Pressable",
|
|
56
|
+
supportedModifiers: ["active", "hover", "focus", "disabled"],
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("should recognize TextInput component", () => {
|
|
61
|
+
const element = createJSXElement("<TextInput />");
|
|
62
|
+
const result = getComponentModifierSupport(element, t);
|
|
63
|
+
|
|
64
|
+
expect(result).toEqual({
|
|
65
|
+
component: "TextInput",
|
|
66
|
+
supportedModifiers: ["focus", "disabled", "placeholder"],
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("should recognize Pressable with attributes", () => {
|
|
71
|
+
const element = createJSXElement('<Pressable className="m-4" onPress={handlePress} />');
|
|
72
|
+
const result = getComponentModifierSupport(element, t);
|
|
73
|
+
|
|
74
|
+
expect(result).toEqual({
|
|
75
|
+
component: "Pressable",
|
|
76
|
+
supportedModifiers: ["active", "hover", "focus", "disabled"],
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("should recognize TextInput with attributes", () => {
|
|
81
|
+
const element = createJSXElement('<TextInput className="border" placeholder="Email" />');
|
|
82
|
+
const result = getComponentModifierSupport(element, t);
|
|
83
|
+
|
|
84
|
+
expect(result).toEqual({
|
|
85
|
+
component: "TextInput",
|
|
86
|
+
supportedModifiers: ["focus", "disabled", "placeholder"],
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe("Member expressions", () => {
|
|
92
|
+
it("should recognize ReactNative.Pressable", () => {
|
|
93
|
+
const element = createJSXElement("<ReactNative.Pressable />");
|
|
94
|
+
const result = getComponentModifierSupport(element, t);
|
|
95
|
+
|
|
96
|
+
expect(result).toEqual({
|
|
97
|
+
component: "Pressable",
|
|
98
|
+
supportedModifiers: ["active", "hover", "focus", "disabled"],
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("should recognize RN.TextInput", () => {
|
|
103
|
+
const element = createJSXElement("<RN.TextInput />");
|
|
104
|
+
const result = getComponentModifierSupport(element, t);
|
|
105
|
+
|
|
106
|
+
expect(result).toEqual({
|
|
107
|
+
component: "TextInput",
|
|
108
|
+
supportedModifiers: ["focus", "disabled", "placeholder"],
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("should recognize nested member expressions", () => {
|
|
113
|
+
const element = createJSXElement("<Components.Input.TextInput />");
|
|
114
|
+
const result = getComponentModifierSupport(element, t);
|
|
115
|
+
|
|
116
|
+
// Should extract "TextInput" from the rightmost property
|
|
117
|
+
expect(result).toEqual({
|
|
118
|
+
component: "TextInput",
|
|
119
|
+
supportedModifiers: ["focus", "disabled", "placeholder"],
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("should recognize Pressable in namespaced imports", () => {
|
|
124
|
+
const element = createJSXElement("<UI.Pressable />");
|
|
125
|
+
const result = getComponentModifierSupport(element, t);
|
|
126
|
+
|
|
127
|
+
expect(result).toEqual({
|
|
128
|
+
component: "Pressable",
|
|
129
|
+
supportedModifiers: ["active", "hover", "focus", "disabled"],
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe("Unsupported components", () => {
|
|
135
|
+
it("should return null for View", () => {
|
|
136
|
+
const element = createJSXElement("<View />");
|
|
137
|
+
const result = getComponentModifierSupport(element, t);
|
|
138
|
+
|
|
139
|
+
expect(result).toBeNull();
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("should return null for TouchableOpacity", () => {
|
|
143
|
+
const element = createJSXElement("<TouchableOpacity />");
|
|
144
|
+
const result = getComponentModifierSupport(element, t);
|
|
145
|
+
|
|
146
|
+
expect(result).toBeNull();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("should return null for custom components", () => {
|
|
150
|
+
const element = createJSXElement("<CustomButton />");
|
|
151
|
+
const result = getComponentModifierSupport(element, t);
|
|
152
|
+
|
|
153
|
+
expect(result).toBeNull();
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("should return null for Text", () => {
|
|
157
|
+
const element = createJSXElement("<Text />");
|
|
158
|
+
const result = getComponentModifierSupport(element, t);
|
|
159
|
+
|
|
160
|
+
expect(result).toBeNull();
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("should return null for Image", () => {
|
|
164
|
+
const element = createJSXElement("<Image />");
|
|
165
|
+
const result = getComponentModifierSupport(element, t);
|
|
166
|
+
|
|
167
|
+
expect(result).toBeNull();
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
describe("Edge cases", () => {
|
|
172
|
+
it("should be case-sensitive", () => {
|
|
173
|
+
// lowercase "pressable" should not match
|
|
174
|
+
const element = createJSXElement("<pressable />");
|
|
175
|
+
const result = getComponentModifierSupport(element, t);
|
|
176
|
+
|
|
177
|
+
expect(result).toBeNull();
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("should not match similar names", () => {
|
|
181
|
+
const element1 = createJSXElement("<PressableButton />");
|
|
182
|
+
const result1 = getComponentModifierSupport(element1, t);
|
|
183
|
+
expect(result1).toBeNull();
|
|
184
|
+
|
|
185
|
+
const element2 = createJSXElement("<MyPressable />");
|
|
186
|
+
const result2 = getComponentModifierSupport(element2, t);
|
|
187
|
+
expect(result2).toBeNull();
|
|
188
|
+
|
|
189
|
+
const element3 = createJSXElement("<TextInputField />");
|
|
190
|
+
const result3 = getComponentModifierSupport(element3, t);
|
|
191
|
+
expect(result3).toBeNull();
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("should handle self-closing tags", () => {
|
|
195
|
+
const element = createJSXElement("<Pressable />");
|
|
196
|
+
const result = getComponentModifierSupport(element, t);
|
|
197
|
+
|
|
198
|
+
expect(result).not.toBeNull();
|
|
199
|
+
expect(result?.component).toBe("Pressable");
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("should return null for non-JSXOpeningElement nodes", () => {
|
|
203
|
+
// Test with a random node type
|
|
204
|
+
const identifier = t.identifier("foo");
|
|
205
|
+
const result = getComponentModifierSupport(identifier, t);
|
|
206
|
+
|
|
207
|
+
expect(result).toBeNull();
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("should return null for JSXFragment", () => {
|
|
211
|
+
// JSXFragment doesn't have a JSXOpeningElement, so create a mock fragment
|
|
212
|
+
const fragment = {
|
|
213
|
+
type: "JSXFragment",
|
|
214
|
+
openingFragment: {},
|
|
215
|
+
closingFragment: {},
|
|
216
|
+
children: [],
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
const result = getComponentModifierSupport(fragment as t.Node, t);
|
|
220
|
+
expect(result).toBeNull();
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
describe("Modifier support differences", () => {
|
|
225
|
+
it("should show Pressable supports active modifier but TextInput does not", () => {
|
|
226
|
+
const pressable = createJSXElement("<Pressable />");
|
|
227
|
+
const textInput = createJSXElement("<TextInput />");
|
|
228
|
+
|
|
229
|
+
const pressableResult = getComponentModifierSupport(pressable, t);
|
|
230
|
+
const textInputResult = getComponentModifierSupport(textInput, t);
|
|
231
|
+
|
|
232
|
+
expect(pressableResult?.supportedModifiers).toContain("active");
|
|
233
|
+
expect(textInputResult?.supportedModifiers).not.toContain("active");
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it("should show both support focus modifier", () => {
|
|
237
|
+
const pressable = createJSXElement("<Pressable />");
|
|
238
|
+
const textInput = createJSXElement("<TextInput />");
|
|
239
|
+
|
|
240
|
+
const pressableResult = getComponentModifierSupport(pressable, t);
|
|
241
|
+
const textInputResult = getComponentModifierSupport(textInput, t);
|
|
242
|
+
|
|
243
|
+
expect(pressableResult?.supportedModifiers).toContain("focus");
|
|
244
|
+
expect(textInputResult?.supportedModifiers).toContain("focus");
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it("should show TextInput supports placeholder but Pressable does not", () => {
|
|
248
|
+
const pressable = createJSXElement("<Pressable />");
|
|
249
|
+
const textInput = createJSXElement("<TextInput />");
|
|
250
|
+
|
|
251
|
+
const pressableResult = getComponentModifierSupport(pressable, t);
|
|
252
|
+
const textInputResult = getComponentModifierSupport(textInput, t);
|
|
253
|
+
|
|
254
|
+
expect(pressableResult?.supportedModifiers).not.toContain("placeholder");
|
|
255
|
+
expect(textInputResult?.supportedModifiers).toContain("placeholder");
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
it("should show both support disabled modifier", () => {
|
|
259
|
+
const pressable = createJSXElement("<Pressable />");
|
|
260
|
+
const textInput = createJSXElement("<TextInput />");
|
|
261
|
+
|
|
262
|
+
const pressableResult = getComponentModifierSupport(pressable, t);
|
|
263
|
+
const textInputResult = getComponentModifierSupport(textInput, t);
|
|
264
|
+
|
|
265
|
+
expect(pressableResult?.supportedModifiers).toContain("disabled");
|
|
266
|
+
expect(textInputResult?.supportedModifiers).toContain("disabled");
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
describe("getStatePropertyForModifier", () => {
|
|
272
|
+
it("should map active to pressed", () => {
|
|
273
|
+
expect(getStatePropertyForModifier("active")).toBe("pressed");
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it("should map hover to hovered", () => {
|
|
277
|
+
expect(getStatePropertyForModifier("hover")).toBe("hovered");
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it("should map focus to focused", () => {
|
|
281
|
+
expect(getStatePropertyForModifier("focus")).toBe("focused");
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
it("should map disabled to disabled", () => {
|
|
285
|
+
expect(getStatePropertyForModifier("disabled")).toBe("disabled");
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it("should return pressed as fallback for unknown modifiers", () => {
|
|
289
|
+
// @ts-expect-error - Testing fallback with invalid modifier
|
|
290
|
+
expect(getStatePropertyForModifier("unknown")).toBe("pressed");
|
|
291
|
+
|
|
292
|
+
// @ts-expect-error - Testing fallback with invalid modifier
|
|
293
|
+
expect(getStatePropertyForModifier("invalid")).toBe("pressed");
|
|
294
|
+
|
|
295
|
+
// @ts-expect-error - Testing fallback with invalid modifier
|
|
296
|
+
expect(getStatePropertyForModifier("")).toBe("pressed");
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it("should handle all Pressable modifier states", () => {
|
|
300
|
+
// Pressable supports: active, hover, focus, disabled
|
|
301
|
+
const pressableModifiers: Array<"active" | "hover" | "focus" | "disabled"> = [
|
|
302
|
+
"active",
|
|
303
|
+
"hover",
|
|
304
|
+
"focus",
|
|
305
|
+
"disabled",
|
|
306
|
+
];
|
|
307
|
+
|
|
308
|
+
const expectedMapping = {
|
|
309
|
+
active: "pressed",
|
|
310
|
+
hover: "hovered",
|
|
311
|
+
focus: "focused",
|
|
312
|
+
disabled: "disabled",
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
for (const modifier of pressableModifiers) {
|
|
316
|
+
expect(getStatePropertyForModifier(modifier)).toBe(expectedMapping[modifier]);
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
it("should handle all TextInput modifier states", () => {
|
|
321
|
+
// TextInput supports: focus, disabled, placeholder
|
|
322
|
+
// Note: placeholder doesn't have a state property (it's a prop, not state)
|
|
323
|
+
const textInputModifiers: Array<"focus" | "disabled"> = ["focus", "disabled"];
|
|
324
|
+
|
|
325
|
+
const expectedMapping = {
|
|
326
|
+
focus: "focused",
|
|
327
|
+
disabled: "disabled",
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
for (const modifier of textInputModifiers) {
|
|
331
|
+
expect(getStatePropertyForModifier(modifier)).toBe(expectedMapping[modifier]);
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
describe("Integration - Real-world scenarios", () => {
|
|
337
|
+
it("should correctly identify modifiers for a Pressable button", () => {
|
|
338
|
+
const element = createJSXElement(
|
|
339
|
+
'<Pressable className="active:bg-blue-700 hover:bg-blue-600 disabled:bg-gray-300" />',
|
|
340
|
+
);
|
|
341
|
+
const result = getComponentModifierSupport(element, t);
|
|
342
|
+
|
|
343
|
+
expect(result).not.toBeNull();
|
|
344
|
+
expect(result?.component).toBe("Pressable");
|
|
345
|
+
|
|
346
|
+
// Verify all used modifiers are supported
|
|
347
|
+
expect(result?.supportedModifiers).toContain("active");
|
|
348
|
+
expect(result?.supportedModifiers).toContain("hover");
|
|
349
|
+
expect(result?.supportedModifiers).toContain("disabled");
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
it("should correctly identify modifiers for a TextInput field", () => {
|
|
353
|
+
const element = createJSXElement(
|
|
354
|
+
'<TextInput className="focus:border-blue-500 disabled:bg-gray-100 placeholder:text-gray-400" />',
|
|
355
|
+
);
|
|
356
|
+
const result = getComponentModifierSupport(element, t);
|
|
357
|
+
|
|
358
|
+
expect(result).not.toBeNull();
|
|
359
|
+
expect(result?.component).toBe("TextInput");
|
|
360
|
+
|
|
361
|
+
// Verify all used modifiers are supported
|
|
362
|
+
expect(result?.supportedModifiers).toContain("focus");
|
|
363
|
+
expect(result?.supportedModifiers).toContain("disabled");
|
|
364
|
+
expect(result?.supportedModifiers).toContain("placeholder");
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it("should handle namespaced components from imports", () => {
|
|
368
|
+
const element = createJSXElement('<RN.Pressable className="active:opacity-80" />');
|
|
369
|
+
const result = getComponentModifierSupport(element, t);
|
|
370
|
+
|
|
371
|
+
expect(result).not.toBeNull();
|
|
372
|
+
expect(result?.component).toBe("Pressable");
|
|
373
|
+
expect(result?.supportedModifiers).toContain("active");
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
it("should return null for unsupported components with modifiers", () => {
|
|
377
|
+
const element = createJSXElement('<View className="hover:bg-blue-500" />');
|
|
378
|
+
const result = getComponentModifierSupport(element, t);
|
|
379
|
+
|
|
380
|
+
// View doesn't support modifiers
|
|
381
|
+
expect(result).toBeNull();
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
it("should map all Pressable modifiers to correct state properties", () => {
|
|
385
|
+
const element = createJSXElement("<Pressable />");
|
|
386
|
+
const result = getComponentModifierSupport(element, t);
|
|
387
|
+
|
|
388
|
+
expect(result).not.toBeNull();
|
|
389
|
+
|
|
390
|
+
// Test each supported modifier maps correctly
|
|
391
|
+
const modifiers = result?.supportedModifiers as Array<"active" | "hover" | "focus" | "disabled">;
|
|
392
|
+
for (const modifier of modifiers) {
|
|
393
|
+
const stateProp = getStatePropertyForModifier(modifier);
|
|
394
|
+
expect(stateProp).toBeTruthy();
|
|
395
|
+
expect(typeof stateProp).toBe("string");
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// Verify the mappings
|
|
399
|
+
expect(getStatePropertyForModifier("active")).toBe("pressed");
|
|
400
|
+
expect(getStatePropertyForModifier("hover")).toBe("hovered");
|
|
401
|
+
expect(getStatePropertyForModifier("focus")).toBe("focused");
|
|
402
|
+
expect(getStatePropertyForModifier("disabled")).toBe("disabled");
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
it("should map all TextInput modifiers to correct state properties", () => {
|
|
406
|
+
const element = createJSXElement("<TextInput />");
|
|
407
|
+
const result = getComponentModifierSupport(element, t);
|
|
408
|
+
|
|
409
|
+
expect(result).not.toBeNull();
|
|
410
|
+
|
|
411
|
+
// Filter out placeholder as it doesn't have a state property
|
|
412
|
+
const stateModifiers = result?.supportedModifiers.filter((m) => m !== "placeholder") as Array<
|
|
413
|
+
"focus" | "disabled"
|
|
414
|
+
>;
|
|
415
|
+
|
|
416
|
+
for (const modifier of stateModifiers) {
|
|
417
|
+
const stateProp = getStatePropertyForModifier(modifier);
|
|
418
|
+
expect(stateProp).toBeTruthy();
|
|
419
|
+
expect(typeof stateProp).toBe("string");
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// Verify the mappings
|
|
423
|
+
expect(getStatePropertyForModifier("focus")).toBe("focused");
|
|
424
|
+
expect(getStatePropertyForModifier("disabled")).toBe("disabled");
|
|
425
|
+
});
|
|
426
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for determining component modifier support
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type * as BabelTypes from "@babel/types";
|
|
6
|
+
import type { ModifierType } from "../../parser/index.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Check if a JSX element supports modifiers and determine which modifiers are supported
|
|
10
|
+
* Returns an object with component info and supported modifiers
|
|
11
|
+
*/
|
|
12
|
+
export function getComponentModifierSupport(
|
|
13
|
+
jsxElement: BabelTypes.Node,
|
|
14
|
+
t: typeof BabelTypes,
|
|
15
|
+
): { component: string; supportedModifiers: ModifierType[] } | null {
|
|
16
|
+
if (!t.isJSXOpeningElement(jsxElement)) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const name = jsxElement.name;
|
|
21
|
+
let componentName: string | null = null;
|
|
22
|
+
|
|
23
|
+
// Handle simple identifier: <Pressable>
|
|
24
|
+
if (t.isJSXIdentifier(name)) {
|
|
25
|
+
componentName = name.name;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Handle member expression: <ReactNative.Pressable>
|
|
29
|
+
if (t.isJSXMemberExpression(name)) {
|
|
30
|
+
const property = name.property;
|
|
31
|
+
if (t.isJSXIdentifier(property)) {
|
|
32
|
+
componentName = property.name;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!componentName) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Map components to their supported modifiers
|
|
41
|
+
switch (componentName) {
|
|
42
|
+
case "Pressable":
|
|
43
|
+
return { component: "Pressable", supportedModifiers: ["active", "hover", "focus", "disabled"] };
|
|
44
|
+
case "TextInput":
|
|
45
|
+
return { component: "TextInput", supportedModifiers: ["focus", "disabled", "placeholder"] };
|
|
46
|
+
default:
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Get the state property name for a modifier type
|
|
53
|
+
* Maps modifier types to component state parameter properties
|
|
54
|
+
*/
|
|
55
|
+
export function getStatePropertyForModifier(modifier: ModifierType): string {
|
|
56
|
+
switch (modifier) {
|
|
57
|
+
case "active":
|
|
58
|
+
return "pressed";
|
|
59
|
+
case "hover":
|
|
60
|
+
return "hovered";
|
|
61
|
+
case "focus":
|
|
62
|
+
return "focused";
|
|
63
|
+
case "disabled":
|
|
64
|
+
return "disabled";
|
|
65
|
+
default:
|
|
66
|
+
return "pressed"; // fallback
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for processing dynamic className expressions
|
|
3
|
+
*/
|
|
4
|
+
import type * as BabelTypes from "@babel/types";
|
|
5
|
+
import type { StyleObject } from "../../types/core.js";
|
|
6
|
+
/**
|
|
7
|
+
* Plugin state interface (subset needed for dynamic processing)
|
|
8
|
+
*/
|
|
9
|
+
export interface DynamicProcessingState {
|
|
10
|
+
styleRegistry: Map<string, StyleObject>;
|
|
11
|
+
customColors: Record<string, string>;
|
|
12
|
+
stylesIdentifier: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Result of processing a dynamic expression
|
|
16
|
+
*/
|
|
17
|
+
export type DynamicExpressionResult = {
|
|
18
|
+
expression: BabelTypes.Expression;
|
|
19
|
+
staticParts?: string[];
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Process a dynamic className expression
|
|
23
|
+
* Extracts static strings and transforms the expression to use pre-compiled styles
|
|
24
|
+
*/
|
|
25
|
+
export declare function processDynamicExpression(expression: BabelTypes.Expression, state: DynamicProcessingState, parseClassName: (className: string, customColors: Record<string, string>) => StyleObject, generateStyleKey: (className: string) => string, t: typeof BabelTypes): {
|
|
26
|
+
expression: BabelTypes.ArrayExpression | BabelTypes.MemberExpression;
|
|
27
|
+
staticParts: string[] | undefined;
|
|
28
|
+
} | {
|
|
29
|
+
expression: BabelTypes.ConditionalExpression;
|
|
30
|
+
} | {
|
|
31
|
+
expression: BabelTypes.LogicalExpression;
|
|
32
|
+
} | null;
|