Skip to content

Commit 4b8ef18

Browse files
committed
Update tsconfig.json
1 parent fadbe77 commit 4b8ef18

File tree

5 files changed

+126
-35
lines changed

5 files changed

+126
-35
lines changed

lib/index.js

+37-19
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,39 @@
22
* @typedef {import('estree-jsx').Program} Program
33
* @typedef {typeof import('source-map').SourceMapGenerator} SourceMapGenerator
44
* @typedef {import('./types.js').Handlers} Handlers
5-
*
5+
*/
6+
7+
/**
68
* @typedef BaseFields
7-
* @property {Handlers} [handlers]
9+
* @property {Handlers | null | undefined} [handlers]
810
* Object mapping node types to functions handling the corresponding nodes.
911
*
1012
* @typedef SourceMapFieldsWithoutSourceMapGenerator
11-
* @property {undefined} [SourceMapGenerator]
13+
* @property {null | undefined} [SourceMapGenerator]
1214
* Generate a source map by passing a `SourceMapGenerator` from `source-map`
1315
* in.
14-
* @property {undefined} [filePath]
16+
* @property {null | undefined} [filePath]
1517
* Path to input file.
1618
*
1719
* @typedef SourceMapFieldsWithSourceMapGenerator
1820
* @property {SourceMapGenerator} SourceMapGenerator
1921
* Generate a source map by passing a `SourceMapGenerator` from `source-map`
2022
* in.
21-
* @property {string} [filePath]
23+
* @property {string | null | undefined} [filePath]
24+
* Path to input file.
25+
*
26+
* @typedef SourceMapFieldsMaybeSourceMapGenerator
27+
* @property {SourceMapGenerator | null | undefined} SourceMapGenerator
28+
* Generate a source map by passing a `SourceMapGenerator` from `source-map`
29+
* in.
30+
* @property {string | null | undefined} [filePath]
2231
* Path to input file.
2332
*
2433
* @typedef {BaseFields & SourceMapFieldsWithoutSourceMapGenerator} OptionsWithoutSourceMapGenerator
2534
* @typedef {BaseFields & SourceMapFieldsWithSourceMapGenerator} OptionsWithSourceMapGenerator
35+
* @typedef {BaseFields & SourceMapFieldsMaybeSourceMapGenerator} OptionsWithMaybeMapGenerator
2636
*
27-
* @typedef {OptionsWithoutSourceMapGenerator|OptionsWithSourceMapGenerator} Options
37+
* @typedef {OptionsWithMaybeMapGenerator} Options
2838
* Configuration (optional).
2939
*
3040
* @typedef Map
@@ -33,8 +43,8 @@
3343
* @property {number} version
3444
* @property {Array<string>} sources
3545
* @property {Array<string>} names
36-
* @property {string|undefined} [sourceRoot]
37-
* @property {Array<string>|undefined} [sourcesContent]
46+
* @property {string | null | undefined} [sourceRoot]
47+
* @property {Array<string> | null | undefined} [sourcesContent]
3848
* @property {string} mappings
3949
* @property {string} file
4050
*
@@ -48,20 +58,27 @@
4858
*
4959
* @typedef ResultFieldsWithSourceMapGenerator
5060
* @property {Map} map
51-
* Source map as (parsed) JSON, if `SourceMapGenerator` is passed.
61+
* Source map as (parsed) JSON, if `SourceMapGenerator` is not passed.
5262
*
53-
* @typedef ResultFieldsRegardlessOfSourceMapGenerator
54-
* @property {Map|undefined} map
55-
* Source map as (parsed) JSON, if `SourceMapGenerator` is passed.
63+
* @typedef ResultFieldsMaybeSourceMapGenerator
64+
* @property {Map | undefined} map
65+
* Source map as (parsed) JSON, if `SourceMapGenerator` might be passed.
5666
*
5767
* @typedef {BaseResultFields & ResultFieldsWithoutSourceMapGenerator} ResultWithoutSourceMapGenerator
5868
* @typedef {BaseResultFields & ResultFieldsWithSourceMapGenerator} ResultWithSourceMapGenerator
59-
* @typedef {BaseResultFields & ResultFieldsRegardlessOfSourceMapGenerator} ResultRegardlessOfSourceMapGenerator
69+
* @typedef {BaseResultFields & ResultFieldsMaybeSourceMapGenerator} ResultMaybeSourceMapGenerator
6070
*
61-
* @typedef {ResultRegardlessOfSourceMapGenerator} Result
71+
* @typedef {ResultMaybeSourceMapGenerator} Result
6272
*/
6373

