Skip to content

Commit ac9112f

Browse files
committed
Refactor code-style
1 parent f0f317d commit ac9112f

File tree

4 files changed

+113
-86
lines changed

4 files changed

+113
-86
lines changed

index.js

Lines changed: 50 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {walk} from 'estree-walker'
22
import {name as isIdentifierName} from 'estree-util-is-identifier-name'
33

4-
var regex = /@(jsx|jsxFrag|jsxImportSource|jsxRuntime)\s+(\S+)/g
4+
const regex = /@(jsx|jsxFrag|jsxImportSource|jsxRuntime)\s+(\S+)/g
55

66
/**
77
* @typedef {import('estree-jsx').Node} Node
@@ -52,11 +52,11 @@ var regex = /@(jsx|jsxFrag|jsxImportSource|jsxRuntime)\s+(\S+)/g
5252
* @returns {T}
5353
*/
5454
export function buildJsx(tree, options = {}) {
55-
var automatic = options.runtime === 'automatic'
55+
let automatic = options.runtime === 'automatic'
5656
/** @type {Annotations} */
57-
var annotations = {}
57+
const annotations = {}
5858
/** @type {{fragment?: boolean, jsx?: boolean, jsxs?: boolean}} */
59-
var imports = {}
59+
const imports = {}
6060

6161
walk(tree, {enter, leave})
6262

@@ -70,20 +70,16 @@ export function buildJsx(tree, options = {}) {
7070
* @param {Node} node
7171
*/
7272
function enter(node) {
73-
/** @type {Comment[]} */
74-
var comments
75-
/** @type {number} */
76-
var index
77-
/** @type {RegExpMatchArray} */
78-
var match
79-
8073
if (node.type === 'Program') {
81-
comments = node.comments || []
82-
index = -1
74+
const comments = node.comments || []
75+
let index = -1
8376

8477
while (++index < comments.length) {
8578
regex.lastIndex = 0
8679

80+
/** @type {RegExpMatchArray} */
81+
let match
82+
8783
while ((match = regex.exec(comments[index].value))) {
8884
annotations[match[1]] = match[2]
8985
}
@@ -125,40 +121,9 @@ export function buildJsx(tree, options = {}) {
125121
*/
126122
// eslint-disable-next-line complexity
127123
function leave(node) {
128-
/** @type {Array.<Expression|SpreadElement>} */
129-
var parameters = []
130-
/** @type {Array.<Expression>} */
131-
var children = []
132-
/** @type {Array.<Expression>} */
133-
var objects = []
134-
/** @type {Array.<Property>} */
135-
var fields = []
136-
var index = -1
137-
/** @type {JSXExpressionContainer|JSXElement|JSXFragment|JSXText|JSXSpreadChild} */
138-
var child
139-
/** @type {MemberExpression|Literal|Identifier} */
140-
var name
141-
/** @type {Expression} */
142-
var props
143-
/** @type {Array<JSXAttribute | JSXSpreadAttribute>} */
144-
var attributes
145-
/** @type {JSXAttribute | JSXSpreadAttribute} */
146-
var attribute
147-
/** @type {boolean} */
148-
var spread
149-
/** @type {Expression} */
150-
var key
151-
/** @type {MemberExpression|Literal|Identifier} */
152-
var callee
153-
/** @type {Array<ImportSpecifier>} */
154-
var specifiers
155-
/** @type {Property} */
156-
var prop
157-
/** @type {string} */
158-
var value
159-
160124
if (node.type === 'Program') {
161-
specifiers = []
125+
/** @type {Array<ImportSpecifier>} */
126+
const specifiers = []
162127

163128
if (imports.fragment) {
164129
specifiers.push({
@@ -202,17 +167,21 @@ export function buildJsx(tree, options = {}) {
202167
return
203168
}
204169

170+
/** @type {Array.<Expression>} */
171+
const children = []
172+
let index = -1
173+
205174
// Figure out `children`.
206175
while (++index < node.children.length) {
207-
child = node.children[index]
176+
const child = node.children[index]
208177

209178
if (child.type === 'JSXExpressionContainer') {
210179
// Ignore empty expressions.
211180
if (child.expression.type !== 'JSXEmptyExpression') {
212181
children.push(child.expression)
213182
}
214183
} else if (child.type === 'JSXText') {
215-
value = child.value
184+
const value = child.value
216185
// Replace tabs w/ spaces.
217186
.replace(/\t/g, ' ')
218187
// Use line feeds, drop spaces around them.
@@ -235,6 +204,17 @@ export function buildJsx(tree, options = {}) {
235204
}
236205
}
237206

207+
/** @type {MemberExpression|Literal|Identifier} */
208+
let name
209+
/** @type {Array.<Property>} */
210+
let fields = []
211+
/** @type {Array.<Expression>} */
212+
const objects = []
213+
/** @type {Array.<Expression|SpreadElement>} */
214+
let parameters = []
215+
/** @type {Expression} */
216+
let key
217+
238218
// Do the stuff needed for elements.
239219
if (node.type === 'JSXElement') {
240220
name = toIdentifier(node.openingElement.name)
@@ -245,13 +225,15 @@ export function buildJsx(tree, options = {}) {
245225
name = create(name, {type: 'Literal', value: name.name})
246226
}
247227

248-
attributes = node.openingElement.attributes
249-
index = -1
228+
/** @type {boolean} */
229+
let spread
230+
const attributes = node.openingElement.attributes
231+
let index = -1
250232

251233
// Place props in the right order, because we might have duplicates
252234
// in them and what’s spread in.
253235
while (++index < attributes.length) {
254-
attribute = attributes[index]
236+
const attribute = attributes[index]
255237

256238
if (attribute.type === 'JSXSpreadAttribute') {
257239
if (fields.length > 0) {
@@ -262,7 +244,7 @@ export function buildJsx(tree, options = {}) {
262244
objects.push(attribute.argument)
263245
spread = true
264246
} else {
265-
prop = toProperty(attribute)
247+
const prop = toProperty(attribute)
266248

267249
if (
268250
automatic &&
@@ -315,6 +297,11 @@ export function buildJsx(tree, options = {}) {
315297
objects.push({type: 'ObjectExpression', properties: fields})
316298
}
317299

300+
/** @type {Expression} */
301+
let props
302+
/** @type {MemberExpression|Literal|Identifier} */
303+
let callee
304+
318305
if (objects.length > 1) {
319306
// Don’t mutate the first object, shallow clone instead.
320307
if (objects[0].type !== 'ObjectExpression') {
@@ -377,7 +364,7 @@ export function buildJsx(tree, options = {}) {
377364
*/
378365
function toProperty(node) {
379366
/** @type {Expression} */
380-
var value
367+
let value
381368

382369
if (node.value) {
383370
if (node.value.type === 'JSXExpressionContainer') {
@@ -416,14 +403,12 @@ function toProperty(node) {
416403
*/
417404
function toIdentifier(node) {
418405
/** @type {MemberExpression|Identifier|Literal} */
419-
var replace
420-
/** @type {MemberExpression|Identifier|Literal} */
421-
var id
406+
let replace
422407

423408
if (node.type === 'JSXMemberExpression') {
424409
// `property` is always a `JSXIdentifier`, but it could be something that
425410
// isn’t an ES identifier name.
426-
id = toIdentifier(node.property)
411+
const id = toIdentifier(node.property)
427412
replace = {
428413
type: 'MemberExpression',
429414
object: toIdentifier(node.object),
@@ -452,15 +437,14 @@ function toIdentifier(node) {
452437
* @returns {Identifier|Literal|MemberExpression}
453438
*/
454439
function toMemberExpression(id) {
455-
var identifiers = id.split('.')
456-
var index = -1
440+
const identifiers = id.split('.')
441+
let index = -1
457442
/** @type {Identifier|Literal|MemberExpression} */
458-
var result
459-
/** @type {Identifier|Literal} */
460-
var prop
443+
let result
461444

462445
while (++index < identifiers.length) {
463-
prop = isIdentifierName(identifiers[index])
446+
/** @type {Identifier|Literal} */
447+
const prop = isIdentifierName(identifiers[index])
464448
? {type: 'Identifier', name: identifiers[index]}
465449
: {type: 'Literal', value: identifiers[index]}
466450
result = index
@@ -484,13 +468,11 @@ function toMemberExpression(id) {
484468
* @returns {T}
485469
*/
486470
function create(from, node) {
487-
var fields = ['start', 'end', 'loc', 'range', 'comments']
488-
var index = -1
489-
/** @type {string} */
490-
var field
471+
const fields = ['start', 'end', 'loc', 'range', 'comments']
472+
let index = -1
491473

492474
while (++index < fields.length) {
493-
field = fields[index]
475+
const field = fields[index]
494476
if (field in from) {
495477
node[field] = from[field]
496478
}

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,7 @@
8080
"trailingComma": "none"
8181
},
8282
"xo": {
83-
"prettier": true,
84-
"rules": {
85-
"no-var": "off",
86-
"prefer-arrow-callback": "off"
87-
}
83+
"prettier": true
8884
},
8985
"remarkConfig": {
9086
"plugins": [

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ console.log(
5555
And our script, `example.js`, looks as follows:
5656

5757
```js
58-
import fs from 'fs'
58+
import fs from 'node:fs'
5959
import {Parser} from 'acorn'
6060
import jsx from 'acorn-jsx'
6161
import {generate} from 'astring'
6262
import {buildJsx} from 'estree-util-build-jsx'
6363

64-
var doc = fs.readFileSync('example.jsx')
64+
const doc = fs.readFileSync('example.jsx')
6565

66-
var tree = Parser.extend(jsx()).parse(doc, {
66+
const tree = Parser.extend(jsx()).parse(doc, {
6767
sourceType: 'module',
6868
ecmaVersion: 2020
6969
})

0 commit comments

Comments
 (0)