Skip to content

Commit d0ab8cd

Browse files
committed
Refactor types
1 parent e1dced0 commit d0ab8cd

File tree

8 files changed

+148
-84
lines changed

8 files changed

+148
-84
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
coverage/
55
node_modules/
66
yarn.lock
7+
!/lib/types.d.ts
8+
!/index.d.ts

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export type {Options, Runtime} from './lib/types.js'
2+
export {buildJsx} from './lib/index.js'

index.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1-
/**
2-
* @typedef {import('./lib/index.js').Options} Options
3-
* @typedef {import('./lib/index.js').Runtime} Runtime
4-
*/
5-
1+
// Note: types exposed from `index.d.ts`.
62
export {buildJsx} from './lib/index.js'

lib/index.js

Lines changed: 1 addition & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -14,84 +14,7 @@
1414
* Property,
1515
* SpreadElement
1616
* } from 'estree-jsx'
17-
*/
18-
19-
/**
20-
* @typedef {'automatic' | 'classic'} Runtime
21-
* How to transform JSX.
22-
*
23-
* @typedef Options
24-
* Configuration.
25-
*
26-
* > 👉 **Note**: you can also configure `runtime`, `importSource`, `pragma`,
27-
* > and `pragmaFrag` from within files through comments.
28-
* @property {Runtime | null | undefined} [runtime='classic']
29-
* Choose the runtime (default: `'classic'`).
30-
*
31-
* Comment form: `@jsxRuntime theRuntime`.
32-
* @property {string | null | undefined} [importSource='react']
33-
* Place to import `jsx`, `jsxs`, `jsxDEV`, and `Fragment` from, when the
34-
* effective runtime is automatic (default: `'react'`).
35-
*
36-
* Comment form: `@jsxImportSource theSource`.
37-
*
38-
* > 👉 **Note**: `/jsx-runtime` or `/jsx-dev-runtime` is appended to this
39-
* > provided source.
40-
* > In CJS, that can resolve to a file (as in `theSource/jsx-runtime.js`),
41-
* > but for ESM an export map needs to be set up to point to files:
42-
* >
43-
* > ```js
44-
* > // …
45-
* > "exports": {
46-
* > // …
47-
* > "./jsx-runtime": "./path/to/jsx-runtime.js",
48-
* > "./jsx-dev-runtime": "./path/to/jsx-runtime.js"
49-
* > // …
50-
* > ```
51-
* @property {string | null | undefined} [pragma='React.createElement']
52-
* Identifier or member expression to call when the effective runtime is
53-
* classic (default: `'React.createElement'`).
54-
*
55-
* Comment form: `@jsx identifier`.
56-
* @property {string | null | undefined} [pragmaFrag='React.Fragment']
57-
* Identifier or member expression to use as a symbol for fragments when the
58-
* effective runtime is classic (default: `'React.Fragment'`).
59-
*
60-
* Comment form: `@jsxFrag identifier`.
61-
* @property {boolean | null | undefined} [development=false]
62-
* When in the automatic runtime, whether to import
63-
* `theSource/jsx-dev-runtime.js`, use `jsxDEV`, and pass location info when
64-
* available (default: `false`).
65-
*
66-
* This helps debugging but adds a lot of code that you don’t want in
67-
* production.
68-
* @property {string | null | undefined} [filePath]
69-
* File path to the original source file (optional).
70-
*
71-
* Passed in location info to `jsxDEV` when using the automatic runtime with
72-
* `development: true`.
73-
*
74-
* @typedef Annotations
75-
* State where info from comments is gathered.
76-
* @property {string | undefined} [jsx]
77-
* JSX identifier (`pragma`).
78-
* @property {string | undefined} [jsxFrag]
79-
* JSX identifier of fragment (`pragmaFrag`).
80-
* @property {string | undefined} [jsxImportSource]
81-
* Where to import an automatic JSX runtime from.
82-
* @property {Runtime | undefined} [jsxRuntime]
83-
* Runtime.
84-
*
85-
* @typedef Imports
86-
* State of used identifiers from the automatic runtime.
87-
* @property {boolean | undefined} [fragment]
88-
* Symbol of `Fragment`.
89-
* @property {boolean | undefined} [jsx]
90-
* Symbol of `jsx`.
91-
* @property {boolean | undefined} [jsxs]
92-
* Symbol of `jsxs`.
93-
* @property {boolean | undefined} [jsxDEV]
94-
* Symbol of `jsxDEV`.
17+
* @import {Annotations, Imports, Options} from './types.js'
9518
*/
9619

9720
import {ok as assert} from 'devlop'