64-
import {GENERATOR, generate} from 'astring'
74+
// @ts-expect-error: `astring` has broken types.
75+
import * as astring from 'astring'
76+
77+
/** @type {Handlers} */
78+
const GENERATOR = astring.GENERATOR
79+
80+
/** @type {(node: Program, options: unknown) => string} */
81+
const generate = astring.generate
6582

6683
/**
6784
* Estree (and esast) utility to serialize estrees as JavaScript.
@@ -80,17 +97,18 @@ export const toJs =
8097
/**
8198
* @type {(
8299
* ((value: Program, options: OptionsWithSourceMapGenerator) => ResultWithSourceMapGenerator) &
83-
* ((value: Program, options?: OptionsWithoutSourceMapGenerator) => ResultWithoutSourceMapGenerator)
100+
* ((value: Program, options: OptionsWithMaybeMapGenerator) => ResultMaybeSourceMapGenerator) &
101+
* ((value: Program, options?: OptionsWithoutSourceMapGenerator | null | undefined) => ResultWithoutSourceMapGenerator)
84102
* )}
85103
*/
86104
(
87105
/**
88106
* @param {Program} tree
89-
* @param {Options} [options={}]
107+
* @param {Options | null | undefined} [options]
90108
* @returns {Result}
91109
*/
92-
function (tree, options = {}) {
93-
const {SourceMapGenerator, filePath, handlers} = options
110+
function (tree, options) {
111+
const {SourceMapGenerator, filePath, handlers} = options || {}
94112
const sourceMap = SourceMapGenerator
95113
? new SourceMapGenerator({file: filePath || '<unknown>.js'})
96114
: undefined

lib/jsx.js

+57-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* @typedef {import('estree-jsx').JSXClosingElement} JSXClosingElement
44
* @typedef {import('estree-jsx').JSXClosingFragment} JSXClosingFragment
55
* @typedef {import('estree-jsx').JSXElement} JSXElement
6-
* @typedef {import('estree-jsx').JSXEmptyExpression} JSXEmptyExpression
76
* @typedef {import('estree-jsx').JSXExpressionContainer} JSXExpressionContainer
87
* @typedef {import('estree-jsx').JSXFragment} JSXFragment
98
* @typedef {import('estree-jsx').JSXIdentifier} JSXIdentifier
@@ -13,7 +12,6 @@
1312
* @typedef {import('estree-jsx').JSXOpeningFragment} JSXOpeningFragment
1413
* @typedef {import('estree-jsx').JSXSpreadAttribute} JSXSpreadAttribute
1514
* @typedef {import('estree-jsx').JSXText} JSXText
16-
* @typedef {import('./types.js').Handler} Handler
1715
* @typedef {import('./types.js').Generator} Generator
1816
* @typedef {import('./types.js').State} State
1917
*/
@@ -41,9 +39,13 @@ export const jsx = {
4139
* `attr={1}`
4240
*
4341
* @this {Generator}
42+
* `astring` generator.
4443
* @param {JSXAttribute} node
44+
* Node to serialize.
4545
* @param {State} state
46+
* Info passed around.
4647
* @returns {void}
48+
* Nothing.
4749
*/
4850
function JSXAttribute(node, state) {
4951
this[node.name.type](node.name, state)
@@ -67,9 +69,13 @@ function JSXAttribute(node, state) {
6769
* `</div>`
6870
*
6971
* @this {Generator}
72+
* `astring` generator.
7073
* @param {JSXClosingElement} node
74+
* Node to serialize.
7175
* @param {State} state
76+
* Info passed around.
7277
* @returns {void}
78+
* Nothing.
7379
*/
7480
function JSXClosingElement(node, state) {
7581
state.write('</')
@@ -81,9 +87,13 @@ function JSXClosingElement(node, state) {
8187
* `</>`
8288
*
8389
* @this {Generator}
90+
* `astring` generator.
8491
* @param {JSXClosingFragment} node
92+
* Node to serialize.
8593
* @param {State} state
94+
* Info passed around.
8695
* @returns {void}
96+
* Nothing.
8797
*/
8898
function JSXClosingFragment(node, state) {
8999
state.write('</>', node)
@@ -94,9 +104,13 @@ function JSXClosingFragment(node, state) {
94104
* `<div></div>`
95105
*
96106
* @this {Generator}
107+
* `astring` generator.
97108
* @param {JSXElement} node
109+
* Node to serialize.
98110
* @param {State} state
111+
* Info passed around.
99112
* @returns {void}
113+
* Nothing.
100114
*/
101115
function JSXElement(node, state) {
102116
let index = -1
@@ -126,17 +140,23 @@ function JSXElement(node, state) {
126140
* `{}` (always in a `JSXExpressionContainer`, which does the curlies)
127141
*
128142
* @this {Generator}
143+
* `astring` generator.
129144
* @returns {void}
145+
* Nothing.
130146
*/
131147
function JSXEmptyExpression() {}
132148

133149
/**
134150
* `{expression}`
135151
*
136152
* @this {Generator}
153+
* `astring` generator.
137154
* @param {JSXExpressionContainer} node
155+
* Node to serialize.
138156
* @param {State} state
157+
* Info passed around.
139158
* @returns {void}
159+
* Nothing.
140160
*/
141161
function JSXExpressionContainer(node, state) {
142162
state.write('{')
@@ -148,9 +168,13 @@ function JSXExpressionContainer(node, state) {
148168
* `<></>`
149169
*
150170
* @this {Generator}
171+
* `astring` generator.
151172
* @param {JSXFragment} node
173+
* Node to serialize.
152174
* @param {State} state
175+
* Info passed around.
153176
* @returns {void}
177+
* Nothing.
154178
*/
155179
function JSXFragment(node, state) {
156180
let index = -1
@@ -178,9 +202,13 @@ function JSXFragment(node, state) {
178202
* `div`
179203
*
180204
* @this {Generator}
205+
* `astring` generator.
181206
* @param {JSXIdentifier} node
207+
* Node to serialize.
182208
* @param {State} state
209+
* Info passed around.
183210
* @returns {void}
211+
* Nothing.
184212
*/
185213
function JSXIdentifier(node, state) {
186214
state.write(node.name, node)
@@ -190,9 +218,13 @@ function JSXIdentifier(node, state) {
190218
* `member.expression`
191219
*
192220
* @this {Generator}
221+
* `astring` generator.
193222
* @param {JSXMemberExpression} node
223+
* Node to serialize.
194224
* @param {State} state
225+
* Info passed around.
195226
* @returns {void}
227+
* Nothing.
196228
*/
197229
function JSXMemberExpression(node, state) {
198230
this[node.object.type](node.object, state)
@@ -204,9 +236,13 @@ function JSXMemberExpression(node, state) {
204236
* `ns:name`
205237
*
206238
* @this {Generator}
239+
* `astring` generator.
207240
* @param {JSXNamespacedName} node
241+
* Node to serialize.
208242
* @param {State} state
243+
* Info passed around.
209244
* @returns {void}
245+
* Nothing.
210246
*/
211247
function JSXNamespacedName(node, state) {
212248
this[node.namespace.type](node.namespace, state)
@@ -218,9 +254,13 @@ function JSXNamespacedName(node, state) {
218254
* `<div>`
219255
*
220256
* @this {Generator}
257+
* `astring` generator.
221258
* @param {JSXOpeningElement} node
259+
* Node to serialize.
222260
* @param {State} state
261+
* Info passed around.
223262
* @returns {void}
263+
* Nothing.
224264
*/
225265
function JSXOpeningElement(node, state) {
226266
let index = -1
@@ -242,9 +282,13 @@ function JSXOpeningElement(node, state) {
242282
* `<>`
243283
*
244284
* @this {Generator}
285+
* `astring` generator.
245286
* @param {JSXOpeningFragment} node
287+
* Node to serialize.
246288
* @param {State} state
289+
* Info passed around.
247290
* @returns {void}
291+
* Nothing.
248292
*/
249293
function JSXOpeningFragment(node, state) {
250294
state.write('<>', node)
@@ -254,9 +298,13 @@ function JSXOpeningFragment(node, state) {
254298
* `{...argument}`
255299
*
256300
* @this {Generator}
301+
* `astring` generator.
257302
* @param {JSXSpreadAttribute} node
303+
* Node to serialize.
258304
* @param {State} state
305+
* Info passed around.
259306
* @returns {void}
307+
* Nothing.
260308
*/
261309
function JSXSpreadAttribute(node, state) {
262310
state.write('{')
@@ -269,9 +317,13 @@ function JSXSpreadAttribute(node, state) {
269317
* `!`
270318
*
271319
* @this {Generator}
320+
* `astring` generator.
272321
* @param {JSXText} node
322+
* Node to serialize.
273323
* @param {State} state
324+
* Info passed around.
274325
* @returns {void}
326+
* Nothing.
275327
*/
276328
function JSXText(node, state) {
277329
state.write(
@@ -290,6 +342,7 @@ function JSXText(node, state) {
290342

291343
/**
292344
* Make sure that character references don’t pop up.
345+
*
293346
* For example, the text `&copy;` should stay that way, and not turn into `©`.
294347
* We could encode all `&` (easy but verbose) or look for actual valid
295348
* references (complex but cleanest output).
@@ -298,7 +351,9 @@ function JSXText(node, state) {
298351
* are for the named references.
299352
*
300353
* @param {string} value
354+
* Value to encode.
301355
* @returns {string}
356+
* Encoded value.
302357
*/
303358
function encodeJsx(value) {
304359
return value.replace(/&(?=[#a-z])/gi, '&amp;')

lib/types.js

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
/**
22
* @typedef {import('estree-jsx').Node} Node
3-
* @typedef {import('estree-jsx').Program} Program
4-
* @typedef {Omit<import('astring').State, 'write'> & {write: ((code: string, node?: Node) => void)}} State
5-
* @typedef {Node['type']} NodeType
6-
* @typedef {{[K in NodeType]: Handler}} Generator
3+
* @typedef {import('source-map').Mapping} Mapping
4+
*/
5+
6+
// To do: `astring` types are broken.
7+
// Either `import('astring').State` if everything is fixed, or:
8+
// `Omit<import('astring').State, 'write'> & {write: ((code: string, node?: Node) => void)}`
9+
/**
10+
* @typedef State
11+
* @property {string} output
12+
* @property {(code: string, node?: Node) => void} write
13+
* @property {boolean} writeComments
14+
* @property {string} indent
15+
* @property {string} lineEnd
16+
* @property {number} indentLevel
17+
* @property {number | undefined} [line]
18+
* @property {number | undefined} [column]
19+
* @property {number | undefined} [lineEndSize]
20+
* @property {Mapping | undefined} [mapping]
21+
*/
22+
23+
/**
24+
* @typedef {Record<Node['type'], Handler>} Generator
725
* @typedef {Partial<Generator>} Handlers
826
*
927
* @callback Handler

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,14 @@
4747
"prettier": "^2.0.0",
4848
"remark-cli": "^11.0.0",
4949
"remark-preset-wooorm": "^9.0.0",
50-
"rimraf": "^3.0.0",
5150
"tape": "^5.0.0",
5251
"type-coverage": "^2.0.0",
5352
"typescript": "^4.0.0",
5453
"xo": "^0.53.0"
5554
},
5655
"scripts": {
5756
"prepack": "npm run build && npm run format",
58-
"build": "rimraf \"{lib,test}/**/*.d.ts\" \"*.d.ts\" && tsc && type-coverage",
57+
"build": "tsc --build --clean && tsc --build && type-coverage",
5958
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
6059
"test-api": "node --conditions development test/index.js",
6160
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api",

0 commit comments

Comments
 (0)