22
22
* > 👉 **Note**: you can also configure `runtime`, `importSource`, `pragma`,
23
23
* > and `pragmaFrag` from within files through comments.
24
24
* @property {Runtime | null | undefined } [runtime='classic']
25
- * Choose the runtime.
25
+ * Choose the runtime (default: `'classic'`) .
26
26
*
27
27
* Comment form: `@jsxRuntime theRuntime`.
28
28
* @property {string | null | undefined } [importSource='react']
29
29
* Place to import `jsx`, `jsxs`, `jsxDEV`, and `Fragment` from, when the
30
- * effective runtime is automatic.
30
+ * effective runtime is automatic (default: `'react'`) .
31
31
*
32
32
* Comment form: `@jsxImportSource theSource`.
33
33
*
46
46
* > ```
47
47
* @property {string | null | undefined } [pragma='React.createElement']
48
48
* Identifier or member expression to call when the effective runtime is
49
- * classic.
49
+ * classic (default: `'React.createElement'`) .
50
50
*
51
51
* Comment form: `@jsx identifier`.
52
52
* @property {string | null | undefined } [pragmaFrag='React.Fragment']
53
53
* Identifier or member expression to use as a symbol for fragments when the
54
- * effective runtime is classic.
54
+ * effective runtime is classic (default: `'React.Fragment'`) .
55
55
*
56
56
* Comment form: `@jsxFrag identifier`.
57
57
* @property {boolean | null | undefined } [development=false]
58
58
* When in the automatic runtime, whether to import
59
59
* `theSource/jsx-dev-runtime.js`, use `jsxDEV`, and pass location info when
60
- * available.
60
+ * available (default: `false`) .
61
61
*
62
62
* This helps debugging but adds a lot of code that you don’t want in
63
63
* production.
64
64
* @property {string | null | undefined } [filePath]
65
- * File path to the original source file.
65
+ * File path to the original source file (optional) .
66
66
*
67
67
* Passed in location info to `jsxDEV` when using the automatic runtime with
68
68
* `development: true`.
69
69
*
70
70
* @typedef Annotations
71
71
* State where info from comments is gathered.
72
- * @property {Runtime | undefined } [jsxRuntime]
73
- * Runtime.
74
72
* @property {string | undefined } [jsx]
75
73
* JSX identifier (`pragma`).
76
74
* @property {string | undefined } [jsxFrag]
77
75
* JSX identifier of fragment (`pragmaFrag`).
78
76
* @property {string | undefined } [jsxImportSource]
79
77
* Where to import an automatic JSX runtime from.
78
+ * @property {Runtime | undefined } [jsxRuntime]
79
+ * Runtime.
80
80
*
81
81
* @typedef Imports
82
82
* State of used identifiers from the automatic runtime.
90
90
* Symbol of `jsxDEV`.
91
91
*/
92
92
93
- import { walk } from 'estree-walker '
93
+ import { ok as assert } from 'devlop '
94
94
import { name as isIdentifierName } from 'estree-util-is-identifier-name'
95
+ import { walk } from 'estree-walker'
95
96
96
97
const regex = / @ ( j s x | j s x F r a g | j s x I m p o r t S o u r c e | j s x R u n t i m e ) \s + ( \S + ) / g
97
98
@@ -141,7 +142,8 @@ export function buildJsx(tree, options) {
141
142
let match = regex . exec ( comments [ index ] . value )
142
143
143
144
while ( match ) {
144
- // @ts -expect-error: indexable.
145
+ // @ts -expect-error: `match[1]` is always a key, `match[2]` when
146
+ // runtime is checked later.
145
147
annotations [ match [ 1 ] ] = match [ 2 ]
146
148
match = regex . exec ( comments [ index ] . value )
147
149
}
@@ -272,14 +274,16 @@ export function buildJsx(tree, options) {
272
274
children . push ( text )
273
275
}
274
276
} else {
275
- // @ts -expect-error JSX{Element,Fragment} have already been compiled,
276
- // and `JSXSpreadChild` is not supported in Babel either, so ignore
277
- // it.
277
+ assert (
278
+ child . type !== 'JSXElement' &&
279
+ child . type !== 'JSXFragment' &&
280
+ child . type !== 'JSXSpreadChild'
281
+ )
278
282
children . push ( child )
279
283
}
280
284
}
281
285
282
- /** @type {MemberExpression | Literal | Identifier } */
286
+ /** @type {Identifier | Literal | MemberExpression } */
283
287
let name
284
288
/** @type {Array<Property | SpreadElement> } */
285
289
const fields = [ ]
@@ -333,9 +337,16 @@ export function buildJsx(tree, options) {
333
337
)
334
338
}
335
339
336
- // @ts -expect-error I can’t see object patterns being used as
337
- // attribute values? 🤷♂️
338
- key = prop . value
340
+ const value = prop . value
341
+
342
+ assert (
343
+ value . type !== 'AssignmentPattern' &&
344
+ value . type !== 'ArrayPattern' &&
345
+ value . type !== 'ObjectPattern' &&
346
+ value . type !== 'RestElement'
347
+ )
348
+
349
+ key = value
339
350
} else {
340
351
fields . push ( prop )
341
352
}
@@ -371,7 +382,7 @@ export function buildJsx(tree, options) {
371
382
parameters = children
372
383
}
373
384
374
- /** @type {MemberExpression | Literal | Identifier } */
385
+ /** @type {Identifier | Literal | MemberExpression } */
375
386
let callee
376
387
377
388
if ( automatic ) {
@@ -487,16 +498,21 @@ function toProperty(node) {
487
498
488
499
if ( node . value ) {
489
500
if ( node . value . type === 'JSXExpressionContainer' ) {
490
- // @ts -expect-error `JSXEmptyExpression` is not allowed in props.
491
- value = node . value . expression
501
+ const valueExpression = node . value . expression
502
+ assert (
503
+ valueExpression . type !== 'JSXEmptyExpression' ,
504
+ '`JSXEmptyExpression` is not allowed in props.'
505
+ )
506
+ value = valueExpression
492
507
}
493
508
// Literal or call expression.
494
509
else {
495
- // @ts -expect-error: JSX{Element,Fragment} are already compiled to
496
- // `CallExpression`.
497
- value = node . value
498
- // @ts -expect-error Remove `raw` so we don’t get character references in
499
- // strings.
510
+ const nodeValue = node . value
511
+ assert (
512
+ nodeValue . type !== 'JSXElement' && nodeValue . type !== 'JSXFragment' ,
513
+ 'JSX{Element,Fragment} are already compiled to `CallExpression`'
514
+ )
515
+ value = nodeValue
500
516
delete value . raw
501
517
}
502
518
}
@@ -522,13 +538,13 @@ function toProperty(node) {
522
538
/**
523
539
* Turn a JSX identifier into a normal JS identifier.
524
540
*
525
- * @param {JSXMemberExpression | JSXNamespacedName | JSXIdentifier } node
541
+ * @param {JSXIdentifier | JSXMemberExpression | JSXNamespacedName } node
526
542
* JSX identifier.
527
- * @returns {MemberExpression | Identifier | Literal }
543
+ * @returns {Identifier | Literal | MemberExpression }
528
544
* JS identifier.
529
545
*/
530
546
function toIdentifier ( node ) {
531
- /** @type {MemberExpression | Identifier | Literal } */
547
+ /** @type {Identifier | Literal | MemberExpression } */
532
548
let replace
533
549
534
550
if ( node . type === 'JSXMemberExpression' ) {
@@ -589,7 +605,7 @@ function toMemberExpression(id) {
589
605
: prop
590
606
}
591
607
592
- // @ts -expect-error: always a result.
608
+ assert ( result , ' always a result' )
593
609
return result
594
610
}
595
611
@@ -600,7 +616,7 @@ function toMemberExpression(id) {
600
616
* Node to inherit from.
601
617
* @param {Node } to
602
618
* Node to add to.
603
- * @returns {void }
619
+ * @returns {undefined }
604
620
* Nothing.
605
621
*/
606
622
function create ( from , to ) {
0 commit comments