@@ -8,6 +8,7 @@ const regex = /@(jsx|jsxFrag|jsxImportSource|jsxRuntime)\s+(\S+)/g
8
8
* @typedef {import('estree-jsx').Comment } Comment
9
9
* @typedef {import('estree-jsx').Expression } Expression
10
10
* @typedef {import('estree-jsx').Pattern } Pattern
11
+ * @typedef {import('estree-jsx').ObjectExpression } ObjectExpression
11
12
* @typedef {import('estree-jsx').Property } Property
12
13
* @typedef {import('estree-jsx').ImportSpecifier } ImportSpecifier
13
14
* @typedef {import('estree-jsx').SpreadElement } SpreadElement
@@ -35,6 +36,8 @@ const regex = /@(jsx|jsxFrag|jsxImportSource|jsxRuntime)\s+(\S+)/g
35
36
* @property {string } [importSource='react']
36
37
* @property {string } [pragma='React.createElement']
37
38
* @property {string } [pragmaFrag='React.Fragment']
39
+ * @property {boolean } [development=false]
40
+ * @property {string } [filePath]
38
41
*/
39
42
40
43
/**
@@ -55,7 +58,7 @@ export function buildJsx(tree, options = {}) {
55
58
let automatic = options . runtime === 'automatic'
56
59
/** @type {Annotations } */
57
60
const annotations = { }
58
- /** @type {{fragment?: boolean, jsx?: boolean, jsxs?: boolean} } */
61
+ /** @type {{fragment?: boolean, jsx?: boolean, jsxs?: boolean, jsxDEV?: boolean } } */
59
62
const imports = { }
60
63
61
64
walk ( tree , {
@@ -139,6 +142,14 @@ export function buildJsx(tree, options = {}) {
139
142
} )
140
143
}
141
144
145
+ if ( imports . jsxDEV ) {
146
+ specifiers . push ( {
147
+ type : 'ImportSpecifier' ,
148
+ imported : { type : 'Identifier' , name : 'jsxDEV' } ,
149
+ local : { type : 'Identifier' , name : '_jsxDEV' }
150
+ } )
151
+ }
152
+
142
153
if ( specifiers . length > 0 ) {
143
154
node . body . unshift ( {
144
155
type : 'ImportDeclaration' ,
@@ -148,7 +159,8 @@ export function buildJsx(tree, options = {}) {
148
159
value :
149
160
( annotations . jsxImportSource ||
150
161
options . importSource ||
151
- 'react' ) + '/jsx-runtime'
162
+ 'react' ) +
163
+ ( options . development ? '/jsx-dev-runtime' : '/jsx-runtime' )
152
164
}
153
165
} )
154
166
}
@@ -267,19 +279,21 @@ export function buildJsx(tree, options = {}) {
267
279
)
268
280
}
269
281
270
- if ( automatic && children . length > 0 ) {
271
- fields . push ( {
272
- type : 'Property' ,
273
- key : { type : 'Identifier' , name : 'children' } ,
274
- value :
275
- children . length > 1
276
- ? { type : 'ArrayExpression' , elements : children }
277
- : children [ 0 ] ,
278
- kind : 'init' ,
279
- method : false ,
280
- shorthand : false ,
281
- computed : false
282
- } )
282
+ if ( automatic ) {
283
+ if ( children . length > 0 ) {
284
+ fields . push ( {
285
+ type : 'Property' ,
286
+ key : { type : 'Identifier' , name : 'children' } ,
287
+ value :
288
+ children . length > 1
289
+ ? { type : 'ArrayExpression' , elements : children }
290
+ : children [ 0 ] ,
291
+ kind : 'init' ,
292
+ method : false ,
293
+ shorthand : false ,
294
+ computed : false
295
+ } )
296
+ }
283
297
} else {
284
298
parameters = children
285
299
}
@@ -310,19 +324,74 @@ export function buildJsx(tree, options = {}) {
310
324
}
311
325
312
326
if ( automatic ) {
313
- if ( children . length > 1 ) {
327
+ parameters . push ( props || { type : 'ObjectExpression' , properties : [ ] } )
328
+
329
+ if ( key ) {
330
+ parameters . push ( key )
331
+ } else if ( options . development ) {
332
+ parameters . push ( { type : 'Identifier' , name : 'undefined' } )
333
+ }
334
+
335
+ const isStaticChildren = children . length > 1
336
+
337
+ if ( options . development ) {
338
+ imports . jsxDEV = true
339
+ callee = {
340
+ type : 'Identifier' ,
341
+ name : '_jsxDEV'
342
+ }
343
+ parameters . push ( { type : 'Literal' , value : isStaticChildren } )
344
+
345
+ /** @type {ObjectExpression } */
346
+ const source = {
347
+ type : 'ObjectExpression' ,
348
+ properties : [
349
+ {
350
+ type : 'Property' ,
351
+ method : false ,
352
+ shorthand : false ,
353
+ computed : false ,
354
+ kind : 'init' ,
355
+ key : { type : 'Identifier' , name : 'fileName' } ,
356
+ value : {
357
+ type : 'Literal' ,
358
+ value : options . filePath || '<source.js>'
359
+ }
360
+ }
361
+ ]
362
+ }
363
+
364
+ if ( node . loc ) {
365
+ source . properties . push (
366
+ {
367
+ type : 'Property' ,
368
+ method : false ,
369
+ shorthand : false ,
370
+ computed : false ,
371
+ kind : 'init' ,
372
+ key : { type : 'Identifier' , name : 'lineNumber' } ,
373
+ value : { type : 'Literal' , value : node . loc . start . line }
374
+ } ,
375
+ {
376
+ type : 'Property' ,
377
+ method : false ,
378
+ shorthand : false ,
379
+ computed : false ,
380
+ kind : 'init' ,
381
+ key : { type : 'Identifier' , name : 'columnNumber' } ,
382
+ value : { type : 'Literal' , value : node . loc . start . column + 1 }
383
+ }
384
+ )
385
+ }
386
+
387
+ parameters . push ( source , { type : 'ThisExpression' } )
388
+ } else if ( isStaticChildren ) {
314
389
imports . jsxs = true
315
390
callee = { type : 'Identifier' , name : '_jsxs' }
316
391
} else {
317
392
imports . jsx = true
318
393
callee = { type : 'Identifier' , name : '_jsx' }
319
394
}
320
-
321
- parameters . push ( props || { type : 'ObjectExpression' , properties : [ ] } )
322
-
323
- if ( key ) {
324
- parameters . push ( key )
325
- }
326
395
}
327
396
// Classic.
328
397
else {
0 commit comments