Skip to content

Commit 0977456

Browse files
committed
Refactor code-style
1 parent 3a594c0 commit 0977456

File tree

4 files changed

+56
-81
lines changed

4 files changed

+56
-81
lines changed

lib/index.js

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ import {html, svg, find} from 'property-information'
4848
import vfileLocation from 'vfile-location'
4949
import {webNamespaces} from 'web-namespaces'
5050

51-
var own = {}.hasOwnProperty
51+
const own = {}.hasOwnProperty
5252

5353
// Handlers.
54-
var map = {
54+
const map = {
5555
'#document': root,
5656
'#document-fragment': root,
5757
'#text': text,
@@ -67,9 +67,9 @@ var map = {
6767
*/
6868
export function fromParse5(ast, options = {}) {
6969
/** @type {Options} */
70-
var settings
70+
let settings
7171
/** @type {VFile} */
72-
var file
72+
let file
7373

7474
if (isFile(options)) {
7575
file = options
@@ -98,15 +98,11 @@ export function fromParse5(ast, options = {}) {
9898
* @returns {Node}
9999
*/
100100
function transform(ctx, ast) {
101-
var schema = ctx.schema
101+
const schema = ctx.schema
102102
/** @type {Handler} */
103-
var fn = own.call(map, ast.nodeName) ? map[ast.nodeName] : element
103+
const fn = own.call(map, ast.nodeName) ? map[ast.nodeName] : element
104104
/** @type {Array.<Child>} */
105-
var children
106-
/** @type {Node} */
107-
var result
108-
/** @type {Position} */
109-
var position
105+
let children
110106

111107
// Element.
112108
if ('tagName' in ast) {
@@ -117,11 +113,11 @@ function transform(ctx, ast) {
117113
children = nodes(ctx, ast.childNodes)
118114
}
119115

120-
result = fn(ctx, ast, children)
116+
const result = fn(ctx, ast, children)
121117

122118
if ('sourceCodeLocation' in ast && ast.sourceCodeLocation && ctx.file) {
123119
// @ts-ignore It’s fine.
124-
position = location(ctx, result, ast.sourceCodeLocation)
120+
const position = location(ctx, result, ast.sourceCodeLocation)
125121

126122
if (position) {
127123
ctx.location = true
@@ -142,9 +138,9 @@ function transform(ctx, ast) {
142138
* @returns {Array.<Child>}
143139
*/
144140
function nodes(ctx, children) {
145-
var index = -1
141+
let index = -1
146142
/** @type {Array.<Child>} */
147-
var result = []
143+
const result = []
148144

149145
while (++index < children.length) {
150146
// @ts-ignore Assume no roots in children.
@@ -165,19 +161,15 @@ function nodes(ctx, children) {
165161
*/
166162
function root(ctx, ast, children) {
167163
/** @type {Root} */
168-
var result = {
164+
const result = {
169165
type: 'root',
170166
children,
171167
data: {quirksMode: ast.mode === 'quirks' || ast.mode === 'limited-quirks'}
172168
}
173-
/** @type {string} */
174-
var doc
175-
/** @type {VFileLocation} */
176-
var location
177169

178170
if (ctx.file && ctx.location) {
179-
doc = String(ctx.file)
180-
location = vfileLocation(doc)
171+
const doc = String(ctx.file)
172+
const location = vfileLocation(doc)
181173
result.position = {
182174
start: location.toPoint(0),
183175
end: location.toPoint(doc.length)
@@ -229,34 +221,24 @@ function comment(_, ast) {
229221
* @returns {Element}
230222
*/
231223
function element(ctx, ast, children) {
232-
var fn = ctx.schema.space === 'svg' ? s : h
233-
var index = -1
224+
const fn = ctx.schema.space === 'svg' ? s : h
225+
let index = -1
234226
/** @type {Object.<string, string>} */
235-
var props = {}
236-
/** @type {Element} */
237-
var result
238-
/** @type {P5Attribute} */
239-
var attribute
240-
/** @type {P5ElementLocation} */
241-
var pos
242-
/** @type {Point} */
243-
var start
244-
/** @type {Point} */
245-
var end
227+
const props = {}
246228

247229
while (++index < ast.attrs.length) {
248-
attribute = ast.attrs[index]
230+
const attribute = ast.attrs[index]
249231
props[(attribute.prefix ? attribute.prefix + ':' : '') + attribute.name] =
250232
attribute.value
251233
}
252234

253-
result = fn(ast.tagName, props, children)
235+
const result = fn(ast.tagName, props, children)
254236

255237
if (result.tagName === 'template' && 'content' in ast) {
256238
// @ts-ignore Types are wrong.
257-
pos = ast.sourceCodeLocation
258-
start = pos && pos.startTag && position(pos.startTag).end
259-
end = pos && pos.endTag && position(pos.endTag).start
239+
const pos = ast.sourceCodeLocation
240+
const start = pos && pos.startTag && position(pos.startTag).end
241+
const end = pos && pos.endTag && position(pos.endTag).start
260242

261243
// @ts-ignore Types are wrong.
262244
result.content = transform(ctx, ast.content)
@@ -278,16 +260,10 @@ function element(ctx, ast, children) {
278260
* @returns {Position}
279261
*/
280262
function location(ctx, node, location) {
281-
var result = position(location)
282-
/** @type {ElementChild} */
283-
var tail
284-
/** @type {string} */
285-
var key
286-
/** @type {Object.<string, Position>} */
287-
var props
263+
const result = position(location)
288264

289265
if (node.type === 'element') {
290-
tail = node.children[node.children.length - 1]
266+
const tail = node.children[node.children.length - 1]
291267

292268
// Bug for unclosed with children.
293269
// See: <https://github.com/inikulin/parse5/issues/109>.
@@ -296,7 +272,10 @@ function location(ctx, node, location) {
296272
}
297273

298274
if (ctx.verbose) {
299-
props = {}
275+
/** @type {Object.<string, Position>} */
276+
const props = {}
277+
/** @type {string} */
278+
let key
300279

301280
for (key in location.attrs) {
302281
if (own.call(location.attrs, key)) {
@@ -322,12 +301,12 @@ function location(ctx, node, location) {
322301
* @returns {Position|null}
323302
*/
324303
function position(loc) {
325-
var start = point({
304+
const start = point({
326305
line: loc.startLine,
327306
column: loc.startCol,
328307
offset: loc.startOffset
329308
})
330-
var end = point({
309+
const end = point({
331310
line: loc.endLine,
332311
column: loc.endCol,
333312
offset: loc.endOffset

package.json

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

readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ Say we have the following file, `example.html`:
3333
And our script, `example.js`, looks as follows:
3434

3535
```js
36-
import vfile from 'to-vfile'
3736
import parse5 from 'parse5'
37+
import {readSync} from 'to-vfile'
3838
import {inspect} from 'unist-util-inspect'
3939
import {fromParse5} from 'hast-util-from-parse5'
4040

41-
var file = vfile.readSync('example.html')
42-
var p5ast = parse5.parse(String(file), {sourceCodeLocationInfo: true})
43-
var hast = fromParse5(p5ast, file)
41+
const file = readSync('example.html')
42+
const p5ast = parse5.parse(String(file), {sourceCodeLocationInfo: true})
43+
const hast = fromParse5(p5ast, file)
4444

4545
console.log(inspect(hast))
4646
```

test/index.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import {visit} from 'unist-util-visit'
1818
import vfile from 'to-vfile'
1919
import {fromParse5} from '../index.js'
2020

21-
var join = path.join
21+
const join = path.join
2222

23-
test('hast-util-from-parse5', function (t) {
24-
var file = vfile({contents: '<title>Hello!</title><h1>World!'})
23+
test('hast-util-from-parse5', (t) => {
24+
const file = vfile({contents: '<title>Hello!</title><h1>World!'})
2525

2626
t.deepEqual(
2727
fromParse5(parse5.parse(String(file))),
@@ -357,10 +357,10 @@ test('hast-util-from-parse5', function (t) {
357357
t.end()
358358
})
359359

360-
test('fixtures', function (t) {
361-
var base = join('test', 'fixtures')
362-
var files = fs.readdirSync(base)
363-
var index = -1
360+
test('fixtures', (t) => {
361+
const base = join('test', 'fixtures')
362+
const files = fs.readdirSync(base)
363+
let index = -1
364364

365365
while (++index < files.length) {
366366
if (!isHidden(files[index])) {
@@ -374,8 +374,8 @@ test('fixtures', function (t) {
374374
* @param {string} fixture
375375
*/
376376
function each(fixture) {
377-
t.test(fixture, function (st) {
378-
var options = {
377+
t.test(fixture, (st) => {
378+
const options = {
379379
file: vfile.readSync(join(base, fixture, 'index.html')),
380380
out: join(base, fixture, 'index.json')
381381
}
@@ -394,12 +394,12 @@ test('fixtures', function (t) {
394394
* @param {Options} options
395395
*/
396396
function checkYesYes(t, options) {
397-
var input = parse5.parse(String(options.file), {
397+
const input = parse5.parse(String(options.file), {
398398
sourceCodeLocationInfo: true
399399
})
400-
var actual = fromParse5(input, {file: options.file, verbose: true})
400+
const actual = fromParse5(input, {file: options.file, verbose: true})
401401
/** @type {Node} */
402-
var expected
402+
let expected
403403

404404
try {
405405
expected = JSON.parse(String(fs.readFileSync(options.out)))
@@ -418,10 +418,10 @@ test('fixtures', function (t) {
418418
* @param {Options} options
419419
*/
420420
function checkNoYes(t, options) {
421-
var input = parse5.parse(String(options.file))
422-
var actual = fromParse5(input, {file: options.file, verbose: true})
421+
const input = parse5.parse(String(options.file))
422+
const actual = fromParse5(input, {file: options.file, verbose: true})
423423
/** @type {Node} */
424-
var expected = JSON.parse(String(fs.readFileSync(options.out)))
424+
const expected = JSON.parse(String(fs.readFileSync(options.out)))
425425

426426
clean(expected)
427427

@@ -434,12 +434,12 @@ test('fixtures', function (t) {
434434
* @param {Options} options
435435
*/
436436
function checkYesNo(t, options) {
437-
var input = parse5.parse(String(options.file), {
437+
const input = parse5.parse(String(options.file), {
438438
sourceCodeLocationInfo: true
439439
})
440-
var actual = fromParse5(input)
440+
const actual = fromParse5(input)
441441
/** @type {Node} */
442-
var expected = JSON.parse(String(fs.readFileSync(options.out)))
442+
const expected = JSON.parse(String(fs.readFileSync(options.out)))
443443

444444
clean(expected)
445445

@@ -452,10 +452,10 @@ test('fixtures', function (t) {
452452
* @param {Options} options
453453
*/
454454
function checkNoNo(t, options) {
455-
var input = parse5.parse(String(options.file))
456-
var actual = fromParse5(input)
455+
const input = parse5.parse(String(options.file))
456+
const actual = fromParse5(input)
457457
/** @type {Node} */
458-
var expected = JSON.parse(String(fs.readFileSync(options.out)))
458+
const expected = JSON.parse(String(fs.readFileSync(options.out)))
459459

460460
clean(expected)
461461

0 commit comments

Comments
 (0)