Skip to content

Commit c1b83c9

Browse files
committed
Refactor code-style
1 parent 106f1b9 commit c1b83c9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+122
-129
lines changed

lib/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ import {one} from './tree.js'
1616
* @returns {string}
1717
*/
1818
export function toHtml(node, options = {}) {
19-
var quote = options.quote || '"'
19+
const quote = options.quote || '"'
2020
/** @type {Quote} */
21-
var alternative = quote === '"' ? "'" : '"'
21+
const alternative = quote === '"' ? "'" : '"'
2222

2323
if (quote !== '"' && quote !== "'") {
2424
throw new Error('Invalid quote `' + quote + '`, expected `\'` or `"`')
2525
}
2626

2727
/** @type {Context} */
28-
var context = {
28+
const context = {
2929
valid: options.allowParseErrors ? 0 : 1,
3030
safe: options.allowDangerousCharacters ? 0 : 1,
3131
schema: options.space === 'svg' ? svg : html,

lib/omission/closing.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const closing = omission({
3737
* @type {OmitHandle}
3838
*/
3939
function headOrColgroupOrCaption(_, index, parent) {
40-
var next = siblingAfter(parent, index, true)
40+
const next = siblingAfter(parent, index, true)
4141
return !next || (!comment(next) && !whitespaceStart(next))
4242
}
4343

@@ -47,7 +47,7 @@ function headOrColgroupOrCaption(_, index, parent) {
4747
* @type {OmitHandle}
4848
*/
4949
function html(_, index, parent) {
50-
var next = siblingAfter(parent, index)
50+
const next = siblingAfter(parent, index)
5151
return !next || !comment(next)
5252
}
5353

@@ -57,7 +57,7 @@ function html(_, index, parent) {
5757
* @type {OmitHandle}
5858
*/
5959
function body(_, index, parent) {
60-
var next = siblingAfter(parent, index)
60+
const next = siblingAfter(parent, index)
6161
return !next || !comment(next)
6262
}
6363

@@ -67,7 +67,7 @@ function body(_, index, parent) {
6767
* @type {OmitHandle}
6868
*/
6969
function p(_, index, parent) {
70-
var next = siblingAfter(parent, index)
70+
const next = siblingAfter(parent, index)
7171
return next
7272
? isElement(next, [
7373
'address',
@@ -120,7 +120,7 @@ function p(_, index, parent) {
120120
* @type {OmitHandle}
121121
*/
122122
function li(_, index, parent) {
123-
var next = siblingAfter(parent, index)
123+
const next = siblingAfter(parent, index)
124124
return !next || isElement(next, 'li')
125125
}
126126

@@ -130,7 +130,7 @@ function li(_, index, parent) {
130130
* @type {OmitHandle}
131131
*/
132132
function dt(_, index, parent) {
133-
var next = siblingAfter(parent, index)
133+
const next = siblingAfter(parent, index)
134134
return next && isElement(next, ['dt', 'dd'])
135135
}
136136

@@ -140,7 +140,7 @@ function dt(_, index, parent) {
140140
* @type {OmitHandle}
141141
*/
142142
function dd(_, index, parent) {
143-
var next = siblingAfter(parent, index)
143+
const next = siblingAfter(parent, index)
144144
return !next || isElement(next, ['dt', 'dd'])
145145
}
146146

@@ -150,7 +150,7 @@ function dd(_, index, parent) {
150150
* @type {OmitHandle}
151151
*/
152152
function rubyElement(_, index, parent) {
153-
var next = siblingAfter(parent, index)
153+
const next = siblingAfter(parent, index)
154154
return !next || isElement(next, ['rp', 'rt'])
155155
}
156156

@@ -160,7 +160,7 @@ function rubyElement(_, index, parent) {
160160
* @type {OmitHandle}
161161
*/
162162
function optgroup(_, index, parent) {
163-
var next = siblingAfter(parent, index)
163+
const next = siblingAfter(parent, index)
164164
return !next || isElement(next, 'optgroup')
165165
}
166166

@@ -170,7 +170,7 @@ function optgroup(_, index, parent) {
170170
* @type {OmitHandle}
171171
*/
172172
function option(_, index, parent) {
173-
var next = siblingAfter(parent, index)
173+
const next = siblingAfter(parent, index)
174174
return !next || isElement(next, ['option', 'optgroup'])
175175
}
176176

@@ -180,7 +180,7 @@ function option(_, index, parent) {
180180
* @type {OmitHandle}
181181
*/
182182
function menuitem(_, index, parent) {
183-
var next = siblingAfter(parent, index)
183+
const next = siblingAfter(parent, index)
184184
return !next || isElement(next, ['menuitem', 'hr', 'menu'])
185185
}
186186

@@ -190,7 +190,7 @@ function menuitem(_, index, parent) {
190190
* @type {OmitHandle}
191191
*/
192192
function thead(_, index, parent) {
193-
var next = siblingAfter(parent, index)
193+
const next = siblingAfter(parent, index)
194194
return next && isElement(next, ['tbody', 'tfoot'])
195195
}
196196

@@ -200,7 +200,7 @@ function thead(_, index, parent) {
200200
* @type {OmitHandle}
201201
*/
202202
function tbody(_, index, parent) {
203-
var next = siblingAfter(parent, index)
203+
const next = siblingAfter(parent, index)
204204
return !next || isElement(next, ['tbody', 'tfoot'])
205205
}
206206

@@ -219,7 +219,7 @@ function tfoot(_, index, parent) {
219219
* @type {OmitHandle}
220220
*/
221221
function tr(_, index, parent) {
222-
var next = siblingAfter(parent, index)
222+
const next = siblingAfter(parent, index)
223223
return !next || isElement(next, 'tr')
224224
}
225225

@@ -229,6 +229,6 @@ function tr(_, index, parent) {
229229
* @type {OmitHandle}
230230
*/
231231
function cells(_, index, parent) {
232-
var next = siblingAfter(parent, index)
232+
const next = siblingAfter(parent, index)
233233
return !next || isElement(next, ['td', 'th'])
234234
}

lib/omission/omission.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @typedef {import('../types.js').OmitHandle} OmitHandle
33
*/
44

5-
var own = {}.hasOwnProperty
5+
const own = {}.hasOwnProperty
66

77
/**
88
* Factory to check if a given node can have a tag omitted.

lib/omission/opening.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const opening = omission({
2424
* @type {OmitHandle}
2525
*/
2626
function html(node) {
27-
var head = siblingAfter(node, -1)
27+
const head = siblingAfter(node, -1)
2828
return !head || !comment(head)
2929
}
3030

@@ -34,12 +34,12 @@ function html(node) {
3434
* @type {OmitHandle}
3535
*/
3636
function head(node) {
37-
var children = node.children
37+
const children = node.children
3838
/** @type {Array.<string>} */
39-
var seen = []
40-
var index = -1
39+
const seen = []
40+
let index = -1
4141
/** @type {Child} */
42-
var child
42+
let child
4343

4444
while (++index < children.length) {
4545
child = children[index]
@@ -58,7 +58,7 @@ function head(node) {
5858
* @type {OmitHandle}
5959
*/
6060
function body(node) {
61-
var head = siblingAfter(node, -1, true)
61+
const head = siblingAfter(node, -1, true)
6262

6363
return (
6464
!head ||
@@ -77,8 +77,8 @@ function body(node) {
7777
* @type {OmitHandle}
7878
*/
7979
function colgroup(node, index, parent) {
80-
var previous = siblingBefore(parent, index)
81-
var head = siblingAfter(node, -1, true)
80+
const previous = siblingBefore(parent, index)
81+
const head = siblingAfter(node, -1, true)
8282

8383
// Previous colgroup was already omitted.
8484
if (
@@ -97,8 +97,8 @@ function colgroup(node, index, parent) {
9797
* @type {OmitHandle}
9898
*/
9999
function tbody(node, index, parent) {
100-
var previous = siblingBefore(parent, index)
101-
var head = siblingAfter(node, -1)
100+
const previous = siblingBefore(parent, index)
101+
const head = siblingAfter(node, -1)
102102

103103
// Previous table section was already omitted.
104104
if (

lib/omission/util/siblings.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ function siblings(increment) {
2525
* @returns {Child}
2626
*/
2727
function sibling(parent, index, includeWhitespace) {
28-
var siblings = parent && parent.children
29-
var offset = index + increment
30-
var next = siblings && siblings[offset]
28+
const siblings = parent && parent.children
29+
let offset = index + increment
30+
let next = siblings && siblings[offset]
3131

3232
if (!includeWhitespace) {
3333
while (next && whitespace(next)) {

lib/omission/util/whitespace-start.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {whitespace} from 'hast-util-whitespace'
88

99
/** @type {import('unist-util-is').AssertPredicate<Text>} */
1010
// @ts-ignore
11-
var isText = convert('text')
11+
const isText = convert('text')
1212

1313
/**
1414
* Check if `node` starts with whitespace.

lib/text.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import {stringifyEntities} from 'stringify-entities'
1111
*/
1212
export function text(ctx, node, _, parent) {
1313
// Check if content of `node` should be escaped.
14-
return parent && (parent.tagName === 'script' || parent.tagName === 'style')
14+
return parent &&
15+
parent.type === 'element' &&
16+
// @ts-expect-error: hush.
17+
(parent.tagName === 'script' || parent.tagName === 'style')
1518
? node.value
1619
: stringifyEntities(
1720
node.value,

lib/tree.js

+25-31
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ import {stringify as commas} from 'comma-separated-tokens'
1313
import {stringifyEntities} from 'stringify-entities'
1414
import {ccount} from 'ccount'
1515
import {constants} from './constants.js'
16-
import {comment} from './comment'
17-
import {doctype} from './doctype'
18-
import {raw} from './raw'
19-
import {text} from './text'
16+
import {comment} from './comment.js'
17+
import {doctype} from './doctype.js'
18+
import {raw} from './raw.js'
19+
import {text} from './text.js'
2020

2121
/**
2222
* @type {Object.<string, Handle>}
2323
*/
24-
var handlers = {
24+
const handlers = {
2525
comment,
2626
doctype,
2727
element,
@@ -32,7 +32,7 @@ var handlers = {
3232
text
3333
}
3434

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

3737
/**
3838
* @type {Handle}
@@ -57,9 +57,9 @@ export function one(ctx, node, index, parent) {
5757
*/
5858
export function all(ctx, parent) {
5959
/** @type {Array.<string>} */
60-
var results = []
61-
var children = (parent && parent.children) || []
62-
var index = -1
60+
const results = []
61+
const children = (parent && parent.children) || []
62+
let index = -1
6363

6464
while (++index < children.length) {
6565
results[index] = one(ctx, children[index], index, parent)
@@ -74,28 +74,24 @@ export function all(ctx, parent) {
7474
*/
7575
// eslint-disable-next-line complexity
7676
export function element(ctx, node, index, parent) {
77-
var schema = ctx.schema
78-
var omit = schema.space === 'svg' ? undefined : ctx.omit
79-
var selfClosing =
77+
const schema = ctx.schema
78+
const omit = schema.space === 'svg' ? undefined : ctx.omit
79+
let selfClosing =
8080
schema.space === 'svg'
8181
? ctx.closeEmpty
8282
: ctx.voids.includes(node.tagName.toLowerCase())
8383
/** @type {Array.<string>} */
84-
var parts = []
84+
const parts = []
8585
/** @type {string} */
86-
var attrs
87-
/** @type {string} */
88-
var content
89-
/** @type {string} */
90-
var last
86+
let last
9187

9288
if (schema.space === 'html' && node.tagName === 'svg') {
9389
ctx.schema = svg
9490
}
9591

96-
attrs = serializeAttributes(ctx, node.properties)
92+
const attrs = serializeAttributes(ctx, node.properties)
9793

98-
content = all(
94+
const content = all(
9995
ctx,
10096
schema.space === 'html' && node.tagName === 'template' ? node.content : node
10197
)
@@ -143,14 +139,14 @@ export function element(ctx, node, index, parent) {
143139
*/
144140
function serializeAttributes(ctx, props) {
145141
/** @type {Array.<string>} */
146-
var values = []
147-
var index = -1
142+
const values = []
143+
let index = -1
148144
/** @type {string} */
149-
var key
145+
let key
150146
/** @type {string} */
151-
var value
147+
let value
152148
/** @type {string} */
153-
var last
149+
let last
154150

155151
for (key in props) {
156152
if (props[key] !== undefined && props[key] !== null) {
@@ -179,12 +175,10 @@ function serializeAttributes(ctx, props) {
179175
*/
180176
// eslint-disable-next-line complexity
181177
function serializeAttribute(ctx, key, value) {
182-
var info = find(ctx.schema, key)
183-
var quote = ctx.quote
184-
/** @type {string} */
185-
var result
178+
const info = find(ctx.schema, key)
179+
let quote = ctx.quote
186180
/** @type {string} */
187-
var name
181+
let result
188182

189183
if (info.overloadedBoolean && (value === info.attribute || value === '')) {
190184
value = true
@@ -204,7 +198,7 @@ function serializeAttribute(ctx, key, value) {
204198
return ''
205199
}
206200

207-
name = stringifyEntities(
201+
const name = stringifyEntities(
208202
info.attribute,
209203
Object.assign({}, ctx.entities, {
210204
// Always encode without parse errors in non-HTML.

package.json

+2-6
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
6666
"test-api": "node test/index.js",
6767
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
68-
"test": "npm run format && npm run test-coverage"
68+
"test": "npm run build && npm run format && npm run test-coverage"
6969
},
7070
"prettier": {
7171
"tabWidth": 2,
@@ -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": [

0 commit comments

Comments
 (0)