Skip to content

Commit 665ed82

Browse files
committed
Refactor code-style
1 parent 4f8b023 commit 665ed82

File tree

4 files changed

+49
-57
lines changed

4 files changed

+49
-57
lines changed

lib/index.js

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,16 @@ import {htmlVoidElements} from 'html-void-elements'
7373
import {webNamespaces} from 'web-namespaces'
7474
import {zwitch} from 'zwitch'
7575

76-
var inTemplateMode = 'IN_TEMPLATE_MODE'
77-
var dataState = 'DATA_STATE'
78-
var characterToken = 'CHARACTER_TOKEN'
79-
var startTagToken = 'START_TAG_TOKEN'
80-
var endTagToken = 'END_TAG_TOKEN'
81-
var commentToken = 'COMMENT_TOKEN'
82-
var doctypeToken = 'DOCTYPE_TOKEN'
76+
const inTemplateMode = 'IN_TEMPLATE_MODE'
77+
const dataState = 'DATA_STATE'
78+
const characterToken = 'CHARACTER_TOKEN'
79+
const startTagToken = 'START_TAG_TOKEN'
80+
const endTagToken = 'END_TAG_TOKEN'
81+
const commentToken = 'COMMENT_TOKEN'
82+
const doctypeToken = 'DOCTYPE_TOKEN'
8383

8484
/** @type {P5ParserOptions} */
85-
var parseOptions = {sourceCodeLocationInfo: true, scriptingEnabled: false}
85+
const parseOptions = {sourceCodeLocationInfo: true, scriptingEnabled: false}
8686

