@knighted/jsx 1.12.0 → 1.13.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.
- package/dist/cjs/transform.cjs +26 -8
- package/dist/cjs/transform.d.cts +1 -0
- package/dist/transform.d.ts +1 -0
- package/dist/transform.js +26 -8
- package/package.json +1 -1
package/dist/cjs/transform.cjs
CHANGED
|
@@ -258,26 +258,37 @@ const unwrapExpressionNode = (value) => {
|
|
|
258
258
|
}
|
|
259
259
|
return current;
|
|
260
260
|
};
|
|
261
|
-
const
|
|
261
|
+
const toJsxExpressionNode = (value) => {
|
|
262
262
|
const unwrapped = unwrapExpressionNode(value);
|
|
263
263
|
if (!isObjectRecord(unwrapped) || typeof unwrapped.type !== 'string') {
|
|
264
|
-
return
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
if (unwrapped.type === 'JSXElement' || unwrapped.type === 'JSXFragment') {
|
|
267
|
+
return unwrapped;
|
|
265
268
|
}
|
|
266
|
-
return
|
|
269
|
+
return null;
|
|
267
270
|
};
|
|
271
|
+
const createEmptyTopLevelJsxExpressionMetadata = () => ({
|
|
272
|
+
hasTopLevelJsxExpression: false,
|
|
273
|
+
topLevelJsxExpressionRange: null,
|
|
274
|
+
});
|
|
268
275
|
const collectTopLevelJsxExpressionMetadata = (body) => {
|
|
269
276
|
if (!Array.isArray(body)) {
|
|
270
|
-
return
|
|
277
|
+
return createEmptyTopLevelJsxExpressionMetadata();
|
|
271
278
|
}
|
|
272
279
|
for (const statement of body) {
|
|
273
280
|
if (!isObjectRecord(statement) || statement.type !== 'ExpressionStatement') {
|
|
274
281
|
continue;
|
|
275
282
|
}
|
|
276
|
-
|
|
277
|
-
|
|
283
|
+
const jsxNode = toJsxExpressionNode(statement.expression);
|
|
284
|
+
if (jsxNode) {
|
|
285
|
+
return {
|
|
286
|
+
hasTopLevelJsxExpression: true,
|
|
287
|
+
topLevelJsxExpressionRange: toSourceRange(jsxNode),
|
|
288
|
+
};
|
|
278
289
|
}
|
|
279
290
|
}
|
|
280
|
-
return
|
|
291
|
+
return createEmptyTopLevelJsxExpressionMetadata();
|
|
281
292
|
};
|
|
282
293
|
const ensureSupportedOptions = (options) => {
|
|
283
294
|
if (options.sourceType !== undefined &&
|
|
@@ -316,9 +327,11 @@ function transformJsxSource(source, options = {}) {
|
|
|
316
327
|
const declarations = internalOptions.collectTopLevelDeclarations
|
|
317
328
|
? collectTopLevelDeclarationMetadata(parsed.program.body)
|
|
318
329
|
: undefined;
|
|
319
|
-
const
|
|
330
|
+
const topLevelJsxExpressionMetadata = internalOptions.collectTopLevelJsxExpression
|
|
320
331
|
? collectTopLevelJsxExpressionMetadata(parsed.program.body)
|
|
321
332
|
: undefined;
|
|
333
|
+
const hasTopLevelJsxExpression = topLevelJsxExpressionMetadata?.hasTopLevelJsxExpression;
|
|
334
|
+
const topLevelJsxExpressionRange = topLevelJsxExpressionMetadata?.topLevelJsxExpressionRange;
|
|
322
335
|
if (parserDiagnostics.length) {
|
|
323
336
|
return {
|
|
324
337
|
code: source,
|
|
@@ -327,6 +340,7 @@ function transformJsxSource(source, options = {}) {
|
|
|
327
340
|
diagnostics: parserDiagnostics,
|
|
328
341
|
declarations,
|
|
329
342
|
hasTopLevelJsxExpression,
|
|
343
|
+
topLevelJsxExpressionRange,
|
|
330
344
|
};
|
|
331
345
|
}
|
|
332
346
|
const transpileBaseOptions = {
|
|
@@ -344,6 +358,7 @@ function transformJsxSource(source, options = {}) {
|
|
|
344
358
|
diagnostics: parserDiagnostics,
|
|
345
359
|
declarations,
|
|
346
360
|
hasTopLevelJsxExpression,
|
|
361
|
+
topLevelJsxExpressionRange,
|
|
347
362
|
};
|
|
348
363
|
}
|
|
349
364
|
if (typescriptStripBackend === 'transpile-manual') {
|
|
@@ -358,6 +373,7 @@ function transformJsxSource(source, options = {}) {
|
|
|
358
373
|
diagnostics: parserDiagnostics,
|
|
359
374
|
declarations,
|
|
360
375
|
hasTopLevelJsxExpression,
|
|
376
|
+
topLevelJsxExpressionRange,
|
|
361
377
|
};
|
|
362
378
|
}
|
|
363
379
|
const transformed = (0, oxc_transform_1.transformSync)('transform-jsx-source.tsx', source, {
|
|
@@ -377,6 +393,7 @@ function transformJsxSource(source, options = {}) {
|
|
|
377
393
|
diagnostics,
|
|
378
394
|
declarations,
|
|
379
395
|
hasTopLevelJsxExpression,
|
|
396
|
+
topLevelJsxExpressionRange,
|
|
380
397
|
};
|
|
381
398
|
}
|
|
382
399
|
const jsxResult = (0, transpile_js_1.transpileJsxSource)(transformed.code, transpileBaseOptions);
|
|
@@ -387,5 +404,6 @@ function transformJsxSource(source, options = {}) {
|
|
|
387
404
|
diagnostics,
|
|
388
405
|
declarations,
|
|
389
406
|
hasTopLevelJsxExpression,
|
|
407
|
+
topLevelJsxExpressionRange,
|
|
390
408
|
};
|
|
391
409
|
}
|
package/dist/cjs/transform.d.cts
CHANGED
|
@@ -45,6 +45,7 @@ export type TransformJsxSourceResult = {
|
|
|
45
45
|
diagnostics: TransformDiagnostic[];
|
|
46
46
|
declarations?: TransformTopLevelDeclaration[];
|
|
47
47
|
hasTopLevelJsxExpression?: boolean;
|
|
48
|
+
topLevelJsxExpressionRange?: SourceRange | null;
|
|
48
49
|
};
|
|
49
50
|
export declare function transformJsxSource(source: string, options?: TransformJsxSourceOptions): TransformJsxSourceResult;
|
|
50
51
|
export {};
|
package/dist/transform.d.ts
CHANGED
|
@@ -45,6 +45,7 @@ export type TransformJsxSourceResult = {
|
|
|
45
45
|
diagnostics: TransformDiagnostic[];
|
|
46
46
|
declarations?: TransformTopLevelDeclaration[];
|
|
47
47
|
hasTopLevelJsxExpression?: boolean;
|
|
48
|
+
topLevelJsxExpressionRange?: SourceRange | null;
|
|
48
49
|
};
|
|
49
50
|
export declare function transformJsxSource(source: string, options?: TransformJsxSourceOptions): TransformJsxSourceResult;
|
|
50
51
|
export {};
|
package/dist/transform.js
CHANGED
|
@@ -255,26 +255,37 @@ const unwrapExpressionNode = (value) => {
|
|
|
255
255
|
}
|
|
256
256
|
return current;
|
|
257
257
|
};
|
|
258
|
-
const
|
|
258
|
+
const toJsxExpressionNode = (value) => {
|
|
259
259
|
const unwrapped = unwrapExpressionNode(value);
|
|
260
260
|
if (!isObjectRecord(unwrapped) || typeof unwrapped.type !== 'string') {
|
|
261
|
-
return
|
|
261
|
+
return null;
|
|
262
|
+
}
|
|
263
|
+
if (unwrapped.type === 'JSXElement' || unwrapped.type === 'JSXFragment') {
|
|
264
|
+
return unwrapped;
|
|
262
265
|
}
|
|
263
|
-
return
|
|
266
|
+
return null;
|
|
264
267
|
};
|
|
268
|
+
const createEmptyTopLevelJsxExpressionMetadata = () => ({
|
|
269
|
+
hasTopLevelJsxExpression: false,
|
|
270
|
+
topLevelJsxExpressionRange: null,
|
|
271
|
+
});
|
|
265
272
|
const collectTopLevelJsxExpressionMetadata = (body) => {
|
|
266
273
|
if (!Array.isArray(body)) {
|
|
267
|
-
return
|
|
274
|
+
return createEmptyTopLevelJsxExpressionMetadata();
|
|
268
275
|
}
|
|
269
276
|
for (const statement of body) {
|
|
270
277
|
if (!isObjectRecord(statement) || statement.type !== 'ExpressionStatement') {
|
|
271
278
|
continue;
|
|
272
279
|
}
|
|
273
|
-
|
|
274
|
-
|
|
280
|
+
const jsxNode = toJsxExpressionNode(statement.expression);
|
|
281
|
+
if (jsxNode) {
|
|
282
|
+
return {
|
|
283
|
+
hasTopLevelJsxExpression: true,
|
|
284
|
+
topLevelJsxExpressionRange: toSourceRange(jsxNode),
|
|
285
|
+
};
|
|
275
286
|
}
|
|
276
287
|
}
|
|
277
|
-
return
|
|
288
|
+
return createEmptyTopLevelJsxExpressionMetadata();
|
|
278
289
|
};
|
|
279
290
|
const ensureSupportedOptions = (options) => {
|
|
280
291
|
if (options.sourceType !== undefined &&
|
|
@@ -313,9 +324,11 @@ export function transformJsxSource(source, options = {}) {
|
|
|
313
324
|
const declarations = internalOptions.collectTopLevelDeclarations
|
|
314
325
|
? collectTopLevelDeclarationMetadata(parsed.program.body)
|
|
315
326
|
: undefined;
|
|
316
|
-
const
|
|
327
|
+
const topLevelJsxExpressionMetadata = internalOptions.collectTopLevelJsxExpression
|
|
317
328
|
? collectTopLevelJsxExpressionMetadata(parsed.program.body)
|
|
318
329
|
: undefined;
|
|
330
|
+
const hasTopLevelJsxExpression = topLevelJsxExpressionMetadata?.hasTopLevelJsxExpression;
|
|
331
|
+
const topLevelJsxExpressionRange = topLevelJsxExpressionMetadata?.topLevelJsxExpressionRange;
|
|
319
332
|
if (parserDiagnostics.length) {
|
|
320
333
|
return {
|
|
321
334
|
code: source,
|
|
@@ -324,6 +337,7 @@ export function transformJsxSource(source, options = {}) {
|
|
|
324
337
|
diagnostics: parserDiagnostics,
|
|
325
338
|
declarations,
|
|
326
339
|
hasTopLevelJsxExpression,
|
|
340
|
+
topLevelJsxExpressionRange,
|
|
327
341
|
};
|
|
328
342
|
}
|
|
329
343
|
const transpileBaseOptions = {
|
|
@@ -341,6 +355,7 @@ export function transformJsxSource(source, options = {}) {
|
|
|
341
355
|
diagnostics: parserDiagnostics,
|
|
342
356
|
declarations,
|
|
343
357
|
hasTopLevelJsxExpression,
|
|
358
|
+
topLevelJsxExpressionRange,
|
|
344
359
|
};
|
|
345
360
|
}
|
|
346
361
|
if (typescriptStripBackend === 'transpile-manual') {
|
|
@@ -355,6 +370,7 @@ export function transformJsxSource(source, options = {}) {
|
|
|
355
370
|
diagnostics: parserDiagnostics,
|
|
356
371
|
declarations,
|
|
357
372
|
hasTopLevelJsxExpression,
|
|
373
|
+
topLevelJsxExpressionRange,
|
|
358
374
|
};
|
|
359
375
|
}
|
|
360
376
|
const transformed = transformSync('transform-jsx-source.tsx', source, {
|
|
@@ -374,6 +390,7 @@ export function transformJsxSource(source, options = {}) {
|
|
|
374
390
|
diagnostics,
|
|
375
391
|
declarations,
|
|
376
392
|
hasTopLevelJsxExpression,
|
|
393
|
+
topLevelJsxExpressionRange,
|
|
377
394
|
};
|
|
378
395
|
}
|
|
379
396
|
const jsxResult = transpileJsxSource(transformed.code, transpileBaseOptions);
|
|
@@ -384,5 +401,6 @@ export function transformJsxSource(source, options = {}) {
|
|
|
384
401
|
diagnostics,
|
|
385
402
|
declarations,
|
|
386
403
|
hasTopLevelJsxExpression,
|
|
404
|
+
topLevelJsxExpressionRange,
|
|
387
405
|
};
|
|
388
406
|
}
|