Skip to content

Commit 9db76d9

Browse files
committed
Refactor code-style
1 parent cbad1b6 commit 9db76d9

File tree

7 files changed

+285
-257
lines changed

7 files changed

+285
-257
lines changed

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
2-
* @typedef {import('./lib/types.js').Handler} Handler
3-
* @typedef {import('./lib/types.js').Handlers} Handlers
2+
* @typedef {import('./lib/index.js').Handler} Handler
3+
* @typedef {import('./lib/index.js').Handlers} Handlers
44
* @typedef {import('./lib/index.js').Options} Options
55
* @typedef {import('./lib/index.js').Result} Result
6-
* @typedef {import('./lib/types.js').State} State
6+
* @typedef {import('./lib/index.js').State} State
77
*/
88

99
export {toJs} from './lib/index.js'

lib/index.js

Lines changed: 93 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,145 @@
11
/**
2+
* @typedef {import('astring').State} State
3+
* @typedef {import('estree-jsx').Node} Nodes
24
* @typedef {import('estree-jsx').Program} Program
35
* @typedef {typeof import('source-map').SourceMapGenerator} SourceMapGenerator
46
* @typedef {import('source-map').RawSourceMap} Map
5-
* @typedef {import('./types.js').Handlers} Handlers
67
*/
78

89
/**
9-
* @typedef BaseFields
10+
* @typedef {Record<Nodes['type'], Handler>} Generator
11+
*
12+
* @callback Handler
13+
* Handle a particular node.
14+
* @param {Generator} this
15+
* `astring` generator.
16+
* @param {any} node
17+
* Node to serialize.
18+
* @param {State} state
19+
* Info passed around.
20+
* @returns {undefined}
21+
* Nothing.
22+
*
23+
* @typedef {Partial<import('astring').Generator>} Handlers
24+
*/
25+
26+
/**
27+
* @typedef {OptionsWithMaybeMapGenerator} Options
28+
* Configuration.
29+
*
30+
* @typedef OptionsFieldsBase
31+
* Base shared option fields.
1032
* @property {Handlers | null | undefined} [handlers]
1133
* Object mapping node types to functions handling the corresponding nodes.
1234
*
13-
* @typedef SourceMapFieldsWithoutSourceMapGenerator
35+
* @typedef OptionsFieldsWithoutSourceMapGenerator
36+
* Extra option fields where there’s definitely no source map generator.
1437
* @property {null | undefined} [SourceMapGenerator]
1538
* Generate a source map by passing a `SourceMapGenerator` from `source-map`
16-
* in.
17-
* This works if there is positional info on nodes.
39+
* in; this works if there is positional info on nodes.
1840
* @property {null | undefined} [filePath]
19-
* Path to input file.
20-
* Only used in source map.
41+
* Path to input file; only used in source map.
2142
*
22-
* @typedef SourceMapFieldsWithSourceMapGenerator
43+
* @typedef OptionsFieldsWithSourceMapGenerator
44+
* Extra option fields where there’s definitely a source map generator.
2345
* @property {SourceMapGenerator} SourceMapGenerator
2446
* Generate a source map by passing a `SourceMapGenerator` from `source-map`
25-
* in.
26-
* This works if there is positional info on nodes.
47+
* in; this works if there is positional info on nodes.
2748
* @property {string | null | undefined} [filePath]
28-
* Path to input file.
29-
* Only used in source map.
49+
* Path to input file; only used in source map.
3050
*
31-
* @typedef SourceMapFieldsMaybeSourceMapGenerator
32-
* @property {SourceMapGenerator | null | undefined} SourceMapGenerator
51+
* @typedef OptionsFieldsMaybeSourceMapGenerator
52+
* Extra option fields where there may or may not be a source map generator.
53+
* @property {SourceMapGenerator | null | undefined} [SourceMapGenerator]
3354
* Generate a source map by passing a `SourceMapGenerator` from `source-map`
34-
* in.
35-
* This works if there is positional info on nodes.
55+
* in; this works if there is positional info on nodes.
3656
* @property {string | null | undefined} [filePath]
37-
* Path to input file.
38-
* Only used in source map.
57+
* Path to input file; only used in source map.
3958
*
40-
* @typedef {BaseFields & SourceMapFieldsWithoutSourceMapGenerator} OptionsWithoutSourceMapGenerator
41-
* @typedef {BaseFields & SourceMapFieldsWithSourceMapGenerator} OptionsWithSourceMapGenerator
42-
* @typedef {BaseFields & SourceMapFieldsMaybeSourceMapGenerator} OptionsWithMaybeMapGenerator
59+
* @typedef {OptionsFieldsBase & OptionsFieldsWithoutSourceMapGenerator} OptionsWithoutSourceMapGenerator
60+
* Options where there’s definitely no source map generator.
61+
* @typedef {OptionsFieldsBase & OptionsFieldsWithSourceMapGenerator} OptionsWithSourceMapGenerator
62+
* Options where there’s definitely a source map generator.
63+
* @typedef {OptionsFieldsBase & OptionsFieldsMaybeSourceMapGenerator} OptionsWithMaybeMapGenerator
64+
* Options where there may or may not be a source map generator.
4365
*
44-
* @typedef {OptionsWithMaybeMapGenerator} Options
45-
* Configuration (optional).
66+
* @typedef {ResultWithMaybeSourceMapGenerator} Result
67+
* Result.
4668
*
47-
* @typedef BaseResultFields
69+
* @typedef ResultFieldsBase
70+
* Base shared result fields.
4871
* @property {string} value
4972
* Serialized JavaScript.
5073
*
5174
* @typedef ResultFieldsWithoutSourceMapGenerator
75+
* Extra result fields where there’s definitely no source map generator.
5276
* @property {undefined} map
5377
* Source map as (parsed) JSON, if `SourceMapGenerator` is passed.
5478
*
5579
* @typedef ResultFieldsWithSourceMapGenerator
80+
* Extra result fields where there’s definitely a source map generator.
5681
* @property {Map} map
5782
* Source map as (parsed) JSON, if `SourceMapGenerator` is not passed.
5883
*
5984
* @typedef ResultFieldsMaybeSourceMapGenerator
85+
* Extra result fields where there may or may not be a source map generator.
6086
* @property {Map | undefined} map
6187
* Source map as (parsed) JSON, if `SourceMapGenerator` might be passed.
6288
*
63-
* @typedef {BaseResultFields & ResultFieldsWithoutSourceMapGenerator} ResultWithoutSourceMapGenerator
64-
* @typedef {BaseResultFields & ResultFieldsWithSourceMapGenerator} ResultWithSourceMapGenerator
65-
* @typedef {BaseResultFields & ResultFieldsMaybeSourceMapGenerator} ResultMaybeSourceMapGenerator
66-
*
67-
* @typedef {ResultMaybeSourceMapGenerator} Result
89+
* @typedef {ResultFieldsBase & ResultFieldsWithoutSourceMapGenerator} ResultWithoutSourceMapGenerator
90+
* Result where there’s definitely no source map generator.
91+
* @typedef {ResultFieldsBase & ResultFieldsWithSourceMapGenerator} ResultWithSourceMapGenerator
92+
* Result where there’s definitely a source map generator.
93+
* @typedef {ResultFieldsBase & ResultFieldsMaybeSourceMapGenerator} ResultWithMaybeSourceMapGenerator
94+
* Result where there may or may not be a source map generator.
6895
*/
6996

