scss-variable-extractor 1.6.4 → 1.6.6

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.
@@ -1,353 +1,385 @@
1
- const { refactorFile } = require('../src/refactorer');
2
-
3
- describe('Refactorer Edge Cases', () => {
4
- test('should handle rgba() color functions without partial replacements', () => {
5
- const scss = `
6
- .component {
7
- background: rgba(255, 100, 50, 0.5);
8
- color: rgba(100, 100, 100, 1);
9
- }
10
- `;
11
-
12
- const analysis = {
13
- colors: [
14
- { value: 'rgba(255, 100, 50, 0.5)', suggestedName: '$color-primary-light' }
15
- ],
16
- spacing: [
17
- { value: '100px', suggestedName: '$spacing-xxl' }
18
- ],
19
- fontSizes: [],
20
- fontWeights: [],
21
- fontFamilies: [],
22
- borderRadius: [],
23
- shadows: [],
24
- zIndex: [],
25
- sizing: [],
26
- lineHeight: [],
27
- opacity: [
28
- { value: '0.5', suggestedName: '$opacity-half' }
29
- ],
30
- transitions: []
31
- };
32
-
33
- const result = refactorFile(scss, analysis, '/libs/styles/_variables.scss', '/app/test.scss', {});
34
-
35
- // Should replace complete rgba function
36
- expect(result.content).toContain('background: $color-primary-light');
37
-
38
- // Should NOT do partial replacements inside rgba()
39
- expect(result.content).toContain('rgba(100, 100, 100, 1)'); // Not rgba($spacing-xxl, $spacing-xxl, $spacing-xxl, 1)
40
- expect(result.content).not.toContain('rgba($spacing-xxl');
41
- expect(result.content).not.toContain('rgba(255, $spacing-xxl');
42
- });
43
-
44
- test('should handle calc() expressions without partial replacements', () => {
45
- const scss = `
46
- .component {
47
- width: calc(100% - 16px);
48
- height: calc(50vh + 8px);
49
- margin: calc(16px / 2);
50
- }
51
- `;
52
-
53
- const analysis = {
54
- colors: [],
55
- spacing: [
56
- { value: '16px', suggestedName: '$spacing-md' },
57
- { value: '8px', suggestedName: '$spacing-sm' }
58
- ],
59
- fontSizes: [],
60
- fontWeights: [],
61
- fontFamilies: [],
62
- borderRadius: [],
63
- shadows: [],
64
- zIndex: [],
65
- sizing: [],
66
- lineHeight: [],
67
- opacity: [],
68
- transitions: []
69
- };
70
-
71
- const result = refactorFile(scss, analysis, '/libs/styles/_variables.scss', '/app/test.scss', {});
72
-
73
- // calc() values should be protected from replacement (safe zone)
74
- const content = result.content;
75
-
76
- // Values inside calc() should NOT be replaced
77
- expect(content).toContain('calc(100% - 16px)');
78
- expect(content).toContain('calc(50vh + 8px)');
79
- expect(content).toContain('calc(16px / 2)');
80
- });
81
-
82
- test('should handle negative values correctly', () => {
83
- const scss = `
84
- .component {
85
- margin: -16px;
86
- top: -8px;
87
- z-index: -1;
88
- }
89
- `;
90
-
91
- const analysis = {
92
- colors: [],
93
- spacing: [
94
- { value: '16px', suggestedName: '$spacing-md' },
95
- { value: '8px', suggestedName: '$spacing-sm' }
96
- ],
97
- fontSizes: [],
98
- fontWeights: [],
99
- fontFamilies: [],
100
- borderRadius: [],
101
- shadows: [],
102
- zIndex: [
103
- { value: '1', suggestedName: '$z-index-1' }
104
- ],
105
- sizing: [],
106
- lineHeight: [],
107
- opacity: [],
108
- transitions: []
109
- };
110
-
111
- const result = refactorFile(scss, analysis, '/libs/styles/_variables.scss', '/app/test.scss', {});
112
-
113
- // Negative values should be protected (safe zone) to prevent -$variable syntax
114
- expect(result.content).toContain('margin: -16px');
115
- expect(result.content).toContain('top: -8px');
116
- expect(result.content).toContain('z-index: -1');
117
- });
118
-
119
- test('should handle multiple values in shorthand properties', () => {
120
- const scss = `
121
- .component {
122
- margin: 16px 8px;
123
- padding: 16px 8px 16px 8px;
124
- border-radius: 4px 2px;
125
- }
126
- `;
127
-
128
- const analysis = {
129
- colors: [],
130
- spacing: [
131
- { value: '16px', suggestedName: '$spacing-md' },
132
- { value: '8px', suggestedName: '$spacing-sm' }
133
- ],
134
- fontSizes: [],
135
- fontWeights: [],
136
- fontFamilies: [],
137
- borderRadius: [
138
- { value: '4px', suggestedName: '$border-radius-sm' },
139
- { value: '2px', suggestedName: '$border-radius-xs' }
140
- ],
141
- shadows: [],
142
- zIndex: [],
143
- sizing: [],
144
- lineHeight: [],
145
- opacity: [],
146
- transitions: []
147
- };
148
-
149
- const result = refactorFile(scss, analysis, '/libs/styles/_variables.scss', '/app/test.scss', {});
150
-
151
- // Should replace individual values in shorthand
152
- expect(result.content).toContain('$spacing-md');
153
- expect(result.content).toContain('$spacing-sm');
154
- expect(result.content).toContain('$border-radius-sm');
155
- expect(result.content).toContain('$border-radius-xs');
156
- });
157
-
158
- test('should not replace values in CSS custom properties', () => {
159
- const scss = `
160
- :root {
161
- --primary-color: #1976d2;
162
- --spacing: 16px;
163
- }
164
- .component {
165
- color: var(--primary-color);
166
- padding: var(--spacing);
167
- }
168
- `;
169
-
170
- const analysis = {
171
- colors: [
172
- { value: '#1976d2', suggestedName: '$color-primary' }
173
- ],
174
- spacing: [
175
- { value: '16px', suggestedName: '$spacing-md' }
176
- ],
177
- fontSizes: [],
178
- fontWeights: [],
179
- fontFamilies: [],
180
- borderRadius: [],
181
- shadows: [],
182
- zIndex: [],
183
- sizing: [],
184
- lineHeight: [],
185
- opacity: [],
186
- transitions: []
187
- };
188
-
189
- const result = refactorFile(scss, analysis, '/libs/styles/_variables.scss', '/app/test.scss', {});
190
-
191
- // CSS custom property declarations should be replaced (they're like SCSS variables)
192
- // But var() references should be preserved
193
- expect(result.content).toContain('var(--primary-color)');
194
- expect(result.content).toContain('var(--spacing)');
195
- });
196
-
197
- test('should not replace values in attribute selectors', () => {
198
- const scss = `
199
- [data-size="16"] {
200
- padding: 16px;
201
- }
202
- [data-color="#1976d2"] {
203
- color: #1976d2;
204
- }
205
- `;
206
-
207
- const analysis = {
208
- colors: [
209
- { value: '#1976d2', suggestedName: '$color-primary' }
210
- ],
211
- spacing: [
212
- { value: '16px', suggestedName: '$spacing-md' }
213
- ],
214
- fontSizes: [],
215
- fontWeights: [],
216
- fontFamilies: [],
217
- borderRadius: [],
218
- shadows: [],
219
- zIndex: [],
220
- sizing: [],
221
- lineHeight: [],
222
- opacity: [],
223
- transitions: []
224
- };
225
-
226
- const result = refactorFile(scss, analysis, '/libs/styles/_variables.scss', '/app/test.scss', {});
227
-
228
- // Should replace in property values (outside attribute selectors)
229
- expect(result.content).toContain('padding: $spacing-md');
230
- expect(result.content).toContain('color: $color-primary');
231
-
232
- // Should NOT replace in attribute selector values (safe zone)
233
- expect(result.content).toContain('[data-size="16"]');
234
- expect(result.content).toContain('[data-color="#1976d2"]');
235
- });
236
-
237
- test('should handle transform functions', () => {
238
- const scss = `
239
- .component {
240
- transform: translate(10px, 20px);
241
- transform: rotate(45deg);
242
- transform: scale(2);
243
- }
244
- `;
245
-
246
- const analysis = {
247
- colors: [],
248
- spacing: [
249
- { value: '10px', suggestedName: '$spacing-xs' },
250
- { value: '20px', suggestedName: '$spacing-sm' }
251
- ],
252
- fontSizes: [],
253
- fontWeights: [],
254
- fontFamilies: [],
255
- borderRadius: [],
256
- shadows: [],
257
- zIndex: [
258
- { value: '2', suggestedName: '$z-index-2' }
259
- ],
260
- sizing: [],
261
- lineHeight: [],
262
- opacity: [],
263
- transitions: []
264
- };
265
-
266
- const result = refactorFile(scss, analysis, '/libs/styles/_variables.scss', '/app/test.scss', {});
267
-
268
- // Transform function arguments should be protected (safe zone)
269
- const content = result.content;
270
- expect(content).toContain('translate(10px, 20px)');
271
- expect(content).toContain('scale(2)');
272
- expect(content).toContain('rotate(45deg)');
273
- });
274
-
275
- test('should handle keyframe percentages', () => {
276
- const scss = `
277
- @keyframes fade {
278
- 0% { opacity: 0; }
279
- 50% { opacity: 0.5; }
280
- 100% { opacity: 1; }
281
- }
282
- `;
283
-
284
- const analysis = {
285
- colors: [],
286
- spacing: [],
287
- fontSizes: [],
288
- fontWeights: [],
289
- fontFamilies: [],
290
- borderRadius: [],
291
- shadows: [],
292
- zIndex: [],
293
- sizing: [],
294
- lineHeight: [],
295
- opacity: [
296
- { value: '0', suggestedName: '$opacity-transparent' },
297
- { value: '0.5', suggestedName: '$opacity-half' },
298
- { value: '1', suggestedName: '$opacity-full' }
299
- ],
300
- transitions: []
301
- };
302
-
303
- const result = refactorFile(scss, analysis, '/libs/styles/_variables.scss', '/app/test.scss', {});
304
-
305
- // Keyframe selectors (0%, 50%, 100%) should not be replaced
306
- expect(result.content).toContain('0%');
307
- expect(result.content).toContain('50%');
308
- expect(result.content).toContain('100%');
309
-
310
- // But opacity values should be replaced
311
- expect(result.content).toContain('opacity: $opacity-transparent');
312
- expect(result.content).toContain('opacity: $opacity-half');
313
- expect(result.content).toContain('opacity: $opacity-full');
314
- });
315
-
316
- test('should handle comments containing values', () => {
317
- const scss = `
318
- /* Primary color is #1976d2 */
319
- .component {
320
- color: #1976d2; // Use #1976d2 for brand
321
- padding: 16px; /* 16px is our standard spacing */
322
- }
323
- `;
324
-
325
- const analysis = {
326
- colors: [
327
- { value: '#1976d2', suggestedName: '$color-primary' }
328
- ],
329
- spacing: [
330
- { value: '16px', suggestedName: '$spacing-md' }
331
- ],
332
- fontSizes: [],
333
- fontWeights: [],
334
- fontFamilies: [],
335
- borderRadius: [],
336
- shadows: [],
337
- zIndex: [],
338
- sizing: [],
339
- lineHeight: [],
340
- opacity: [],
341
- transitions: []
342
- };
343
-
344
- const result = refactorFile(scss, analysis, '/libs/styles/_variables.scss', '/app/test.scss', {});
345
-
346
- // Should replace in actual property values
347
- expect(result.content).toContain('color: $color-primary');
348
- expect(result.content).toContain('padding: $spacing-md');
349
-
350
- // Comments will likely be replaced too (current behavior)
351
- // This might be acceptable or we could add comment detection to safe zones
352
- });
353
- });
1
+ const { refactorFile } = require('../src/refactorer');
2
+
3
+ describe('Refactorer Edge Cases', () => {
4
+ test('should handle rgba() color functions without partial replacements', () => {
5
+ const scss = `
6
+ .component {
7
+ background: rgba(255, 100, 50, 0.5);
8
+ color: rgba(100, 100, 100, 1);
9
+ }
10
+ `;
11
+
12
+ const analysis = {
13
+ colors: [{ value: 'rgba(255, 100, 50, 0.5)', suggestedName: '$color-primary-light' }],
14
+ spacing: [{ value: '100px', suggestedName: '$spacing-xxl' }],
15
+ fontSizes: [],
16
+ fontWeights: [],
17
+ fontFamilies: [],
18
+ borderRadius: [],
19
+ shadows: [],
20
+ zIndex: [],
21
+ sizing: [],
22
+ lineHeight: [],
23
+ opacity: [{ value: '0.5', suggestedName: '$opacity-half' }],
24
+ transitions: [],
25
+ };
26
+
27
+ const result = refactorFile(
28
+ scss,
29
+ analysis,
30
+ '/libs/styles/_variables.scss',
31
+ '/app/test.scss',
32
+ {}
33
+ );
34
+
35
+ // Should replace complete rgba function
36
+ expect(result.content).toContain('background: $color-primary-light');
37
+
38
+ // Should NOT do partial replacements inside rgba()
39
+ expect(result.content).toContain('rgba(100, 100, 100, 1)'); // Not rgba($spacing-xxl, $spacing-xxl, $spacing-xxl, 1)
40
+ expect(result.content).not.toContain('rgba($spacing-xxl');
41
+ expect(result.content).not.toContain('rgba(255, $spacing-xxl');
42
+ });
43
+
44
+ test('should handle calc() expressions without partial replacements', () => {
45
+ const scss = `
46
+ .component {
47
+ width: calc(100% - 16px);
48
+ height: calc(50vh + 8px);
49
+ margin: calc(16px / 2);
50
+ }
51
+ `;
52
+
53
+ const analysis = {
54
+ colors: [],
55
+ spacing: [
56
+ { value: '16px', suggestedName: '$spacing-md' },
57
+ { value: '8px', suggestedName: '$spacing-sm' },
58
+ ],
59
+ fontSizes: [],
60
+ fontWeights: [],
61
+ fontFamilies: [],
62
+ borderRadius: [],
63
+ shadows: [],
64
+ zIndex: [],
65
+ sizing: [],
66
+ lineHeight: [],
67
+ opacity: [],
68
+ transitions: [],
69
+ };
70
+
71
+ const result = refactorFile(
72
+ scss,
73
+ analysis,
74
+ '/libs/styles/_variables.scss',
75
+ '/app/test.scss',
76
+ {}
77
+ );
78
+
79
+ // calc() values should be protected from replacement (safe zone)
80
+ const content = result.content;
81
+
82
+ // Values inside calc() should NOT be replaced
83
+ expect(content).toContain('calc(100% - 16px)');
84
+ expect(content).toContain('calc(50vh + 8px)');
85
+ expect(content).toContain('calc(16px / 2)');
86
+ });
87
+
88
+ test('should handle negative values correctly', () => {
89
+ const scss = `
90
+ .component {
91
+ margin: -16px;
92
+ top: -8px;
93
+ z-index: -1;
94
+ }
95
+ `;
96
+
97
+ const analysis = {
98
+ colors: [],
99
+ spacing: [
100
+ { value: '16px', suggestedName: '$spacing-md' },
101
+ { value: '8px', suggestedName: '$spacing-sm' },
102
+ ],
103
+ fontSizes: [],
104
+ fontWeights: [],
105
+ fontFamilies: [],
106
+ borderRadius: [],
107
+ shadows: [],
108
+ zIndex: [{ value: '1', suggestedName: '$z-index-1' }],
109
+ sizing: [],
110
+ lineHeight: [],
111
+ opacity: [],
112
+ transitions: [],
113
+ };
114
+
115
+ const result = refactorFile(
116
+ scss,
117
+ analysis,
118
+ '/libs/styles/_variables.scss',
119
+ '/app/test.scss',
120
+ {}
121
+ );
122
+
123
+ // Negative values should be protected (safe zone) to prevent -$variable syntax
124
+ expect(result.content).toContain('margin: -16px');
125
+ expect(result.content).toContain('top: -8px');
126
+ expect(result.content).toContain('z-index: -1');
127
+ });
128
+
129
+ test('should handle multiple values in shorthand properties', () => {
130
+ const scss = `
131
+ .component {
132
+ margin: 16px 8px;
133
+ padding: 16px 8px 16px 8px;
134
+ border-radius: 4px 2px;
135
+ }
136
+ `;
137
+
138
+ const analysis = {
139
+ colors: [],
140
+ spacing: [
141
+ { value: '16px', suggestedName: '$spacing-md' },
142
+ { value: '8px', suggestedName: '$spacing-sm' },
143
+ ],
144
+ fontSizes: [],
145
+ fontWeights: [],
146
+ fontFamilies: [],
147
+ borderRadius: [
148
+ { value: '4px', suggestedName: '$border-radius-sm' },
149
+ { value: '2px', suggestedName: '$border-radius-xs' },
150
+ ],
151
+ shadows: [],
152
+ zIndex: [],
153
+ sizing: [],
154
+ lineHeight: [],
155
+ opacity: [],
156
+ transitions: [],
157
+ };
158
+
159
+ const result = refactorFile(
160
+ scss,
161
+ analysis,
162
+ '/libs/styles/_variables.scss',
163
+ '/app/test.scss',
164
+ {}
165
+ );
166
+
167
+ // Should replace individual values in shorthand
168
+ expect(result.content).toContain('$spacing-md');
169
+ expect(result.content).toContain('$spacing-sm');
170
+ expect(result.content).toContain('$border-radius-sm');
171
+ expect(result.content).toContain('$border-radius-xs');
172
+ });
173
+
174
+ test('should not replace values in CSS custom properties', () => {
175
+ const scss = `
176
+ :root {
177
+ --primary-color: #1976d2;
178
+ --spacing: 16px;
179
+ }
180
+ .component {
181
+ color: var(--primary-color);
182
+ padding: var(--spacing);
183
+ }
184
+ `;
185
+
186
+ const analysis = {
187
+ colors: [{ value: '#1976d2', suggestedName: '$color-primary' }],
188
+ spacing: [{ value: '16px', suggestedName: '$spacing-md' }],
189
+ fontSizes: [],
190
+ fontWeights: [],
191
+ fontFamilies: [],
192
+ borderRadius: [],
193
+ shadows: [],
194
+ zIndex: [],
195
+ sizing: [],
196
+ lineHeight: [],
197
+ opacity: [],
198
+ transitions: [],
199
+ };
200
+
201
+ const result = refactorFile(
202
+ scss,
203
+ analysis,
204
+ '/libs/styles/_variables.scss',
205
+ '/app/test.scss',
206
+ {}
207
+ );
208
+
209
+ // CSS custom property declarations should be replaced (they're like SCSS variables)
210
+ // But var() references should be preserved
211
+ expect(result.content).toContain('var(--primary-color)');
212
+ expect(result.content).toContain('var(--spacing)');
213
+ });
214
+
215
+ test('should not replace values in attribute selectors', () => {
216
+ const scss = `
217
+ [data-size="16"] {
218
+ padding: 16px;
219
+ }
220
+ [data-color="#1976d2"] {
221
+ color: #1976d2;
222
+ }
223
+ `;
224
+
225
+ const analysis = {
226
+ colors: [{ value: '#1976d2', suggestedName: '$color-primary' }],
227
+ spacing: [{ value: '16px', suggestedName: '$spacing-md' }],
228
+ fontSizes: [],
229
+ fontWeights: [],
230
+ fontFamilies: [],
231
+ borderRadius: [],
232
+ shadows: [],
233
+ zIndex: [],
234
+ sizing: [],
235
+ lineHeight: [],
236
+ opacity: [],
237
+ transitions: [],
238
+ };
239
+
240
+ const result = refactorFile(
241
+ scss,
242
+ analysis,
243
+ '/libs/styles/_variables.scss',
244
+ '/app/test.scss',
245
+ {}
246
+ );
247
+
248
+ // Should replace in property values (outside attribute selectors)
249
+ expect(result.content).toContain('padding: $spacing-md');
250
+ expect(result.content).toContain('color: $color-primary');
251
+
252
+ // Should NOT replace in attribute selector values (safe zone)
253
+ expect(result.content).toContain('[data-size="16"]');
254
+ expect(result.content).toContain('[data-color="#1976d2"]');
255
+ });
256
+
257
+ test('should handle transform functions', () => {
258
+ const scss = `
259
+ .component {
260
+ transform: translate(10px, 20px);
261
+ transform: rotate(45deg);
262
+ transform: scale(2);
263
+ }
264
+ `;
265
+
266
+ const analysis = {
267
+ colors: [],
268
+ spacing: [
269
+ { value: '10px', suggestedName: '$spacing-xs' },
270
+ { value: '20px', suggestedName: '$spacing-sm' },
271
+ ],
272
+ fontSizes: [],
273
+ fontWeights: [],
274
+ fontFamilies: [],
275
+ borderRadius: [],
276
+ shadows: [],
277
+ zIndex: [{ value: '2', suggestedName: '$z-index-2' }],
278
+ sizing: [],
279
+ lineHeight: [],
280
+ opacity: [],
281
+ transitions: [],
282
+ };
283
+
284
+ const result = refactorFile(
285
+ scss,
286
+ analysis,
287
+ '/libs/styles/_variables.scss',
288
+ '/app/test.scss',
289
+ {}
290
+ );
291
+
292
+ // Transform function arguments should be protected (safe zone)
293
+ const content = result.content;
294
+ expect(content).toContain('translate(10px, 20px)');
295
+ expect(content).toContain('scale(2)');
296
+ expect(content).toContain('rotate(45deg)');
297
+ });
298
+
299
+ test('should handle keyframe percentages', () => {
300
+ const scss = `
301
+ @keyframes fade {
302
+ 0% { opacity: 0; }
303
+ 50% { opacity: 0.5; }
304
+ 100% { opacity: 1; }
305
+ }
306
+ `;
307
+
308
+ const analysis = {
309
+ colors: [],
310
+ spacing: [],
311
+ fontSizes: [],
312
+ fontWeights: [],
313
+ fontFamilies: [],
314
+ borderRadius: [],
315
+ shadows: [],
316
+ zIndex: [],
317
+ sizing: [],
318
+ lineHeight: [],
319
+ opacity: [
320
+ { value: '0', suggestedName: '$opacity-transparent' },
321
+ { value: '0.5', suggestedName: '$opacity-half' },
322
+ { value: '1', suggestedName: '$opacity-full' },
323
+ ],
324
+ transitions: [],
325
+ };
326
+
327
+ const result = refactorFile(
328
+ scss,
329
+ analysis,
330
+ '/libs/styles/_variables.scss',
331
+ '/app/test.scss',
332
+ {}
333
+ );
334
+
335
+ // Keyframe selectors (0%, 50%, 100%) should not be replaced
336
+ expect(result.content).toContain('0%');
337
+ expect(result.content).toContain('50%');
338
+ expect(result.content).toContain('100%');
339
+
340
+ // But opacity values should be replaced
341
+ expect(result.content).toContain('opacity: $opacity-transparent');
342
+ expect(result.content).toContain('opacity: $opacity-half');
343
+ expect(result.content).toContain('opacity: $opacity-full');
344
+ });
345
+
346
+ test('should handle comments containing values', () => {
347
+ const scss = `
348
+ /* Primary color is #1976d2 */
349
+ .component {
350
+ color: #1976d2; // Use #1976d2 for brand
351
+ padding: 16px; /* 16px is our standard spacing */
352
+ }
353
+ `;
354
+
355
+ const analysis = {
356
+ colors: [{ value: '#1976d2', suggestedName: '$color-primary' }],
357
+ spacing: [{ value: '16px', suggestedName: '$spacing-md' }],
358
+ fontSizes: [],
359
+ fontWeights: [],
360
+ fontFamilies: [],
361
+ borderRadius: [],
362
+ shadows: [],
363
+ zIndex: [],
364
+ sizing: [],
365
+ lineHeight: [],
366
+ opacity: [],
367
+ transitions: [],
368
+ };
369
+
370
+ const result = refactorFile(
371
+ scss,
372
+ analysis,
373
+ '/libs/styles/_variables.scss',
374
+ '/app/test.scss',
375
+ {}
376
+ );
377
+
378
+ // Should replace in actual property values
379
+ expect(result.content).toContain('color: $color-primary');
380
+ expect(result.content).toContain('padding: $spacing-md');
381
+
382
+ // Comments will likely be replaced too (current behavior)
383
+ // This might be acceptable or we could add comment detection to safe zones
384
+ });
385
+ });