Skip to content

Commit 6ea9805

Browse files
committed
Refactor code-style
1 parent b7fb560 commit 6ea9805

File tree

3 files changed

+1434
-1254
lines changed

3 files changed

+1434
-1254
lines changed

lib/index.js

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
* > 👉 **Note**: you can also configure `runtime`, `importSource`, `pragma`,
2323
* > and `pragmaFrag` from within files through comments.
2424
* @property {Runtime | null | undefined} [runtime='classic']
25-
* Choose the runtime.
25+
* Choose the runtime (default: `'classic'`).
2626
*
2727
* Comment form: `@jsxRuntime theRuntime`.
2828
* @property {string | null | undefined} [importSource='react']
2929
* Place to import `jsx`, `jsxs`, `jsxDEV`, and `Fragment` from, when the
30-
* effective runtime is automatic.
30+
* effective runtime is automatic (default: `'react'`).
3131
*
3232
* Comment form: `@jsxImportSource theSource`.
3333
*
@@ -46,37 +46,37 @@
4646
* > ```
4747
* @property {string | null | undefined} [pragma='React.createElement']
4848
* Identifier or member expression to call when the effective runtime is
49-
* classic.
49+
* classic (default: `'React.createElement'`).
5050
*
5151
* Comment form: `@jsx identifier`.
5252
* @property {string | null | undefined} [pragmaFrag='React.Fragment']
5353
* 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'`).
5555
*
5656
* Comment form: `@jsxFrag identifier`.
5757
* @property {boolean | null | undefined} [development=false]
5858
* When in the automatic runtime, whether to import
5959
* `theSource/jsx-dev-runtime.js`, use `jsxDEV`, and pass location info when
60-
* available.
60+
* available (default: `false`).
6161
*
6262
* This helps debugging but adds a lot of code that you don’t want in
6363
* production.
6464
* @property {string | null | undefined} [filePath]
65-
* File path to the original source file.
65+
* File path to the original source file (optional).
6666
*
6767
* Passed in location info to `jsxDEV` when using the automatic runtime with
6868
* `development: true`.
6969
*
7070
* @typedef Annotations
7171
* State where info from comments is gathered.
72-
* @property {Runtime | undefined} [jsxRuntime]
73-
* Runtime.
7472
* @property {string | undefined} [jsx]
7573
* JSX identifier (`pragma`).
7674
* @property {string | undefined} [jsxFrag]
7775
* JSX identifier of fragment (`pragmaFrag`).
7876
* @property {string | undefined} [jsxImportSource]
7977
* Where to import an automatic JSX runtime from.
78+
* @property {Runtime | undefined} [jsxRuntime]
79+
* Runtime.
8080
*
8181
* @typedef Imports
8282
* State of used identifiers from the automatic runtime.
@@ -90,8 +90,9 @@
9090
* Symbol of `jsxDEV`.
9191
*/
9292

93-
import {walk} from 'estree-walker'
93+
import {ok as assert} from 'devlop'
9494
import {name as isIdentifierName} from 'estree-util-is-identifier-name'
95+
import {walk} from 'estree-walker'
9596

9697
const regex = /@(jsx|jsxFrag|jsxImportSource|jsxRuntime)\s+(\S+)/g
9798

@@ -141,7 +142,8 @@ export function buildJsx(tree, options) {
141142
let match = regex.exec(comments[index].value)
142143

143144
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.
145147
annotations[match[1]] = match[2]
146148
match = regex.exec(comments[index].value)
147149
}
@@ -272,14 +274,16 @@ export function buildJsx(tree, options) {
272274
children.push(text)
273275
}
274276
} 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+
)
278282
children.push(child)
279283
}
280284
}
281285

282-
/** @type {MemberExpression | Literal | Identifier} */
286+
/** @type {Identifier | Literal | MemberExpression} */
283287
let name
284288
/** @type {Array<Property | SpreadElement>} */
285289
const fields = []
@@ -333,9 +337,16 @@ export function buildJsx(tree, options) {
333337
)
334338
}
335339

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
339350
} else {
340351
fields.push(prop)
341352
}
@@ -371,7 +382,7 @@ export function buildJsx(tree, options) {
371382
parameters = children
372383
}
373384

374-
/** @type {MemberExpression | Literal | Identifier} */
385+
/** @type {Identifier | Literal | MemberExpression} */
375386
let callee
376387

377388
if (automatic) {
@@ -487,16 +498,21 @@ function toProperty(node) {
487498

488499
if (node.value) {
489500
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
492507
}
493508
// Literal or call expression.
494509
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
500516
delete value.raw
501517
}
502518
}
@@ -522,13 +538,13 @@ function toProperty(node) {
522538
/**
523539
* Turn a JSX identifier into a normal JS identifier.
524540
*
525-
* @param {JSXMemberExpression | JSXNamespacedName | JSXIdentifier} node
541+
* @param {JSXIdentifier | JSXMemberExpression | JSXNamespacedName} node
526542
* JSX identifier.
527-
* @returns {MemberExpression | Identifier | Literal}
543+
* @returns {Identifier | Literal | MemberExpression}
528544
* JS identifier.
529545
*/
530546
function toIdentifier(node) {
531-
/** @type {MemberExpression | Identifier | Literal} */
547+
/** @type {Identifier | Literal | MemberExpression} */
532548
let replace
533549

534550
if (node.type === 'JSXMemberExpression') {
@@ -589,7 +605,7 @@ function toMemberExpression(id) {
589605
: prop
590606
}
591607

592-
// @ts-expect-error: always a result.
608+
assert(result, 'always a result')
593609
return result
594610
}
595611

@@ -600,7 +616,7 @@ function toMemberExpression(id) {
600616
* Node to inherit from.
601617
* @param {Node} to
602618
* Node to add to.
603-
* @returns {void}
619+
* @returns {undefined}
604620
* Nothing.
605621
*/
606622
function create(from, to) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
],
4242
"dependencies": {
4343
"@types/estree-jsx": "^1.0.0",
44+
"devlop": "^1.0.0",
4445
"estree-util-is-identifier-name": "^3.0.0",
4546
"estree-walker": "^3.0.0"
4647
},

0 commit comments

Comments
 (0)