70-
import * as astring from 'astring'
97+
import {GENERATOR, generate} from 'astring'
7198

72-
/** @type {Handlers} */
73-
const GENERATOR = astring.GENERATOR
74-
75-
const generate = astring.generate
99+
/** @type {Options} */
100+
const emptyOptions = {}
76101

77102
/**
78103
* Serialize an estree as JavaScript.
79104
*
80-
* @param tree
105+
* @overload
106+
* @param {Program} tree
107+
* @param {OptionsWithSourceMapGenerator} options
108+
* @returns {ResultWithSourceMapGenerator}
109+
*
110+
* @overload
111+
* @param {Program} tree
112+
* @param {OptionsWithMaybeMapGenerator} options
113+
* @returns {ResultWithMaybeSourceMapGenerator}
114+
*
115+
* @overload
116+
* @param {Program} tree
117+
* @param {OptionsWithoutSourceMapGenerator | null | undefined} [options]
118+
* @returns {ResultWithoutSourceMapGenerator}
119+
*
120+
* @param {Program} tree
81121
* Estree (esast).
82-
* @param options
122+
* @param {Options | null | undefined} [options]
83123
* Configuration (optional).
84-
* @returns
124+
* @returns {Result}
85125
* Result, optionally with source map.
86126
*/
87-
export const toJs =
88-
/**
89-
* @type {(
90-
* ((value: Program, options: OptionsWithSourceMapGenerator) => ResultWithSourceMapGenerator) &
91-
* ((value: Program, options: OptionsWithMaybeMapGenerator) => ResultMaybeSourceMapGenerator) &
92-
* ((value: Program, options?: OptionsWithoutSourceMapGenerator | null | undefined) => ResultWithoutSourceMapGenerator)
93-
* )}
94-
*/
95-
(
96-
/**
97-
* @param {Program} tree
98-
* @param {Options | null | undefined} [options]
99-
* @returns {Result}
100-
*/
101-
function (tree, options) {
102-
const {SourceMapGenerator, filePath, handlers} = options || {}
103-
const sourceMap = SourceMapGenerator
104-
? new SourceMapGenerator({file: filePath || '<unknown>.js'})
105-
: undefined
127+
export function toJs(tree, options) {
128+
const {SourceMapGenerator, filePath, handlers} = options || emptyOptions
129+
const sourceMap = SourceMapGenerator
130+
? new SourceMapGenerator({file: filePath || '<unknown>.js'})
131+
: undefined
106132

107-
const value = generate(tree, {
108-
comments: true,
109-
// @ts-expect-error: assume all handlers are defined.
110-
generator: {...GENERATOR, ...handlers},
111-
sourceMap
112-
})
113-
const map = sourceMap ? sourceMap.toJSON() : undefined
114-
115-
return {value, map}
133+
const value = generate(
134+
tree,
135+
// @ts-expect-error: `sourceMap` can be undefined, `astring` types are buggy.
136+
{
137+
comments: true,
138+
generator: {...GENERATOR, ...handlers},
139+
sourceMap: sourceMap || undefined
116140
}
117141
)
142+
const map = sourceMap ? sourceMap.toJSON() : undefined
143+
144+
return {value, map}
145+
}

0 commit comments

Comments
 (0)