Skip to content

Commit 367baf3

Browse files
committed
Refactor code-style
1 parent e5dabdd commit 367baf3

File tree

10 files changed

+67
-75
lines changed

10 files changed

+67
-75
lines changed

lib/index.js

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ import {toH} from 'hast-to-hyperscript'
3232
import {webNamespaces} from 'web-namespaces'
3333
import {zwitch} from 'zwitch'
3434

35-
var own = {}.hasOwnProperty
35+
const own = {}.hasOwnProperty
3636

37-
var one = zwitch('type', {handlers: {root, element, text, comment, doctype}})
37+
const one = zwitch('type', {handlers: {root, element, text, comment, doctype}})
3838

3939
/**
4040
* Transform a tree from hast to Parse5’s AST.
@@ -55,7 +55,7 @@ export function toParse5(tree, space) {
5555
*/
5656
function root(node, schema) {
5757
/** @type {P5Document} */
58-
var p5 = {
58+
const p5 = {
5959
nodeName: '#document',
6060
mode: (node.data || {}).quirksMode ? 'quirks' : 'no-quirks',
6161
childNodes: []
@@ -72,7 +72,7 @@ function root(node, schema) {
7272
*/
7373
function fragment(node, schema) {
7474
/** @type {P5Fragment} */
75-
var p5 = {nodeName: '#document-fragment', childNodes: []}
75+
const p5 = {nodeName: '#document-fragment', childNodes: []}
7676
// @ts-ignore Assume correct children.
7777
p5.childNodes = all(node.children, p5, schema)
7878
return patch(node, p5)
@@ -127,7 +127,7 @@ function comment(node) {
127127
function element(node, schema) {
128128
/** @type {Space} */
129129
// @ts-ignore Assume space.
130-
var space = schema.space
130+
const space = schema.space
131131
return toH(h, Object.assign({}, node, {children: []}), {space})
132132

133133
/**
@@ -136,33 +136,29 @@ function element(node, schema) {
136136
*/
137137
function h(name, attrs) {
138138
/** @type {Array.<P5Attribute>} */
139-
var values = []
140-
/** @type {Info} */
141-
var info
142-
/** @type {P5Attribute} */
143-
var value
139+
const values = []
144140
/** @type {string} */
145-
var key
146-
/** @type {number} */
147-
var index
148-
/** @type {P5Element} */
149-
var p5
141+
let key
150142

151143
for (key in attrs) {
152144
if (!own.call(attrs, key) || attrs[key] === false) {
153145
continue
154146
}
155147

156-
info = find(schema, key)
148+
const info = find(schema, key)
157149

158150
if (info.boolean && !attrs[key]) {
159151
continue
160152
}
161153

162-
value = {name: key, value: attrs[key] === true ? '' : String(attrs[key])}
154+
/** @type {P5Attribute} */
155+
const value = {
156+
name: key,
157+
value: attrs[key] === true ? '' : String(attrs[key])
158+
}
163159

164160
if (info.space && info.space !== 'html' && info.space !== 'svg') {
165-
index = key.indexOf(':')
161+
const index = key.indexOf(':')
166162

167163
if (index < 0) {
168164
value.prefix = ''
@@ -179,7 +175,8 @@ function element(node, schema) {
179175

180176
if (schema.space === 'html' && node.tagName === 'svg') schema = svg
181177

182-
p5 = patch(node, {
178+
/** @type {P5Element} */
179+
const p5 = patch(node, {
183180
nodeName: name,
184181
tagName: name,
185182
attrs: values,
@@ -205,16 +202,15 @@ function element(node, schema) {
205202
* @returns {Array.<P5Child>}
206203
*/
207204
function all(children, p5, schema) {
208-
var index = -1
205+
let index = -1
209206
/** @type {Array.<P5Child>} */
210-
var result = []
211-
/** @type {P5Child} */
212-
var child
207+
const result = []
213208

214209
if (children) {
215210
while (++index < children.length) {
211+
/** @type {P5Child} */
216212
// @ts-ignore Assume child.
217-
child = one(children[index], schema)
213+
const child = one(children[index], schema)
218214

219215
// @ts-ignore types are wrong.
220216
child.parentNode = p5
@@ -235,7 +231,7 @@ function all(children, p5, schema) {
235231
* @returns {T}
236232
*/
237233
function patch(node, p5) {
238-
var position = node.position
234+
const position = node.position
239235

240236
if (position && position.start && position.end) {
241237
// @ts-ignore Types are wrong.

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,7 @@
7373
"trailingComma": "none"
7474
},
7575
"xo": {
76-
"prettier": true,
77-
"rules": {
78-
"no-var": "off",
79-
"prefer-arrow-callback": "off"
80-
}
76+
"prettier": true
8177
},
8278
"remarkConfig": {
8379
"plugins": [

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ npm install hast-util-to-parse5
2929
```js
3030
import {toParse5} from 'hast-util-to-parse5'
3131

32-
var ast = toParse5({
32+
const tree = toParse5({
3333
type: 'element',
3434
tagName: 'h1',
3535
properties: {},
3636
children: [{type: 'text', value: 'World!'}]
3737
})
3838

39-
console.log(ast)
39+
console.log(tree)
4040
```
4141

4242
Yields:

test/comment.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import test from 'tape'
22
import parse5 from 'parse5'
33
import {toParse5} from '../index.js'
44

5-
test('comment', function (t) {
6-
var actual = toParse5({type: 'comment', value: 'Alpha'})
7-
var expected = parse5.parseFragment('<!--Alpha-->').childNodes[0]
5+
test('comment', (t) => {
6+
const actual = toParse5({type: 'comment', value: 'Alpha'})
7+
const expected = parse5.parseFragment('<!--Alpha-->').childNodes[0]
88

99
expected.parentNode = undefined
1010

test/doctype.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import test from 'tape'
22
import parse5 from 'parse5'
33
import {toParse5} from '../index.js'
44

5-
test('doctype', function (t) {
6-
t.test('should transform a doctype (legacy)', function (st) {
7-
var actual = toParse5({
5+
test('doctype', (t) => {
6+
t.test('should transform a doctype (legacy)', (st) => {
7+
const actual = toParse5({
88
type: 'doctype',
99
name: 'html',
1010
system: 'http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd'
1111
})
12-
var expected = parse5.parse('<!DOCTYPE html>').childNodes[0]
12+
const expected = parse5.parse('<!DOCTYPE html>').childNodes[0]
1313

1414
expected.parentNode = undefined
1515

@@ -18,9 +18,9 @@ test('doctype', function (t) {
1818
st.end()
1919
})
2020

21-
t.test('should transform a doctype (modern)', function (st) {
22-
var actual = toParse5({type: 'doctype', name: 'html'})
23-
var expected = parse5.parse('<!doctypehtml>').childNodes[0]
21+
t.test('should transform a doctype (modern)', (st) => {
22+
const actual = toParse5({type: 'doctype', name: 'html'})
23+
const expected = parse5.parse('<!doctypehtml>').childNodes[0]
2424

2525
expected.parentNode = undefined
2626

test/element.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import parse5 from 'parse5'
33
import {json} from './json.js'
44
import {toParse5} from '../index.js'
55

6-
test('element', function (t) {
7-
t.test('should transform elements', function (st) {
8-
var actual = toParse5({
6+
test('element', (t) => {
7+
t.test('should transform elements', (st) => {
8+
const actual = toParse5({
99
type: 'element',
1010
tagName: 'h1',
1111
children: [{type: 'text', value: 'Alpha'}]
1212
})
13-
var expected = parse5.parseFragment('<h1>Alpha').childNodes[0]
13+
const expected = parse5.parseFragment('<h1>Alpha').childNodes[0]
1414

1515
delete expected.parentNode
1616

@@ -19,8 +19,8 @@ test('element', function (t) {
1919
st.end()
2020
})
2121

22-
t.test('should transform attributes', function (st) {
23-
var actual = toParse5({
22+
t.test('should transform attributes', (st) => {
23+
const actual = toParse5({
2424
type: 'element',
2525
tagName: 'div',
2626
properties: {},
@@ -72,7 +72,7 @@ test('element', function (t) {
7272
]
7373
})
7474

75-
var expected = parse5.parseFragment(
75+
const expected = parse5.parseFragment(
7676
[
7777
'<div>',
7878
'<h1 id="a" class="b c" hidden height="2">',
@@ -94,14 +94,14 @@ test('element', function (t) {
9494
st.end()
9595
})
9696

97-
t.test('should transform void elements', function (st) {
97+
t.test('should transform void elements', (st) => {
9898
// @ts-ignore runtime.
99-
var actual = toParse5({
99+
const actual = toParse5({
100100
type: 'element',
101101
tagName: 'img',
102102
properties: {src: '#', alt: ''}
103103
})
104-
var expected = parse5.parseFragment('<img src=# alt="">').childNodes[0]
104+
const expected = parse5.parseFragment('<img src=# alt="">').childNodes[0]
105105

106106
delete expected.parentNode
107107

@@ -110,9 +110,9 @@ test('element', function (t) {
110110
st.end()
111111
})
112112

113-
t.test('should transform templates with elements', function (st) {
113+
t.test('should transform templates with elements', (st) => {
114114
// @ts-ignore runtime.
115-
var actual = toParse5({
115+
const actual = toParse5({
116116
type: 'element',
117117
tagName: 'template',
118118
properties: {id: 'a'},
@@ -121,7 +121,7 @@ test('element', function (t) {
121121
children: [{type: 'text', value: 'Alpha'}]
122122
}
123123
})
124-
var expected = parse5.parseFragment('<template id="a">Alpha</template>')
124+
const expected = parse5.parseFragment('<template id="a">Alpha</template>')
125125
.childNodes[0]
126126

127127
delete expected.parentNode
@@ -131,9 +131,9 @@ test('element', function (t) {
131131
st.end()
132132
})
133133

134-
t.test('should transform templates with text', function (st) {
134+
t.test('should transform templates with text', (st) => {
135135
// @ts-ignore runtime.
136-
var actual = toParse5({
136+
const actual = toParse5({
137137
type: 'element',
138138
tagName: 'template',
139139
properties: {id: 'b'},
@@ -155,7 +155,7 @@ test('element', function (t) {
155155
}
156156
})
157157

158-
var expected = parse5.parseFragment(
158+
const expected = parse5.parseFragment(
159159
'<template id="b"><b>bold</b> and <i>italic</i></template>'
160160
).childNodes[0]
161161

test/position.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import parse5 from 'parse5'
33
import {json} from './json.js'
44
import {toParse5} from '../index.js'
55

6-
test('position', function (t) {
7-
var actual = toParse5({
6+
test('position', (t) => {
7+
const actual = toParse5({
88
type: 'element',
99
tagName: 'h1',
1010
children: [
@@ -23,7 +23,7 @@ test('position', function (t) {
2323
}
2424
})
2525

26-
var expected = parse5.parseFragment('<h1>Alpha', {
26+
const expected = parse5.parseFragment('<h1>Alpha', {
2727
sourceCodeLocationInfo: true
2828
}).childNodes[0]
2929

test/root.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import parse5 from 'parse5'
33
import {json} from './json.js'
44
import {toParse5} from '../index.js'
55

6-
test('root', function (t) {
7-
t.test('should transform a root (quirks)', function (st) {
8-
var expected = parse5.parse('')
6+
test('root', (t) => {
7+
t.test('should transform a root (quirks)', (st) => {
8+
const expected = parse5.parse('')
99

1010
st.deepEqual(
1111
json(
@@ -30,8 +30,8 @@ test('root', function (t) {
3030
st.end()
3131
})
3232

33-
t.test('should transform a root (no-quirks)', function (st) {
34-
var expected = parse5.parse('<!doctypehtml>')
33+
t.test('should transform a root (no-quirks)', (st) => {
34+
const expected = parse5.parse('<!doctypehtml>')
3535

3636
st.deepEqual(
3737
json(

test/svg.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import parse5 from 'parse5'
33
import {json} from './json.js'
44
import {toParse5} from '../index.js'
55

6-
test('svg', function (t) {
7-
t.test('should transform SVG in HTML', function (st) {
8-
var actual = toParse5({
6+
test('svg', (t) => {
7+
t.test('should transform SVG in HTML', (st) => {
8+
const actual = toParse5({
99
type: 'element',
1010
tagName: 'svg',
1111
properties: {
@@ -30,7 +30,7 @@ test('svg', function (t) {
3030
]
3131
})
3232

33-
var expected = parse5.parseFragment(
33+
const expected = parse5.parseFragment(
3434
[
3535
'<svg width="230" height="120" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">',
3636
'<circle cx="60" cy="60" r="50" fill="red"/>',
@@ -45,8 +45,8 @@ test('svg', function (t) {
4545

4646
st.end()
4747
})
48-
t.test('should transform SVG', function (st) {
49-
var actual = toParse5(
48+
t.test('should transform SVG', (st) => {
49+
const actual = toParse5(
5050
{
5151
type: 'element',
5252
tagName: 'circle',
@@ -56,7 +56,7 @@ test('svg', function (t) {
5656
'svg'
5757
)
5858

59-
var expected = parse5.parseFragment(
59+
const expected = parse5.parseFragment(
6060
'<svg><circle cx="60" cy="60" r="50" fill="red"/></svg>'
6161
// @ts-ignore runtime.
6262
).childNodes[0].childNodes[0]

test/text.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import test from 'tape'
22
import parse5 from 'parse5'
33
import {toParse5} from '../index.js'
44

5-
test('text', function (t) {
6-
var expected = parse5.parseFragment('Alpha').childNodes[0]
5+
test('text', (t) => {
6+
const expected = parse5.parseFragment('Alpha').childNodes[0]
77

88
expected.parentNode = undefined
99

0 commit comments

Comments
 (0)