lib/types.d.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* How to transform JSX.
3+
*/
4+
export type Runtime = 'automatic' | 'classic'
5+
6+
/**
7+
* Configuration.
8+
*
9+
* > 👉 **Note**: you can also configure `runtime`, `importSource`, `pragma`,
10+
* > and `pragmaFrag` from within files through comments.
11+
*/
12+
export interface Options {
13+
/**
14+
* When in the automatic runtime, whether to import
15+
* `theSource/jsx-dev-runtime.js`, use `jsxDEV`, and pass location info when
16+
* available (default: `false`).
17+
*
18+
* This helps debugging but adds a lot of code that you don’t want in
19+
* production.
20+
*/
21+
development?: boolean | null | undefined
22+
/**
23+
* File path to the original source file (optional).
24+
*
25+
* Passed in location info to `jsxDEV` when using the automatic runtime with
26+
* `development: true`.
27+
*/
28+
filePath?: string | null | undefined
29+
/**
30+
* Place to import `jsx`, `jsxs`, `jsxDEV`, and `Fragment` from, when the
31+
* effective runtime is automatic (default: `'react'`).
32+
*
33+
* Comment form: `@jsxImportSource theSource`.
34+
*
35+
* > 👉 **Note**: `/jsx-runtime` or `/jsx-dev-runtime` is appended to this
36+
* > provided source.
37+
* > In CJS, that can resolve to a file (as in `theSource/jsx-runtime.js`),
38+
* > but for ESM an export map needs to be set up to point to files:
39+
* >
40+
* > ```js
41+
* > // …
42+
* > "exports": {
43+
* > // …
44+
* > "./jsx-runtime": "./path/to/jsx-runtime.js",
45+
* > "./jsx-dev-runtime": "./path/to/jsx-runtime.js"
46+
* > // …
47+
* > ```
48+
*/
49+
importSource?: string | null | undefined
50+
/**
51+
* Identifier or member expression to use as a symbol for fragments when the
52+
* effective runtime is classic (default: `'React.Fragment'`).
53+
*
54+
* Comment form: `@jsxFrag identifier`.
55+
*/
56+
pragmaFrag?: string | null | undefined
57+
/**
58+
* Identifier or member expression to call when the effective runtime is
59+
* classic (default: `'React.createElement'`).
60+
*
61+
* Comment form: `@jsx identifier`.
62+
*/
63+
pragma?: string | null | undefined
64+
/**
65+
* Choose the runtime (default: `'classic'`).
66+
*
67+
* Comment form: `@jsxRuntime theRuntime`.
68+
*/
69+
runtime?: Runtime | null | undefined
70+
}
71+
72+
/**
73+
* State where info from comments is gathered.
74+
*/
75+
export interface Annotations {
76+
/**
77+
* JSX identifier of fragment (`pragmaFrag`).
78+
*/
79+
jsxFrag?: string | undefined
80+
/**
81+
* Where to import an automatic JSX runtime from.
82+
*/
83+
jsxImportSource?: string | undefined
84+
/**
85+
* Runtime.
86+
*/
87+
jsxRuntime?: Runtime | undefined
88+
/**
89+
* JSX identifier (`pragma`).
90+
*/
91+
jsx?: string | undefined
92+
}
93+
94+
/**
95+
* State of used identifiers from the automatic runtime.
96+
*/
97+
export interface Imports {
98+
/**
99+
* Symbol of `Fragment`.
100+
*/
101+
fragment?: boolean | undefined
102+
/**
103+
* Symbol of `jsxDEV`.
104+
*/
105+
jsxDEV?: boolean | undefined
106+
/**
107+
* Symbol of `jsxs`.
108+
*/
109+
jsxs?: boolean | undefined
110+
/**
111+
* Symbol of `jsx`.
112+
*/
113+
jsx?: boolean | undefined
114+
}

lib/types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Note: types only.
2+
export {}

package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,31 @@
8585
"strict": true
8686
},
8787
"xo": {
88+
"overrides": [
89+
{
90+
"files": [
91+
"**/*.d.ts"
92+
],
93+
"rules": {
94+
"@typescript-eslint/array-type": [
95+
"error",
96+
{
97+
"default": "generic"
98+
}
99+
],
100+
"@typescript-eslint/ban-types": [
101+
"error",
102+
{
103+
"extendDefaults": true
104+
}
105+
],
106+
"@typescript-eslint/consistent-type-definitions": [
107+
"error",
108+
"interface"
109+
]
110+
}
111+
}
112+
],
88113
"prettier": true,
89114
"rules": {
90115
"unicorn/prefer-string-replace-all": "off"

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
"target": "es2022"
1212
},
1313
"exclude": ["coverage/", "node_modules/"],
14-
"include": ["**/*.js"]
14+
"include": ["**/*.js", "lib/types.d.ts", "index.d.ts"]
1515
}

0 commit comments

Comments
 (0)