8787
/**
8888
* Given a hast tree and an optional vfile (for positional info), return a new
@@ -93,24 +93,22 @@ var parseOptions = {sourceCodeLocationInfo: true, scriptingEnabled: false}
9393
* @param {Options} [options] Configuration
9494
*/
9595
export function raw(tree, file, options) {
96-
var index = -1
97-
var parser = new Parser(parseOptions)
98-
var one = zwitch('type', {
96+
let index = -1
97+
const parser = new Parser(parseOptions)
98+
const one = zwitch('type', {
9999
handlers: {root, element, text, comment, doctype, raw: handleRaw},
100100
unknown
101101
})
102102
/** @type {boolean} */
103-
var stitches
103+
let stitches
104104
/** @type {HiddenTokenizer} */
105-
var tokenizer
105+
let tokenizer
106106
/** @type {HiddenPreprocessor} */
107-
var preprocessor
107+
let preprocessor
108108
/** @type {HiddenPosTracker} */
109-
var posTracker
109+
let posTracker
110110
/** @type {HiddenLocationTracker} */
111-
var locationTracker
112-
/** @type {Node} */
113-
var result
111+
let locationTracker
114112

115113
if (isOptions(file)) {
116114
options = file
@@ -124,7 +122,7 @@ export function raw(tree, file, options) {
124122
}
125123

126124
// @ts-expect-error: update `vfile`.
127-
result = fromParse5(documentMode(tree) ? document() : fragment(), file)
125+
const result = fromParse5(documentMode(tree) ? document() : fragment(), file)
128126

129127
if (stitches) {
130128
visit(result, 'comment', mend)
@@ -156,7 +154,7 @@ export function raw(tree, file, options) {
156154
*/
157155
function fragment() {
158156
/** @type {P5Element} */
159-
var context = {
157+
const context = {
160158
nodeName: 'template',
161159
tagName: 'template',
162160
attrs: [],
@@ -165,7 +163,7 @@ export function raw(tree, file, options) {
165163
parentNode: undefined
166164
}
167165
/** @type {P5Element} */
168-
var mock = {
166+
const mock = {
169167
nodeName: 'documentmock',
170168
tagName: 'documentmock',
171169
attrs: [],
@@ -174,7 +172,7 @@ export function raw(tree, file, options) {
174172
parentNode: undefined
175173
}
176174
/** @type {P5Fragment} */
177-
var doc = {nodeName: '#document-fragment', childNodes: []}
175+
const doc = {nodeName: '#document-fragment', childNodes: []}
178176

179177
parser._bootstrap(mock, context)
180178
parser._pushTmplInsertionMode(inTemplateMode)
@@ -200,7 +198,7 @@ export function raw(tree, file, options) {
200198
*/
201199
function document() {
202200
/** @type {P5Document} */
203-
var doc = parser.treeAdapter.createDocument()
201+
const doc = parser.treeAdapter.createDocument()
204202

205203
parser._bootstrap(doc, null)
206204
tokenizer = parser.tokenizer
@@ -218,7 +216,7 @@ export function raw(tree, file, options) {
218216
* @returns {void}
219217
*/
220218
function all(nodes) {
221-
var index = -1
219+
let index = -1
222220

223221
/* istanbul ignore else - invalid nodes, see rehypejs/rehype-raw#7. */
224222
if (nodes) {
@@ -299,12 +297,10 @@ export function raw(tree, file, options) {
299297
* @returns {void}
300298
*/
301299
function handleRaw(node) {
302-
var start = pointStart(node)
303-
var line = start.line || 1
304-
var column = start.column || 1
305-
var offset = start.offset || 0
306-
/** @type {HiddenToken} */
307-
var token
300+
const start = pointStart(node)
301+
const line = start.line || 1
302+
const column = start.column || 1
303+
const offset = start.offset || 0
308304

309305
// Reset preprocessor:
310306
// See: <https://github.com/inikulin/parse5/blob/9c683e1/packages/parse5/lib/tokenizer/preprocessor.js>.
@@ -339,7 +335,7 @@ export function raw(tree, file, options) {
339335
// Process final characters if they’re still there after hibernating.
340336
// Similar to:
341337
// See: <https://github.com/inikulin/parse5/blob/9c683e1/packages/parse5/lib/extensions/location-info/tokenizer-mixin.js#L95>.
342-
token = tokenizer.currentCharacterToken
338+
const token = tokenizer.currentCharacterToken
343339

344340
if (token) {
345341
token.location.endLine = posTracker.line
@@ -353,7 +349,7 @@ export function raw(tree, file, options) {
353349
* @param {UnistNode} node
354350
*/
355351
function stitch(node) {
356-
var clone = Object.assign({}, node)
352+
const clone = Object.assign({}, node)
357353

358354
stitches = true
359355

@@ -404,10 +400,10 @@ export function raw(tree, file, options) {
404400
* @returns {HiddenToken}
405401
*/
406402
function startTag(node) {
407-
/** @type {unknown} */
408-
var location = Object.assign(createParse5Location(node), {
409-
startTag: Object.assign({}, location)
410-
})
403+
/** @type {P5Location} */
404+
const location = Object.assign(createParse5Location(node))
405+
// @ts-ignore extra positional info.
406+
location.startTag = Object.assign({}, location)
411407

412408
// Untyped token.
413409
return {
@@ -439,10 +435,10 @@ function attributes(node) {
439435
* @returns {HiddenToken}
440436
*/
441437
function endTag(node) {
442-
/** @type {unknown} */
443-
var location = Object.assign(createParse5Location(node), {
444-
endTag: Object.assign({}, location)
445-
})
438+
/** @type {P5Location} */
439+
const location = Object.assign(createParse5Location(node))
440+
// @ts-ignore extra positional info.
441+
location.startTag = Object.assign({}, location)
446442

447443
// Untyped token.
448444
return {
@@ -466,7 +462,7 @@ function unknown(node) {
466462
* @returns {boolean}
467463
*/
468464
function documentMode(node) {
469-
var head = node.type === 'root' ? node.children[0] : node
465+
const head = node.type === 'root' ? node.children[0] : node
470466
return Boolean(
471467
head &&
472468
(head.type === 'doctype' ||
@@ -479,8 +475,8 @@ function documentMode(node) {
479475
* @returns {P5Location}
480476
*/
481477
function createParse5Location(node) {
482-
var start = pointStart(node)
483-
var end = pointEnd(node)
478+
const start = pointStart(node)
479+
const end = pointEnd(node)
484480

485481
return {
486482
startLine: start.line,

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@
8282
"trailingComma": "none"
8383
},
8484
"xo": {
85-
"prettier": true,
86-
"rules": {
87-
"no-var": "off",
88-
"prefer-arrow-callback": "off"
89-
}
85+
"prettier": true
9086
},
9187
"remarkConfig": {
9288
"plugins": [

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ npm install hast-util-raw
3737
import {h} from 'hastscript'
3838
import {raw} from 'hast-util-raw'
3939

40-
var tree = h('div', [h('h1', ['Foo ', h('h2', 'Bar'), ' Baz'])])
40+
const tree = h('div', [h('h1', ['Foo ', h('h2', 'Bar'), ' Baz'])])
4141

42-
var clean = raw(tree)
42+
const reformatted = raw(tree)
4343

44-
console.log(clean)
44+
console.log(reformatted)
4545
```
4646

4747
Yields:

test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import remarkRehype from 'remark-rehype'
77
import rehypeStringify from 'rehype-stringify'
88
import {raw} from './index.js'
99

10-
test('raw', function (t) {
10+
test('raw', (t) => {
1111
t.throws(
12-
function () {
12+
() => {
1313
// @ts-ignore runtime.
1414
raw(u('unknown'))
1515
},
@@ -284,15 +284,15 @@ test('raw', function (t) {
284284
t.end()
285285
})
286286

287-
test('integration', function (t) {
287+
test('integration', (t) => {
288288
unified()
289289
.use(remarkParse)
290290
.use(remarkRehype, {allowDangerousHtml: true})
291-
.use(function () {
291+
.use(() => {
292292
// @ts-ignore hast is more specific than unist.
293293
return (tree, file) => raw(tree, file)
294294
})
295-
.use(function () {
295+
.use(() => {
296296
return transformer
297297
function transformer(/** @type {import('unist').Node} */ tree) {
298298
t.deepEqual(
@@ -672,7 +672,7 @@ test('integration', function (t) {
672672
'<p>Hello, world!',
673673
''
674674
].join('\n'),
675-
function (error, file) {
675+
(error, file) => {
676676
t.ifErr(error, 'should not fail')
677677

678678
t.equal(

0 commit comments

Comments
 (0)