Skip to content

Commit 35d39ba

Browse files
committed
Refactor code-style
1 parent 4386782 commit 35d39ba

File tree

4 files changed

+157
-135
lines changed

4 files changed

+157
-135
lines changed

index.js

Lines changed: 69 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* @typedef {ReturnType<find>} Info
4444
* @typedef {'html'|'svg'} Space
4545
*
46-
* @typedef {(node: Node, context: Context) => EstreeJsxChild?} Handle
46+
* @typedef {(node: any, context: Context) => EstreeJsxChild?} Handle
4747
*
4848
* @typedef Options
4949
* @property {Space} [space='html']
@@ -69,17 +69,17 @@ import style from 'style-to-object'
6969
import {position} from 'unist-util-position'
7070
import {zwitch} from 'zwitch'
7171

72-
var own = {}.hasOwnProperty
73-
var push = [].push
72+
const own = {}.hasOwnProperty
73+
const push = [].push
7474

7575
/**
76-
* @param {Node} tree
76+
* @param {Node|MDXJsxAttributeValueExpression|MDXJsxAttribute|MDXJsxExpressionAttribute|MDXJsxFlowElement|MDXJsxTextElement|MDXFlowExpression|MDXTextExpression} tree
7777
* @param {Options} options
7878
* @returns {EstreeProgram}
7979
*/
8080
export function toEstree(tree, options = {}) {
8181
/** @type {Context} */
82-
var context = {
82+
const context = {
8383
schema: options.space === 'svg' ? svg : html,
8484
comments: [],
8585
esm: [],
@@ -104,8 +104,8 @@ export function toEstree(tree, options = {}) {
104104
)
105105
})
106106
}
107-
var result = context.handle(tree, context)
108-
var body = context.esm
107+
let result = context.handle(tree, context)
108+
const body = context.esm
109109

