@react-spectrum/codemods 0.1.0

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.
@@ -0,0 +1,199 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright 2024 Adobe. All rights reserved.
4
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License. You may obtain a copy
6
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
7
+ *
8
+ * Unless required by applicable law or agreed to in writing, software distributed under
9
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
10
+ * OF ANY KIND, either express or implied. See the License for the specific language
11
+ * governing permissions and limitations under the License.
12
+ */
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.convertDimension = convertDimension;
15
+ exports.convertGridTrack = convertGridTrack;
16
+ exports.convertUnsafeDimension = convertUnsafeDimension;
17
+ const dimensions = {
18
+ 'size-0': 0,
19
+ 'size-10': 1,
20
+ 'size-25': 2,
21
+ 'size-40': 3,
22
+ 'size-50': 4,
23
+ 'size-65': 5,
24
+ 'size-75': 6,
25
+ 'size-85': 7,
26
+ 'size-100': 8,
27
+ 'size-115': 9,
28
+ 'size-125': 10,
29
+ 'size-130': 11,
30
+ 'size-150': 12,
31
+ 'size-160': 13,
32
+ 'size-175': 14,
33
+ 'size-200': 16,
34
+ 'size-225': 18,
35
+ 'size-250': 20,
36
+ 'size-275': 22,
37
+ 'size-300': 24,
38
+ 'size-325': 26,
39
+ 'size-350': 28,
40
+ 'size-400': 32,
41
+ 'size-450': 36,
42
+ 'size-500': 40,
43
+ 'size-550': 44,
44
+ 'size-600': 48,
45
+ 'size-675': 54,
46
+ 'size-700': 56,
47
+ 'size-800': 64,
48
+ 'size-900': 72,
49
+ 'size-1000': 80,
50
+ 'size-1200': 96,
51
+ 'size-1250': 100,
52
+ 'size-1600': 128,
53
+ 'size-1700': 136,
54
+ 'size-2000': 160,
55
+ 'size-2400': 192,
56
+ 'size-3000': 240,
57
+ 'size-3400': 272,
58
+ 'size-3600': 288,
59
+ 'size-4600': 368,
60
+ 'size-5000': 400,
61
+ 'size-6000': 480,
62
+ 'static-size-0': 0,
63
+ 'static-size-10': 1,
64
+ 'static-size-25': 2,
65
+ 'static-size-50': 4,
66
+ 'static-size-40': 3,
67
+ 'static-size-65': 5,
68
+ 'static-size-100': 8,
69
+ 'static-size-115': 9,
70
+ 'static-size-125': 10,
71
+ 'static-size-130': 11,
72
+ 'static-size-150': 12,
73
+ 'static-size-160': 13,
74
+ 'static-size-175': 14,
75
+ 'static-size-200': 16,
76
+ 'static-size-225': 18,
77
+ 'static-size-250': 20,
78
+ 'static-size-300': 24,
79
+ 'static-size-400': 32,
80
+ 'static-size-450': 36,
81
+ 'static-size-500': 40,
82
+ 'static-size-550': 44,
83
+ 'static-size-600': 48,
84
+ 'static-size-700': 56,
85
+ 'static-size-800': 64,
86
+ 'static-size-900': 72,
87
+ 'static-size-1000': 80,
88
+ 'static-size-1200': 96,
89
+ 'static-size-1700': 136,
90
+ 'static-size-2400': 192,
91
+ 'static-size-2600': 208,
92
+ 'static-size-3400': 272,
93
+ 'static-size-3600': 288,
94
+ 'static-size-4600': 368,
95
+ 'static-size-5000': 400,
96
+ 'static-size-6000': 480,
97
+ 'single-line-height': 32, // size-400
98
+ 'single-line-width': 192 // size-2400
99
+ };
100
+ const spacingValues = [
101
+ 0,
102
+ 4,
103
+ 8,
104
+ 12,
105
+ 16,
106
+ 20,
107
+ 24,
108
+ 28,
109
+ 32,
110
+ 36,
111
+ 40,
112
+ 44,
113
+ 48,
114
+ 56,
115
+ 64,
116
+ 80,
117
+ 96,
118
+ 112,
119
+ 128,
120
+ 144,
121
+ 160,
122
+ 176,
123
+ 192,
124
+ 208,
125
+ 224,
126
+ 240,
127
+ 256,
128
+ 288,
129
+ 320,
130
+ 384
131
+ ];
132
+ const sizing = {
133
+ 'auto': 'auto',
134
+ '100vh': 'screen',
135
+ 'min-content': 'min',
136
+ 'max-content': 'max',
137
+ 'fit-content': 'fit'
138
+ };
139
+ const UNIT_RE = /(%|px|em|rem|vw|vh|auto|cm|mm|in|pt|pc|ex|ch|rem|vmin|vmax|fr)$/;
140
+ const FUNC_RE = /^\s*\w+\(/;
141
+ // const SPECTRUM_VARIABLE_RE = /(static-)?size-\d+|single-line-(height|width)/g;
142
+ const SIZING_RE = /auto|100vh|min-content|max-content|fit-content/;
143
+ function convertDimension(value, toPixels = false) {
144
+ let pixelValue;
145
+ if (typeof value === 'number') {
146
+ pixelValue = value;
147
+ }
148
+ else if (UNIT_RE.test(value)) {
149
+ if (value.endsWith('px')) {
150
+ pixelValue = parseFloat(value);
151
+ }
152
+ else if (value === '100%') {
153
+ return 'full';
154
+ }
155
+ else {
156
+ return `[${value}]`;
157
+ }
158
+ }
159
+ else if (FUNC_RE.test(value)) {
160
+ // return value.replace(SPECTRUM_VARIABLE_RE, 'var(--spectrum-global-dimension-$&, var(--spectrum-alias-$&))');
161
+ // TODO: handle calc
162
+ return null;
163
+ }
164
+ else if (SIZING_RE.test(value)) {
165
+ return sizing[value];
166
+ }
167
+ else {
168
+ pixelValue = dimensions[value];
169
+ }
170
+ if (pixelValue == null) {
171
+ throw new Error('invalid dimension: ' + value);
172
+ }
173
+ if (toPixels) {
174
+ return `${pixelValue}px`;
175
+ }
176
+ if (spacingValues.includes(pixelValue)) {
177
+ return pixelValue;
178
+ }
179
+ // TODO: Convert to rems? Find nearest value?
180
+ return `[${pixelValue}px]`;
181
+ }
182
+ function convertGridTrack(value, toPixels = false) {
183
+ if (typeof value === 'string' && /^max-content|min-content|minmax|auto|fit-content|repeat|\d+fr/.test(value)) {
184
+ return value;
185
+ }
186
+ else {
187
+ return convertDimension(value, toPixels);
188
+ }
189
+ }
190
+ function convertUnsafeDimension(value) {
191
+ if (typeof value === 'number') {
192
+ return convertDimension(value);
193
+ }
194
+ let m = value.match(/^var\(--spectrum-global-dimension-(static-)?size-(.*)\)$/);
195
+ if (m) {
196
+ return convertDimension(`${m[1] || ''}size-${m[2]}`);
197
+ }
198
+ return null;
199
+ }