@gomeniucivan/ui 1.0.59 → 1.0.60
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/package.json +1 -1
- package/scripts/stylePathTransform.js +229 -426
package/package.json
CHANGED
|
@@ -1,426 +1,229 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Functions whose calls get a `{ path }` option injected.
|
|
5
|
-
*/
|
|
6
|
-
const TARGET_FNS = ['createStaticStyles', 'createStyles'
|
|
7
|
-
const CALL_RE = new RegExp(`(?:${TARGET_FNS.join('|')})\\s*\\(`, 'g');
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
continue;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (ch === '
|
|
111
|
-
|
|
112
|
-
continue;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (ch === '
|
|
116
|
-
|
|
117
|
-
continue;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
if (ch === '
|
|
121
|
-
|
|
122
|
-
continue;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
if (ch === '
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
// Line comment inside expression
|
|
231
|
-
if (c === '/' && i + 1 < source.length && source[i + 1] === '/') {
|
|
232
|
-
i += 2;
|
|
233
|
-
while (i < source.length && source[i] !== '\n') i++;
|
|
234
|
-
continue;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// Block comment inside expression
|
|
238
|
-
if (c === '/' && i + 1 < source.length && source[i + 1] === '*') {
|
|
239
|
-
i += 2;
|
|
240
|
-
while (
|
|
241
|
-
i < source.length &&
|
|
242
|
-
!(source[i] === '*' && i + 1 < source.length && source[i + 1] === '/')
|
|
243
|
-
) {
|
|
244
|
-
i++;
|
|
245
|
-
}
|
|
246
|
-
if (i < source.length) i += 2; // skip */
|
|
247
|
-
continue;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
i++;
|
|
251
|
-
}
|
|
252
|
-
continue;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
i++;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
return -1; // unclosed template literal
|
|
259
|
-
};
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* Check whether a position in the source is inside a comment or string.
|
|
263
|
-
* Simple heuristic: checks the line text before the match.
|
|
264
|
-
*/
|
|
265
|
-
const isInCommentOrString = (source, index) => {
|
|
266
|
-
const lineStart = source.lastIndexOf('\n', index) + 1;
|
|
267
|
-
const lineText = source.slice(lineStart, index).trimStart();
|
|
268
|
-
return lineText.startsWith('*') || lineText.startsWith('//');
|
|
269
|
-
};
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* Wrap standalone `css` tagged template literals with __inspCss__().
|
|
273
|
-
*
|
|
274
|
-
* Transforms: css`color: red;`
|
|
275
|
-
* Into: __inspCss__(css`color: red;`, 'src/File/path')
|
|
276
|
-
*
|
|
277
|
-
* The wrapper attaches __inspPath to SerializedStyles objects so that
|
|
278
|
-
* createCSS.ts can register the className → path in the source registry.
|
|
279
|
-
*/
|
|
280
|
-
const wrapCssTaggedTemplates = (source, filePath) => {
|
|
281
|
-
CSS_TAG_RE.lastIndex = 0;
|
|
282
|
-
if (!CSS_TAG_RE.test(source)) return source;
|
|
283
|
-
CSS_TAG_RE.lastIndex = 0;
|
|
284
|
-
|
|
285
|
-
const escapedPath = escapePathValue(filePath);
|
|
286
|
-
|
|
287
|
-
// Collect all match positions
|
|
288
|
-
const positions = [];
|
|
289
|
-
let match;
|
|
290
|
-
while ((match = CSS_TAG_RE.exec(source)) !== null) {
|
|
291
|
-
if (isInCommentOrString(source, match.index)) continue;
|
|
292
|
-
|
|
293
|
-
// cssStart = start of "css", backtickIdx = position of the backtick
|
|
294
|
-
const cssStart = match.index;
|
|
295
|
-
const backtickIdx = cssStart + match[0].length - 1;
|
|
296
|
-
positions.push({ cssStart, backtickIdx });
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
if (positions.length === 0) return source;
|
|
300
|
-
|
|
301
|
-
// Process from right to left so indices remain valid
|
|
302
|
-
let result = source;
|
|
303
|
-
let needsHelper = false;
|
|
304
|
-
|
|
305
|
-
for (let pi = positions.length - 1; pi >= 0; pi--) {
|
|
306
|
-
const { cssStart, backtickIdx } = positions[pi];
|
|
307
|
-
const afterBacktick = backtickIdx + 1;
|
|
308
|
-
const endBacktick = findTemplateLiteralEnd(result, afterBacktick);
|
|
309
|
-
if (endBacktick === -1) continue;
|
|
310
|
-
|
|
311
|
-
// Extract the full css`...` expression
|
|
312
|
-
const beforeCss = result.slice(0, cssStart);
|
|
313
|
-
const cssExpr = result.slice(cssStart, endBacktick + 1);
|
|
314
|
-
const afterCss = result.slice(endBacktick + 1);
|
|
315
|
-
|
|
316
|
-
result = beforeCss + `__inspCss__(${cssExpr}, '${escapedPath}')` + afterCss;
|
|
317
|
-
needsHelper = true;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
if (needsHelper) {
|
|
321
|
-
result = INSP_CSS_HELPER + result;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
return result;
|
|
325
|
-
};
|
|
326
|
-
|
|
327
|
-
/**
|
|
328
|
-
* Inject `{ path: '<file-path>' }` into createStyles/createStaticStyles/createStylish calls.
|
|
329
|
-
*
|
|
330
|
-
* Rules:
|
|
331
|
-
* - One arg call: append second arg `{ path }`
|
|
332
|
-
* - Two+ arg call:
|
|
333
|
-
* - if second arg is object literal and has no `path`, inject into object
|
|
334
|
-
* - otherwise append third arg `{ path }` as a fallback
|
|
335
|
-
*
|
|
336
|
-
* Also wraps standalone `css` tagged templates with __inspCss__() for
|
|
337
|
-
* path tracking on SerializedStyles objects.
|
|
338
|
-
*/
|
|
339
|
-
const injectStylePath = (source, filePath, resourcePath = '') => {
|
|
340
|
-
const normalizedResourcePath = resourcePath.replace(/\\/g, '/');
|
|
341
|
-
|
|
342
|
-
// Skip the style factory implementation files themselves.
|
|
343
|
-
if (
|
|
344
|
-
(normalizedResourcePath.includes('createStaticStyles') ||
|
|
345
|
-
normalizedResourcePath.includes('createStyles') ||
|
|
346
|
-
normalizedResourcePath.includes('createStyish') ||
|
|
347
|
-
normalizedResourcePath.includes('createGlobalStyle')) &&
|
|
348
|
-
(normalizedResourcePath.endsWith('index.ts') || normalizedResourcePath.endsWith('.ts') || normalizedResourcePath.endsWith('.tsx'))
|
|
349
|
-
) {
|
|
350
|
-
// Still check if we should skip: only skip files inside the antd-style factories
|
|
351
|
-
if (normalizedResourcePath.includes('antd-style/factories/')) {
|
|
352
|
-
return source;
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
let result = source;
|
|
357
|
-
|
|
358
|
-
// 1. Inject { path } into createStaticStyles/createStyles/createStylish calls
|
|
359
|
-
CALL_RE.lastIndex = 0;
|
|
360
|
-
if (CALL_RE.test(result)) {
|
|
361
|
-
CALL_RE.lastIndex = 0;
|
|
362
|
-
|
|
363
|
-
const positions = [];
|
|
364
|
-
let match;
|
|
365
|
-
while ((match = CALL_RE.exec(result)) !== null) {
|
|
366
|
-
if (isInCommentOrString(result, match.index)) continue;
|
|
367
|
-
positions.push(match.index + match[0].length);
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
if (positions.length > 0) {
|
|
371
|
-
const escapedPath = escapePathValue(filePath);
|
|
372
|
-
|
|
373
|
-
for (let pi = positions.length - 1; pi >= 0; pi--) {
|
|
374
|
-
const afterParen = positions[pi];
|
|
375
|
-
const callInfo = findCallBoundaries(result, afterParen);
|
|
376
|
-
if (!callInfo) continue;
|
|
377
|
-
|
|
378
|
-
const { closeIdx, topLevelCommas } = callInfo;
|
|
379
|
-
|
|
380
|
-
// Single-arg call: add options as second arg.
|
|
381
|
-
if (topLevelCommas.length === 0) {
|
|
382
|
-
const inner = result.slice(afterParen, closeIdx);
|
|
383
|
-
const hasTrailingComma = /,\s*$/.test(inner);
|
|
384
|
-
result =
|
|
385
|
-
result.slice(0, closeIdx) +
|
|
386
|
-
(hasTrailingComma ? ` { path: '${escapedPath}' }` : `, { path: '${escapedPath}' }`) +
|
|
387
|
-
result.slice(closeIdx);
|
|
388
|
-
continue;
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
// Two+ args: patch second arg when it's an object literal.
|
|
392
|
-
const secondStart = topLevelCommas[0] + 1;
|
|
393
|
-
const secondEnd = topLevelCommas[1] ?? closeIdx;
|
|
394
|
-
const secondArgRaw = result.slice(secondStart, secondEnd);
|
|
395
|
-
|
|
396
|
-
if (hasPathProperty(secondArgRaw)) continue;
|
|
397
|
-
|
|
398
|
-
const injectedSecondArg = injectPathIntoObjectLiteral(secondArgRaw, filePath);
|
|
399
|
-
if (injectedSecondArg !== null) {
|
|
400
|
-
result =
|
|
401
|
-
result.slice(0, secondStart) +
|
|
402
|
-
injectedSecondArg +
|
|
403
|
-
result.slice(secondEnd);
|
|
404
|
-
continue;
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
// Fallback for non-object second args.
|
|
408
|
-
const inner = result.slice(afterParen, closeIdx);
|
|
409
|
-
const hasTrailingComma = /,\s*$/.test(inner);
|
|
410
|
-
result =
|
|
411
|
-
result.slice(0, closeIdx) +
|
|
412
|
-
(hasTrailingComma ? ` { path: '${escapedPath}' }` : `, { path: '${escapedPath}' }`) +
|
|
413
|
-
result.slice(closeIdx);
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
// 2. Wrap standalone css tagged templates with __inspCss__()
|
|
419
|
-
result = wrapCssTaggedTemplates(result, filePath);
|
|
420
|
-
|
|
421
|
-
return result;
|
|
422
|
-
};
|
|
423
|
-
|
|
424
|
-
module.exports = {
|
|
425
|
-
injectStylePath,
|
|
426
|
-
};
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Functions whose calls get a `{ path }` option injected.
|
|
5
|
+
*/
|
|
6
|
+
const TARGET_FNS = ['createStaticStyles', 'createStyles'];
|
|
7
|
+
const CALL_RE = new RegExp(`(?:${TARGET_FNS.join('|')})\\s*\\(`, 'g');
|
|
8
|
+
|
|
9
|
+
const hasPathProperty = (text) => /\bpath\s*:/.test(text);
|
|
10
|
+
|
|
11
|
+
const escapePathValue = (value) => value.replace(/\\/g, '/').replace(/'/g, "\\'");
|
|
12
|
+
|
|
13
|
+
const injectPathIntoObjectLiteral = (objectText, pathValue) => {
|
|
14
|
+
const trimmed = objectText.trim();
|
|
15
|
+
|
|
16
|
+
if (!trimmed.startsWith('{') || !trimmed.endsWith('}')) return null;
|
|
17
|
+
if (hasPathProperty(trimmed)) return objectText;
|
|
18
|
+
|
|
19
|
+
const body = trimmed.slice(1, -1).trim();
|
|
20
|
+
const injected =
|
|
21
|
+
body.length === 0
|
|
22
|
+
? `{ path: '${escapePathValue(pathValue)}' }`
|
|
23
|
+
: `{ path: '${escapePathValue(pathValue)}', ${body} }`;
|
|
24
|
+
|
|
25
|
+
const leading = objectText.match(/^\s*/)?.[0] ?? '';
|
|
26
|
+
const trailing = objectText.match(/\s*$/)?.[0] ?? '';
|
|
27
|
+
return `${leading}${injected}${trailing}`;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Find the matching `)` for a call starting right after `(` and collect
|
|
32
|
+
* top-level argument commas.
|
|
33
|
+
*/
|
|
34
|
+
const findCallBoundaries = (source, afterParen) => {
|
|
35
|
+
let depth = 1;
|
|
36
|
+
let braceDepth = 0;
|
|
37
|
+
let bracketDepth = 0;
|
|
38
|
+
let closeIdx = -1;
|
|
39
|
+
const topLevelCommas = [];
|
|
40
|
+
|
|
41
|
+
let inStr = false;
|
|
42
|
+
let strCh = '';
|
|
43
|
+
let inTpl = false;
|
|
44
|
+
let inLineComment = false;
|
|
45
|
+
let inBlockComment = false;
|
|
46
|
+
|
|
47
|
+
for (let i = afterParen; i < source.length; i++) {
|
|
48
|
+
const ch = source[i];
|
|
49
|
+
const prev = i > 0 ? source[i - 1] : '';
|
|
50
|
+
const next = i + 1 < source.length ? source[i + 1] : '';
|
|
51
|
+
|
|
52
|
+
if (inLineComment) {
|
|
53
|
+
if (ch === '\n') inLineComment = false;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (inBlockComment) {
|
|
58
|
+
if (ch === '*' && next === '/') {
|
|
59
|
+
inBlockComment = false;
|
|
60
|
+
i++;
|
|
61
|
+
}
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (inStr) {
|
|
66
|
+
if (ch === strCh && prev !== '\\') inStr = false;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (inTpl) {
|
|
71
|
+
if (ch === '`' && prev !== '\\') {
|
|
72
|
+
inTpl = false;
|
|
73
|
+
}
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (ch === '/' && next === '/') {
|
|
78
|
+
inLineComment = true;
|
|
79
|
+
i++;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (ch === '/' && next === '*') {
|
|
84
|
+
inBlockComment = true;
|
|
85
|
+
i++;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (ch === '"' || ch === "'") {
|
|
90
|
+
inStr = true;
|
|
91
|
+
strCh = ch;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (ch === '`') {
|
|
96
|
+
inTpl = true;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (ch === '{') {
|
|
101
|
+
braceDepth++;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (ch === '}') {
|
|
106
|
+
if (braceDepth > 0) braceDepth--;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (ch === '[') {
|
|
111
|
+
bracketDepth++;
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (ch === ']') {
|
|
116
|
+
if (bracketDepth > 0) bracketDepth--;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (ch === '(') {
|
|
121
|
+
depth++;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (ch === ')') {
|
|
126
|
+
depth--;
|
|
127
|
+
if (depth === 0) {
|
|
128
|
+
closeIdx = i;
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (ch === ',' && depth === 1 && braceDepth === 0 && bracketDepth === 0) {
|
|
135
|
+
topLevelCommas.push(i);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (closeIdx === -1) return null;
|
|
140
|
+
return { closeIdx, topLevelCommas };
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Inject `{ path: '<file-path>' }` into createStyles/createStaticStyles calls.
|
|
145
|
+
*
|
|
146
|
+
* Rules:
|
|
147
|
+
* - One arg call: append second arg `{ path }`
|
|
148
|
+
* - Two+ arg call:
|
|
149
|
+
* - if second arg is object literal and has no `path`, inject into object
|
|
150
|
+
* - otherwise append third arg `{ path }` as a fallback
|
|
151
|
+
*/
|
|
152
|
+
const injectStylePath = (source, filePath, resourcePath = '') => {
|
|
153
|
+
if (!CALL_RE.test(source)) return source;
|
|
154
|
+
CALL_RE.lastIndex = 0;
|
|
155
|
+
|
|
156
|
+
const normalizedResourcePath = resourcePath.replace(/\\/g, '/');
|
|
157
|
+
|
|
158
|
+
// Skip the style factory implementation files themselves.
|
|
159
|
+
if (
|
|
160
|
+
(normalizedResourcePath.includes('createStaticStyles') ||
|
|
161
|
+
normalizedResourcePath.includes('createStyles')) &&
|
|
162
|
+
normalizedResourcePath.endsWith('index.ts')
|
|
163
|
+
) {
|
|
164
|
+
return source;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const positions = [];
|
|
168
|
+
let match;
|
|
169
|
+
while ((match = CALL_RE.exec(source)) !== null) {
|
|
170
|
+
const lineStart = source.lastIndexOf('\n', match.index) + 1;
|
|
171
|
+
const lineText = source.slice(lineStart, match.index).trimStart();
|
|
172
|
+
if (lineText.startsWith('*') || lineText.startsWith('//')) continue;
|
|
173
|
+
positions.push(match.index + match[0].length);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (positions.length === 0) return source;
|
|
177
|
+
|
|
178
|
+
let result = source;
|
|
179
|
+
const escapedPath = escapePathValue(filePath);
|
|
180
|
+
|
|
181
|
+
for (let pi = positions.length - 1; pi >= 0; pi--) {
|
|
182
|
+
const afterParen = positions[pi];
|
|
183
|
+
const callInfo = findCallBoundaries(result, afterParen);
|
|
184
|
+
if (!callInfo) continue;
|
|
185
|
+
|
|
186
|
+
const { closeIdx, topLevelCommas } = callInfo;
|
|
187
|
+
|
|
188
|
+
// Single-arg call: add options as second arg.
|
|
189
|
+
if (topLevelCommas.length === 0) {
|
|
190
|
+
const inner = result.slice(afterParen, closeIdx);
|
|
191
|
+
const hasTrailingComma = /,\s*$/.test(inner);
|
|
192
|
+
result =
|
|
193
|
+
result.slice(0, closeIdx) +
|
|
194
|
+
(hasTrailingComma ? ` { path: '${escapedPath}' }` : `, { path: '${escapedPath}' }`) +
|
|
195
|
+
result.slice(closeIdx);
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Two+ args: patch second arg when it's an object literal.
|
|
200
|
+
const secondStart = topLevelCommas[0] + 1;
|
|
201
|
+
const secondEnd = topLevelCommas[1] ?? closeIdx;
|
|
202
|
+
const secondArgRaw = result.slice(secondStart, secondEnd);
|
|
203
|
+
|
|
204
|
+
if (hasPathProperty(secondArgRaw)) continue;
|
|
205
|
+
|
|
206
|
+
const injectedSecondArg = injectPathIntoObjectLiteral(secondArgRaw, filePath);
|
|
207
|
+
if (injectedSecondArg !== null) {
|
|
208
|
+
result =
|
|
209
|
+
result.slice(0, secondStart) +
|
|
210
|
+
injectedSecondArg +
|
|
211
|
+
result.slice(secondEnd);
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Fallback for non-object second args.
|
|
216
|
+
const inner = result.slice(afterParen, closeIdx);
|
|
217
|
+
const hasTrailingComma = /,\s*$/.test(inner);
|
|
218
|
+
result =
|
|
219
|
+
result.slice(0, closeIdx) +
|
|
220
|
+
(hasTrailingComma ? ` { path: '${escapedPath}' }` : `, { path: '${escapedPath}' }`) +
|
|
221
|
+
result.slice(closeIdx);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return result;
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
module.exports = {
|
|
228
|
+
injectStylePath,
|
|
229
|
+
};
|