Skip to content
This repository was archived by the owner on Aug 9, 2023. It is now read-only.

Commit 5bc5b47

Browse files
committed
Refactor code-style
1 parent f8180c8 commit 5bc5b47

File tree

4 files changed

+108
-123
lines changed

4 files changed

+108
-123
lines changed

index.js

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* @callback CreateElementLike
1111
* @param {string} name
12-
* @param {Object<string, any>} [attributes]
12+
* @param {Record<string, any>} [attributes]
1313
* @param {Array.<string|any>} [children]
1414
* @returns {any}
1515
*
@@ -34,17 +34,17 @@ import style from 'style-to-object'
3434
import {webNamespaces as ns} from 'web-namespaces'
3535
import {convert} from 'unist-util-is'
3636

37-
var own = {}.hasOwnProperty
37+
const own = {}.hasOwnProperty
3838

3939
/** @type {AssertRoot} */
4040
// @ts-ignore it’s correct.
41-
var root = convert('root')
41+
const root = convert('root')
4242
/** @type {AssertElement} */
4343
// @ts-ignore it’s correct.
44-
var element = convert('element')
44+
const element = convert('element')
4545
/** @type {AssertText} */
4646
// @ts-ignore it’s correct.
47-
var text = convert('text')
47+
const text = convert('text')
4848

4949
/**
5050
* @template {CreateElementLike} H
@@ -58,13 +58,13 @@ export function toH(h, tree, options) {
5858
throw new TypeError('h is not a function')
5959
}
6060

61-
var r = react(h)
62-
var v = vue(h)
63-
var vd = vdom(h)
61+
const r = react(h)
62+
const v = vue(h)
63+
const vd = vdom(h)
6464
/** @type {string|boolean} */
65-
var prefix
65+
let prefix
6666
/** @type {Element} */
67-
var node
67+
let node
6868

6969
if (typeof options === 'string' || typeof options === 'boolean') {
7070
prefix = options
@@ -123,18 +123,16 @@ export function toH(h, tree, options) {
123123
* @param {Context} ctx
124124
*/
125125
function transform(h, node, ctx) {
126-
var parentSchema = ctx.schema
127-
var schema = parentSchema
128-
var name = node.tagName
129-
/** @type {Object.<string, unknown>} */
130-
var attributes = {}
126+
const parentSchema = ctx.schema
127+
let schema = parentSchema
128+
let name = node.tagName
129+
/** @type {Record<string, unknown>} */
130+
const attributes = {}
131131
/** @type {Array.<ReturnType<H>|string>} */
132-
var nodes = []
133-
var index = -1
132+
const nodes = []
133+
let index = -1
134134
/** @type {string} */
135-
var key
136-
/** @type {Element['children'][number]} */
137-
var value
135+
let key
138136

139137
if (parentSchema.space === 'html' && name.toLowerCase() === 'svg') {
140138
schema = svg
@@ -162,7 +160,7 @@ function transform(h, node, ctx) {
162160

163161
if (node.children) {
164162
while (++index < node.children.length) {
165-
value = node.children[index]
163+
const value = node.children[index]
166164

167165
if (element(value)) {
168166
nodes.push(transform(h, value, ctx))
@@ -183,17 +181,17 @@ function transform(h, node, ctx) {
183181
}
184182

185183
/**
186-
* @param {Object.<string, unknown>} props
184+
* @param {Record<string, unknown>} props
187185
* @param {string} prop
188186
* @param {unknown} value
189187
* @param {Context} ctx
190188
* @param {string} name
191189
*/
192190
// eslint-disable-next-line complexity, max-params
193191
function addAttribute(props, prop, value, ctx, name) {
194-
var info = find(ctx.schema, prop)
192+
const info = find(ctx.schema, prop)
195193
/** @type {string} */
196-
var subprop
194+
let subprop
197195

198196
// Ignore nullish and `NaN` values.
199197
// Ignore `false` and falsey known booleans for hyperlike DSLs.
@@ -255,7 +253,7 @@ function addAttribute(props, prop, value, ctx, name) {
255253
*/
256254
function react(h) {
257255
/** @type {unknown} */
258-
var node = h('div')
256+
const node = h('div')
259257
return Boolean(
260258
node &&
261259
// @ts-ignore Looks like a React node.
@@ -283,7 +281,7 @@ function hyperscript(h) {
283281
*/
284282
function vdom(h) {
285283
/** @type {unknown} */
286-
var node = h('div')
284+
const node = h('div')
287285
// @ts-ignore Looks like a vnode.
288286
return node.type === 'VirtualNode'
289287
}
@@ -296,46 +294,40 @@ function vdom(h) {
296294
*/
297295
function vue(h) {
298296
/** @type {unknown} */
299-
var node = h('div')
297+
const node = h('div')
300298
// @ts-ignore Looks like a Vue node.
301299
return Boolean(node && node.context && node.context._isVue)
302300
}
303301

304302
/**
305303
* @param {string} value
306304
* @param {string} tagName
307-
* @returns {Object.<string, string>}
305+
* @returns {Record<string, string>}
308306
*/
309307
function parseStyle(value, tagName) {
310-
/** @type {Object.<string, string>} */
311-
var result = {}
308+
/** @type {Record<string, string>} */
309+
const result = {}
312310

313311
try {
314-
style(value, iterator)
312+
style(value, (name, value) => {
313+
if (name.slice(0, 4) === '-ms-') name = 'ms-' + name.slice(4)
314+
315+
result[
316+
name.replace(
317+
/-([a-z])/g,
318+
/**
319+
* @param {string} _
320+
* @param {string} $1
321+
* @returns {string}
322+
*/ (_, $1) => $1.toUpperCase()
323+
)
324+
] = value
325+
})
315326
} catch (error) {
316327
error.message =
317328
tagName + '[style]' + error.message.slice('undefined'.length)
318329
throw error
319330
}
320331

321332
return result
322-
323-
/**
324-
* @param {string} name
325-
* @param {string} value
326-
* @returns {void}
327-
*/
328-
function iterator(name, value) {
329-
if (name.slice(0, 4) === '-ms-') name = 'ms-' + name.slice(4)
330-
result[name.replace(/-([a-z])/g, styleReplacer)] = value
331-
}
332-
}
333-
334-
/**
335-
* @param {string} _
336-
* @param {string} $1
337-
* @returns {string}
338-
*/
339-
function styleReplacer(_, $1) {
340-
return $1.toUpperCase()
341333
}

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,7 @@
9191
"trailingComma": "none"
9292
},
9393
"xo": {
94-
"prettier": true,
95-
"rules": {
96-
"no-var": "off",
97-
"prefer-arrow-callback": "off"
98-
}
94+
"prettier": true
9995
},
10096
"remarkConfig": {
10197
"plugins": [

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ npm install hast-to-hyperscript
2828
import {toH} from 'hast-to-hyperscript'
2929
import h from 'hyperscript'
3030

31-
var tree = {
31+
const tree = {
3232
type: 'element',
3333
tagName: 'p',
3434
properties: {id: 'alpha', className: ['bravo']},
@@ -45,7 +45,7 @@ var tree = {
4545
}
4646

4747
// Transform (`hyperscript` needs `outerHTML` to serialize):
48-
var doc = toH(h, tree).outerHTML
48+
const doc = toH(h, tree).outerHTML
4949

5050
console.log(doc)
5151
```

0 commit comments

Comments
 (0)