Skip to content

Commit 6c84d55

Browse files
committed
Refactor code-style
* Add support for `null` as input of API * Add more docs to JSDoc
1 parent 3044211 commit 6c84d55

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

lib/index.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,26 @@
1313
* @typedef {import('estree-jsx').JSXNamespacedName} JSXNamespacedName
1414
* @typedef {import('estree-jsx').JSXIdentifier} JSXIdentifier
1515
*
16+
* @typedef {'automatic' | 'classic'} Runtime
17+
*
1618
* @typedef Options
17-
* Configuration (optional).
19+
* Configuration.
1820
*
1921
* > 👉 **Note**: you can also configure `runtime`, `importSource`, `pragma`,
2022
* > and `pragmaFrag` from within files through comments.
21-
* @property {'automatic' | 'classic'} [runtime='classic']
23+
* @property {Runtime | null | undefined} [runtime='classic']
2224
* Choose the runtime.
2325
*
2426
* Comment form: `@jsxRuntime theRuntime`.
25-
* @property {string} [importSource='react']
27+
* @property {string | null | undefined} [importSource='react']
2628
* Place to import `jsx`, `jsxs`, `jsxDEV`, and/or `Fragment` from, when the
2729
* effective runtime is automatic.
2830
*
2931
* Comment form: `@jsxImportSource theSource`.
3032
*
3133
* > 👉 **Note**: `/jsx-runtime` or `/jsx-dev-runtime` is appended to this
3234
* > provided source.
33-
* > In CJS, that can resolve to a file, as in `theSource/jsx-runtime.js`,
35+
* > In CJS, that can resolve to a file (as in `theSource/jsx-runtime.js`),
3436
* > but for ESM an export map needs to be set up to point to files:
3537
* >
3638
* > ```js
@@ -41,33 +43,50 @@
4143
* > "./jsx-dev-runtime": "./path/to/jsx-runtime.js"
4244
* > // …
4345
* > ```
44-
* @property {string} [pragma='React.createElement']
46+
* @property {string | null | undefined} [pragma='React.createElement']
4547
* Identifier or member expression to call when the effective runtime is
4648
* classic.
4749
*
4850
* Comment form: `@jsx identifier`.
49-
* @property {string} [pragmaFrag='React.Fragment']
51+
* @property {string | null | undefined} [pragmaFrag='React.Fragment']
5052
* Identifier or member expression to use as a symbol for fragments when the
5153
* effective runtime is classic.
5254
*
5355
* Comment form: `@jsxFrag identifier`.
54-
* @property {boolean} [development=false]
56+
* @property {boolean | null | undefined} [development=false]
5557
* Import `jsxDEV` from `theSource/jsx-dev-runtime.js` and add location info
5658
* on where a component originated from.
5759
*
5860
* This helps debugging but adds a lot of code that you don’t want in
5961
* production.
6062
* Only used in the automatic runtime.
61-
* @property {string} [filePath]
63+
* @property {string | null | undefined} [filePath]
6264
* File path to the original source file.
65+
*
6366
* Used in the location info when using the automatic runtime with
6467
* `development: true`.
6568
*
6669
* @typedef Annotations
67-
* @property {'automatic' | 'classic'} [jsxRuntime]
68-
* @property {string} [jsx]
69-
* @property {string} [jsxFrag]
70-
* @property {string} [jsxImportSource]
70+
* State where info from comments is gathered.
71+
* @property {Runtime | undefined} [jsxRuntime]
72+
* Runtime.
73+
* @property {string | undefined} [jsx]
74+
* JSX identifier (`pragma`).
75+
* @property {string | undefined} [jsxFrag]
76+
* JSX identifier of fragment (`pragmaFrag`).
77+
* @property {string | undefined} [jsxImportSource]
78+
* Where to import an automatic JSX runtime from.
79+
*
80+
* @typedef Imports
81+
* State of used identifiers from the automatic runtime.
82+
* @property {boolean | undefined} [fragment]
83+
* Symbol of `Fragment`.
84+
* @property {boolean | undefined} [jsx]
85+
* Symbol of `jsx`.
86+
* @property {boolean | undefined} [jsxs]
87+
* Symbol of `jsxs`.
88+
* @property {boolean | undefined} [jsxDEV]
89+
* Symbol of `jsxDEV`.
7190
*/
7291

7392
import {walk} from 'estree-walker'
@@ -79,6 +98,7 @@ const regex = /@(jsx|jsxFrag|jsxImportSource|jsxRuntime)\s+(\S+)/g
7998
* Turn JSX in `tree` into function calls: `<x />` -> `h('x')`!
8099
*
81100
* @template {Node} Tree
101+
* Node type.
82102
* @param {Tree} tree
83103
* Tree to transform.
84104
* @param {Options} [options={}]
@@ -91,7 +111,7 @@ export function buildJsx(tree, options = {}) {
91111
let automatic = options.runtime === 'automatic'
92112
/** @type {Annotations} */
93113
const annotations = {}
94-
/** @type {{fragment?: boolean, jsx?: boolean, jsxs?: boolean, jsxDEV?: boolean}} */
114+
/** @type {Imports} */
95115
const imports = {}
96116

97117
walk(tree, {
@@ -444,7 +464,6 @@ export function buildJsx(tree, options = {}) {
444464
parameters.unshift(name)
445465

446466
// Types of `estree-walker` are wrong
447-
// type-coverage:ignore-next-line
448467
this.replace(
449468
create(node, {
450469
type: 'CallExpression',

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ Comment form: `@jsxImportSource theSource`.
156156

157157
> 👉 **Note**: `/jsx-runtime` or `/jsx-dev-runtime` is appended to this provided
158158
> source.
159-
> In CJS, that can resolve to a file, as in `theSource/jsx-runtime.js`, but for
159+
> In CJS, that can resolve to a file (as in `theSource/jsx-runtime.js`), but for
160160
> ESM an export map needs to be set up to point to files:
161161
>
162162
> ```js

0 commit comments

Comments
 (0)