Skip to content

Commit 3506854

Browse files
committed
Refactor code-style
1 parent b2abdf9 commit 3506854

File tree

4 files changed

+32
-37
lines changed

4 files changed

+32
-37
lines changed

index.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ import {position} from 'unist-util-position'
3737
import {webNamespaces} from 'web-namespaces'
3838
import {zwitch} from 'zwitch'
3939

40-
var own = {}.hasOwnProperty
40+
const own = {}.hasOwnProperty
4141

42-
var one = zwitch('type', {
42+
const one = zwitch('type', {
4343
handlers: {root, element, text, comment, doctype},
4444
invalid,
4545
unknown
@@ -64,7 +64,7 @@ function unknown(value) {
6464
* @param {Space|Options} [options]
6565
*/
6666
export function toXast(tree, options) {
67-
var space = typeof options === 'string' ? options : (options || {}).space
67+
const space = typeof options === 'string' ? options : (options || {}).space
6868
// @ts-ignore types are wrong.
6969
return one(tree, {schema: space === 'svg' ? svg : html, ns: null})
7070
}
@@ -114,18 +114,16 @@ function doctype(node) {
114114
*/
115115
// eslint-disable-next-line complexity
116116
function element(node, parentConfig) {
117-
var props = node.properties || {}
118-
var schema = parentConfig.schema
117+
const props = node.properties || {}
118+
let schema = parentConfig.schema
119119
/** @type {XastAttributes} */
120-
var attributes = {}
121-
/** @type {Context} */
122-
var config
120+
const attributes = {}
123121
/** @type {HastPropertyValue} */
124-
var value
122+
let value
125123
/** @type {string} */
126-
var key
124+
let key
127125
/** @type {Info} */
128-
var info
126+
let info
129127

130128
if (props.xmlns === webNamespaces.html) {
131129
schema = html
@@ -137,7 +135,8 @@ function element(node, parentConfig) {
137135
schema = svg
138136
}
139137

140-
config = Object.assign({}, parentConfig, {
138+
/** @type {Context} */
139+
const config = Object.assign({}, parentConfig, {
141140
schema,
142141
ns: webNamespaces[schema.space]
143142
})
@@ -202,8 +201,8 @@ function element(node, parentConfig) {
202201
*/
203202
function all(origin, config) {
204203
/** @type {Array.<XastElement|XastText|XastComment|XastDoctype>} */
205-
var result = []
206-
var index = -1
204+
const result = []
205+
let index = -1
207206

208207
if (
209208
config.schema === html &&

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,7 @@
7676
"trailingComma": "none"
7777
},
7878
"xo": {
79-
"prettier": true,
80-
"rules": {
81-
"no-var": "off",
82-
"prefer-arrow-callback": "off"
83-
}
79+
"prettier": true
8480
},
8581
"remarkConfig": {
8682
"plugins": [

readme.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,19 @@ Say we have an `example.html` file, that looks as follows:
3535
…and our script, `example.js`, looks as follows:
3636

3737
```js
38-
import fs from 'fs'
39-
import unified from 'unified'
40-
import parse from 'rehype-parse'
38+
import fs from 'node:fs'
39+
import {unified} from 'unified'
40+
import rehypeParse from 'rehype-parse'
4141
import {toXast} from 'hast-util-to-xast'
4242
import {toXml} from 'xast-util-to-xml'
4343

4444
// Get the HTML syntax tree:
45-
var hast = unified()
46-
.use(parse)
45+
const hast = unified()
46+
.use(rehypeParse)
4747
.parse(fs.readFileSync('example.html'))
4848

4949
// Turn hast to xast:
50-
var xast = toXast(hast)
50+
const xast = toXast(hast)
5151

5252
// Serialize xast:
5353
console.log(toXml(xast))

test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import s from 'hastscript/svg.js'
66
import x from 'xastscript'
77
import {toXast} from './index.js'
88

9-
test('toXast', function (t) {
10-
t.test('main', function (t) {
9+
test('toXast', (t) => {
10+
t.test('main', (t) => {
1111
t.equal(typeof toXast, 'function', 'should expose a function')
1212

1313
t.throws(
14-
function () {
14+
() => {
1515
// @ts-ignore runtime.
1616
toXast()
1717
},
@@ -20,7 +20,7 @@ test('toXast', function (t) {
2020
)
2121

2222
t.throws(
23-
function () {
23+
() => {
2424
// @ts-ignore well-known.
2525
toXast({type: 'raw', value: '<script>alert(1)</script>'})
2626
},
@@ -69,7 +69,7 @@ test('toXast', function (t) {
6969
t.end()
7070
})
7171

72-
t.test('root', function (t) {
72+
t.test('root', (t) => {
7373
t.deepEqual(
7474
toXast(u('root', [h('div', 'Alpha')])),
7575
u('root', [x('div', {xmlns: ns.html}, 'Alpha')]),
@@ -79,7 +79,7 @@ test('toXast', function (t) {
7979
t.end()
8080
})
8181

82-
t.test('text', function (t) {
82+
t.test('text', (t) => {
8383
t.deepEqual(
8484
toXast(u('text', 'Alpha')),
8585
u('text', 'Alpha'),
@@ -96,7 +96,7 @@ test('toXast', function (t) {
9696
t.end()
9797
})
9898

99-
t.test('comment', function (t) {
99+
t.test('comment', (t) => {
100100
t.deepEqual(
101101
toXast(u('comment', 'Alpha')),
102102
u('comment', 'Alpha'),
@@ -113,7 +113,7 @@ test('toXast', function (t) {
113113
t.end()
114114
})
115115

116-
t.test('doctype', function (t) {
116+
t.test('doctype', (t) => {
117117
t.deepEqual(
118118
// @ts-ignore hast@next.
119119
toXast(u('doctype')),
@@ -124,7 +124,7 @@ test('toXast', function (t) {
124124
t.end()
125125
})
126126

127-
t.test('element', function (t) {
127+
t.test('element', (t) => {
128128
t.deepEqual(
129129
toXast(h('p', [h('a', 'A'), ' & ', h('b', 'B'), '.'])),
130130
x('p', {xmlns: ns.html}, [x('a', 'A'), ' & ', x('b', 'B'), '.']),
@@ -167,7 +167,7 @@ test('toXast', function (t) {
167167
t.end()
168168
})
169169

170-
t.test('attributes', function (t) {
170+
t.test('attributes', (t) => {
171171
t.deepEqual(
172172
toXast(u('element', {tagName: 'br'}, [])),
173173
x('br', {xmlns: ns.html}),
@@ -303,7 +303,7 @@ test('toXast', function (t) {
303303
t.end()
304304
})
305305

306-
t.test('svg', function (t) {
306+
t.test('svg', (t) => {
307307
t.deepEqual(
308308
toXast(
309309
s(
@@ -403,7 +403,7 @@ test('toXast', function (t) {
403403
t.end()
404404
})
405405

406-
t.test('mathml', function (t) {
406+
t.test('mathml', (t) => {
407407
t.deepEqual(
408408
toXast(
409409
u('element', {tagName: 'p', properties: {}}, [

0 commit comments

Comments
 (0)