110110
if (result) {
111111
if (result.type !== 'JSXFragment' && result.type !== 'JSXElement') {
@@ -151,7 +151,7 @@ function ignore() {}
151151
* @returns {EstreeJsxExpressionContainer}
152152
*/
153153
function comment(node, context) {
154-
var esnode = inherit(node, {type: 'Block', value: node.value})
154+
const esnode = inherit(node, {type: 'Block', value: node.value})
155155

156156
context.comments.push(esnode)
157157

@@ -171,39 +171,27 @@ function comment(node, context) {
171171
*/
172172
// eslint-disable-next-line complexity
173173
function element(node, context) {
174-
var parentSchema = context.schema
175-
var schema = parentSchema
176-
var props = node.properties || {}
177-
/** @type {Array<EstreeJsxAttribute|EstreeJsxSpreadAttribute>} */
178-
var attributes = []
179-
/** @type {Array.<EstreeJsxChild>} */
180-
var children
181-
/** @type {Info} */
182-
var info
183-
/** @type {string} */
184-
var prop
185-
/** @type {string} */
186-
var cssProp
187-
/** @type {Array.<EstreeProperty>} */
188-
var cssProperties
189-
/** @type {Properties[string]} */
190-
var value
191-
/** @type {EstreeJsxAttribute['value']} */
192-
var attributeValue
193-
/** @type {Object.<string, string>} */
194-
var styleValue
174+
const parentSchema = context.schema
175+
let schema = parentSchema
176+
const props = node.properties || {}
195177

196178
if (parentSchema.space === 'html' && node.tagName.toLowerCase() === 'svg') {
197179
schema = svg
198180
context.schema = schema
199181
}
200182

201-
children = all(node, context)
183+
const children = all(node, context)
184+
/** @type {Array<EstreeJsxAttribute|EstreeJsxSpreadAttribute>} */
185+
const attributes = []
186+
/** @type {string} */
187+
let prop
202188

203189
for (prop in props) {
204190
if (own.call(props, prop)) {
205-
value = props[prop]
206-
info = find(schema, prop)
191+
let value = props[prop]
192+
const info = find(schema, prop)
193+
/** @type {EstreeJsxAttribute['value']} */
194+
let attributeValue
207195

208196
// Ignore nullish and `NaN` values.
209197
// Ignore `false` and falsey known booleans.
@@ -228,11 +216,15 @@ function element(node, context) {
228216
}
229217

230218
if (prop === 'style') {
219+
/** @type {Object.<string, string>} */
231220
// @ts-ignore Assume `value` is then an object.
232-
styleValue =
221+
const styleValue =
233222
typeof value === 'string' ? parseStyle(value, node.tagName) : value
234223

235-
cssProperties = []
224+
/** @type {Array.<EstreeProperty>} */
225+
const cssProperties = []
226+
/** @type {string} */
227+
let cssProp
236228

237229
for (cssProp in styleValue) {
238230
// eslint-disable-next-line max-depth
@@ -316,8 +308,8 @@ function element(node, context) {
316308
function mdxjsEsm(node, context) {
317309
/** @type {EstreeProgram} */
318310
// @ts-ignore Assume program.
319-
var estree = node.data && node.data.estree
320-
var comments = (estree && estree.comments) || []
311+
const estree = node.data && node.data.estree
312+
const comments = (estree && estree.comments) || []
321313

322314
if (estree) {
323315
push.apply(context.comments, comments)
@@ -334,9 +326,9 @@ function mdxjsEsm(node, context) {
334326
function mdxExpression(node, context) {
335327
/** @type {EstreeProgram} */
336328
// @ts-ignore Assume program.
337-
var estree = node.data && node.data.estree
329+
const estree = node.data && node.data.estree
338330
/** @type {EstreeExpression} */
339-
var expression
331+
let expression
340332

341333
if (estree) {
342334
push.apply(context.comments, estree.comments)
@@ -360,26 +352,10 @@ function mdxExpression(node, context) {
360352
*/
361353
// eslint-disable-next-line complexity
362354
function mdxJsxElement(node, context) {
363-
var parentSchema = context.schema
364-
var schema = parentSchema
365-
var attrs = node.attributes || []
366-
var index = -1
367-
/** @type {Array<EstreeJsxAttribute|EstreeJsxSpreadAttribute>} */
368-
var attributes = []
369-
/** @type {Array.<EstreeJsxChild>} */
370-
var children
371-
/** @type {MDXJsxExpressionAttribute|MDXJsxAttribute} */
372-
var attr
373-
/** @type {MDXJsxAttributeValueExpression|string} */
374-
var value
375-
/** @type {EstreeExpression} */
376-
var expression
377-
/** @type {EstreeProgram} */
378-
var estree
379-
/** @type {EstreeJsxAttribute['value']} */
380-
var attributeValue
381-
/** @type {EstreeJsxSpreadAttribute['argument']} */
382-
var argumentValue
355+
const parentSchema = context.schema
356+
let schema = parentSchema
357+
const attrs = node.attributes || []
358+
let index = -1
383359

384360
if (
385361
node.name &&
@@ -390,11 +366,15 @@ function mdxJsxElement(node, context) {
390366
context.schema = schema
391367
}
392368

393-
children = all(node, context)
369+
const children = all(node, context)
370+
/** @type {Array<EstreeJsxAttribute|EstreeJsxSpreadAttribute>} */
371+
const attributes = []
394372

395373
while (++index < attrs.length) {
396-
attr = attrs[index]
397-
value = attr.value
374+
const attr = attrs[index]
375+
const value = attr.value
376+
/** @type {EstreeJsxAttribute['value']} */
377+
let attributeValue
398378

399379
if (attr.type === 'mdxJsxAttribute') {
400380
if (value === undefined || value === null) {
@@ -403,9 +383,11 @@ function mdxJsxElement(node, context) {
403383
}
404384
// `MDXJsxAttributeValueExpression`.
405385
else if (typeof value === 'object') {
386+
/** @type {EstreeProgram} */
406387
// @ts-ignore Assume program.
407-
estree = value.data && value.data.estree
408-
expression = null
388+
const estree = value.data && value.data.estree
389+
/** @type {EstreeExpression} */
390+
let expression = null
409391

410392
if (estree) {
411393
push.apply(context.comments, estree.comments)
@@ -436,9 +418,11 @@ function mdxJsxElement(node, context) {
436418
}
437419
// MDXJsxExpressionAttribute.
438420
else {
421+
/** @type {EstreeProgram} */
439422
// @ts-ignore Assume program.
440-
estree = attr.data && attr.data.estree
441-
argumentValue = null
423+
const estree = attr.data && attr.data.estree
424+
/** @type {EstreeJsxSpreadAttribute['argument']} */
425+
let argumentValue = null
442426

443427
if (estree) {
444428
push.apply(context.comments, estree.comments)
@@ -498,18 +482,16 @@ function mdxJsxElement(node, context) {
498482
* @returns {EstreeJsxFragment}
499483
*/
500484
function root(node, context) {
501-
var children = all(node, context)
485+
const children = all(node, context)
502486
/** @type {Array.<EstreeJsxChild>} */
503-
var cleanChildren = []
504-
var index = -1
487+
const cleanChildren = []
488+
let index = -1
505489
/** @type {Array.<EstreeJsxChild>} */
506-
var queue
507-
/** @type {EstreeJsxChild} */
508-
var child
490+
let queue
509491

510492
// Remove surrounding whitespace nodes from the fragment.
511493
while (++index < children.length) {
512-
child = children[index]
494+
const child = children[index]
513495

514496
if (
515497
child.type === 'JSXExpressionContainer' &&
@@ -520,8 +502,7 @@ function root(node, context) {
520502
queue.push(child)
521503
}
522504
} else {
523-
push.apply(cleanChildren, queue)
524-
cleanChildren.push(child)
505+
cleanChildren.push(...(queue || []), child)
525506
queue = []
526507
}
527508
}
@@ -539,7 +520,7 @@ function root(node, context) {
539520
* @returns {EstreeJsxExpressionContainer}
540521
*/
541522
function text(node) {
542-
var value = String(node.value || '')
523+
const value = String(node.value || '')
543524

544525
if (!value) return
545526

@@ -555,15 +536,13 @@ function text(node) {
555536
* @returns {Array.<EstreeJsxChild>}
556537
*/
557538
function all(parent, context) {
558-
var children = parent.children || []
559-
var index = -1
539+
const children = parent.children || []
540+
let index = -1
560541
/** @type {Array.<EstreeJsxChild>} */
561-
var results = []
562-
/** @type {EstreeJsxChild|Array.<EstreeJsxChild>} */
563-
var result
542+
const results = []
564543

565544
while (++index < children.length) {
566-
result = context.handle(children[index], context)
545+
const result = context.handle(children[index], context)
567546

568547
if (Array.isArray(result)) {
569548
results.push(...result)
@@ -584,11 +563,11 @@ function all(parent, context) {
584563
* @returns {T}
585564
*/
586565
function inherit(hast, esnode) {
587-
var left = hast.data
566+
const left = hast.data
588567
/** @type {Object.<string, unknown>} */
589-
var right
568+
let right
590569
/** @type {string} */
591-
var key
570+
let key
592571

593572
create(hast, esnode)
594573

@@ -618,7 +597,7 @@ function inherit(hast, esnode) {
618597
* @returns {T}
619598
*/
620599
function create(hast, esnode) {
621-
var p = position(hast)
600+
const p = position(hast)
622601

623602
if (p.start.line) {
624603
// @ts-ignore acorn-style.
@@ -649,13 +628,11 @@ const createJsxName =
649628
* @returns {EstreeJsxElementName}
650629
*/
651630
function (name, attribute) {
652-
/** @type {Array.<string>} */
653-
var parts
654631
/** @type {EstreeJsxElementName} */
655-
var node
632+
let node
656633

657634
if (!attribute && name.includes('.')) {
658-
parts = name.split('.')
635+
const parts = name.split('.')
659636
node = {type: 'JSXIdentifier', name: parts.shift()}
660637
while (parts.length > 0) {
661638
node = {
@@ -665,7 +642,7 @@ const createJsxName =
665642
}
666643
}
667644
} else if (name.includes(':')) {
668-
parts = name.split(':')
645+
const parts = name.split(':')
669646
node = {
670647
type: 'JSXNamespacedName',
671648
namespace: {type: 'JSXIdentifier', name: parts[0]},
@@ -686,7 +663,7 @@ const createJsxName =
686663
*/
687664
function parseStyle(value, tagName) {
688665
/** @type {Object.<string, string>} */
689-
var result = {}
666+
const result = {}
690667

691668
try {
692669
style(value, iterator)
@@ -725,7 +702,7 @@ function styleReplacer(_, $1) {
725702
* @returns {boolean}
726703
*/
727704
function jsxIdentifierName(name) {
728-
var index = -1
705+
let index = -1
729706

730707
while (++index < name.length) {
731708
if (!(index ? cont : identifierStart)(name.charCodeAt(index))) return false

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,7 @@
100100
"trailingComma": "none"
101101
},
102102
"xo": {
103-
"prettier": true,
104-
"rules": {
105-
"no-var": "off",
106-
"prefer-arrow-callback": "off"
107-
}
103+
"prettier": true
108104
},
109105
"remarkConfig": {
110106
"plugins": [

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ Say we have the following HTML file, `example.html`:
4848
And our script, `example.js`, is:
4949

5050
```js
51-
import fs from 'fs'
51+
import fs from 'node:fs'
5252
import parse5 from 'parse5'
5353
import {fromParse5} from 'hast-util-from-parse5'
5454
import {toEstree} from 'hast-util-to-estree'
5555
import recast from 'recast'
5656

57-
var hast = fromParse5(parse5.parse(String(fs.readFileSync('example.html'))))
57+
const hast = fromParse5(parse5.parse(String(fs.readFileSync('example.html'))))
5858

59-
var estree = toEstree(hast)
59+
const estree = toEstree(hast)
6060

6161
estree.comments = null // `recast` doesn’t like comments on the root.
6262
console.log(recast.prettyPrint(estree).code)

0 commit comments

Comments
 (0)