Skip to content

Commit 69ef233

Browse files
committed
Refactor code-style
1 parent 6b7f01e commit 69ef233

File tree

8 files changed

+50
-54
lines changed

8 files changed

+50
-54
lines changed

Diff for: lib/core.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ import {parseSelector} from 'hast-util-parse-selector'
2525
import {parse as spaces} from 'space-separated-tokens'
2626
import {parse as commas} from 'comma-separated-tokens'
2727

28-
var buttonTypes = new Set(['menu', 'submit', 'reset', 'button'])
28+
const buttonTypes = new Set(['menu', 'submit', 'reset', 'button'])
2929

30-
var own = {}.hasOwnProperty
30+
const own = {}.hasOwnProperty
3131

3232
/**
3333
* @param {Schema} schema
3434
* @param {string} defaultTagName
3535
* @param {Array.<string>} [caseSensitive]
3636
*/
3737
export function core(schema, defaultTagName, caseSensitive) {
38-
var adjust = caseSensitive && createAdjustMap(caseSensitive)
38+
const adjust = caseSensitive && createAdjustMap(caseSensitive)
3939

4040
const h =
4141
/**
@@ -56,11 +56,9 @@ export function core(schema, defaultTagName, caseSensitive) {
5656
* @returns {HResult}
5757
*/
5858
function (selector, properties, ...children) {
59-
var index = -1
59+
let index = -1
6060
/** @type {HResult} */
61-
var node
62-
/** @type {string} */
63-
var key
61+
let node
6462

6563
if (selector === undefined || selector === null) {
6664
node = {type: 'root', children: []}
@@ -76,6 +74,9 @@ export function core(schema, defaultTagName, caseSensitive) {
7674

7775
// Handle props.
7876
if (isProperties(properties, node.tagName)) {
77+
/** @type {string} */
78+
let key
79+
7980
for (key in properties) {
8081
if (own.call(properties, key)) {
8182
// @ts-ignore `node.properties` is set.
@@ -142,12 +143,10 @@ function isProperties(value, name) {
142143
* @returns {void}
143144
*/
144145
function addProperty(schema, properties, key, value) {
145-
var info = find(schema, key)
146-
var index = -1
146+
const info = find(schema, key)
147+
let index = -1
147148
/** @type {HPropertyValue} */
148-
var result
149-
/** @type {Array.<string|number>} */
150-
var finalResult
149+
let result
151150

152151
// Ignore nullish and NaN values.
153152
if (value === undefined || value === null) return
@@ -180,7 +179,8 @@ function addProperty(schema, properties, key, value) {
180179
}
181180

182181
if (Array.isArray(result)) {
183-
finalResult = []
182+
/** @type {Array.<string|number>} */
183+
const finalResult = []
184184

185185
while (++index < result.length) {
186186
// @ts-ignore Assume no booleans in array.
@@ -205,7 +205,7 @@ function addProperty(schema, properties, key, value) {
205205
* @returns {void}
206206
*/
207207
function addChild(nodes, value) {
208-
var index = -1
208+
let index = -1
209209

210210
if (value === undefined || value === null) {
211211
// Empty.
@@ -258,9 +258,9 @@ function parsePrimitive(info, name, value) {
258258
*/
259259
function style(value) {
260260
/** @type {Array.<string>} */
261-
var result = []
261+
const result = []
262262
/** @type {string} */
263-
var key
263+
let key
264264

265265
for (key in value) {
266266
if (own.call(value, key)) {
@@ -277,8 +277,8 @@ function style(value) {
277277
*/
278278
function createAdjustMap(values) {
279279
/** @type {Object.<string, string>} */
280-
var result = {}
281-
var index = -1
280+
const result = {}
281+
let index = -1
282282

283283
while (++index < values.length) {
284284
result[values[index].toLowerCase()] = values[index]

Diff for: lib/runtime.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function runtime(f) {
2929
* @returns {HResult}
3030
*/
3131
function (type, props) {
32-
var {children, ...properties} = props
32+
const {children, ...properties} = props
3333
return type === null ? f(type, children) : f(type, properties, children)
3434
}
3535
)

Diff for: package.json

+1-5
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,7 @@
101101
"trailingComma": "none"
102102
},
103103
"xo": {
104-
"prettier": true,
105-
"rules": {
106-
"no-var": "off",
107-
"prefer-arrow-callback": "off"
108-
}
104+
"prettier": true
109105
},
110106
"remarkConfig": {
111107
"plugins": [

Diff for: readme.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ The following example shows how a script is injected that runs when loaded in a
268268
browser.
269269

270270
```js
271-
var tree = {type: 'root', children: []}
271+
const tree = {type: 'root', children: []}
272272

273273
tree.children.push(h('script', 'alert(1)'))
274274
```
@@ -283,11 +283,11 @@ The following example shows how an image is injected that fails loading and
283283
therefore runs code in a browser.
284284

285285
```js
286-
var tree = {type: 'root', children: []}
286+
const tree = {type: 'root', children: []}
287287

288288
// Somehow someone injected these properties instead of an expected `src` and
289289
// `alt`:
290-
var otherProps = {src: 'x', onError: 'alert(2)'}
290+
const otherProps = {src: 'x', onError: 'alert(2)'}
291291

292292
tree.children.push(h('img', {src: 'default.png', ...otherProps}))
293293
```
@@ -302,10 +302,10 @@ The following example shows how code can run in a browser because someone stored
302302
an object in a database instead of the expected string.
303303

304304
```js
305-
var tree = {type: 'root', children: []}
305+
const tree = {type: 'root', children: []}
306306

307307
// Somehow this isn’t the expected `'wooorm'`.
308-
var username = {
308+
const username = {
309309
type: 'element',
310310
tagName: 'script',
311311
children: [{type: 'text', value: 'alert(3)'}]

Diff for: script/build.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'fs'
22
import path from 'path'
33
import {svgTagNames} from 'svg-tag-names'
44

5-
var casing = svgTagNames.filter((d) => d !== d.toLowerCase())
5+
const casing = svgTagNames.filter((d) => d !== d.toLowerCase())
66

77
fs.writeFileSync(
88
path.join('lib', 'svg-case-sensitive-tag-names.js'),

Diff for: script/generate-jsx.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import acornJsx from 'acorn-jsx'
66
import {generate} from 'astring'
77
import {buildJsx} from 'estree-util-build-jsx'
88

9-
var doc = String(fs.readFileSync(path.join('test', 'jsx.jsx')))
9+
const doc = String(fs.readFileSync(path.join('test', 'jsx.jsx')))
1010

1111
fs.writeFileSync(
1212
path.join('test', 'jsx-build-jsx-classic.js'),

Diff for: test/core.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import {h, s} from '../index.js'
33
import {h as hFromRoot} from '../html.js'
44
import {s as sFromRoot} from '../svg.js'
55

6-
test('hastscript', function (t) {
6+
test('hastscript', (t) => {
77
t.equal(h, hFromRoot, '`h` should be exposed from `/html.js`')
88
t.equal(s, sFromRoot, '`s` should be exposed from `/svg.js`')
99

1010
t.equal(typeof h, 'function', 'should expose a function')
1111

12-
t.test('selector', function (t) {
12+
t.test('selector', (t) => {
1313
t.deepEqual(
1414
h(),
1515
{type: 'root', children: []},
@@ -118,8 +118,8 @@ test('hastscript', function (t) {
118118
t.end()
119119
})
120120

121-
t.test('properties', function (t) {
122-
t.test('known property names', function (t) {
121+
t.test('properties', (t) => {
122+
t.test('known property names', (t) => {
123123
t.deepEqual(
124124
h('', {className: 'foo'}),
125125
{
@@ -167,7 +167,7 @@ test('hastscript', function (t) {
167167
t.end()
168168
})
169169

170-
t.test('unknown property names', function (t) {
170+
t.test('unknown property names', (t) => {
171171
t.deepEqual(
172172
h('', {allowbigscreen: true}),
173173
{
@@ -204,7 +204,7 @@ test('hastscript', function (t) {
204204
t.end()
205205
})
206206

207-
t.test('other namespaces', function (t) {
207+
t.test('other namespaces', (t) => {
208208
t.deepEqual(
209209
h('', {'aria-valuenow': 1}),
210210
{
@@ -318,7 +318,7 @@ test('hastscript', function (t) {
318318
t.end()
319319
})
320320

321-
t.test('data property names', function (t) {
321+
t.test('data property names', (t) => {
322322
t.deepEqual(
323323
h('', {'data-foo': true}),
324324
{
@@ -410,7 +410,7 @@ test('hastscript', function (t) {
410410
t.end()
411411
})
412412

413-
t.test('unknown property values', function (t) {
413+
t.test('unknown property values', (t) => {
414414
t.deepEqual(
415415
h('', {foo: 'bar'}),
416416
{
@@ -491,7 +491,7 @@ test('hastscript', function (t) {
491491
t.end()
492492
})
493493

494-
t.test('known booleans', function (t) {
494+
t.test('known booleans', (t) => {
495495
t.deepEqual(
496496
h('', {allowFullScreen: ''}),
497497
{
@@ -528,7 +528,7 @@ test('hastscript', function (t) {
528528
t.end()
529529
})
530530

531-
t.test('known overloaded booleans', function (t) {
531+
t.test('known overloaded booleans', (t) => {
532532
t.deepEqual(
533533
h('', {download: ''}),
534534
{
@@ -565,7 +565,7 @@ test('hastscript', function (t) {
565565
t.end()
566566
})
567567

568-
t.test('known numbers', function (t) {
568+
t.test('known numbers', (t) => {
569569
t.deepEqual(
570570
h('textarea', {cols: '3'}),
571571
{
@@ -602,7 +602,7 @@ test('hastscript', function (t) {
602602
t.end()
603603
})
604604

605-
t.test('known lists', function (t) {
605+
t.test('known lists', (t) => {
606606
t.deepEqual(
607607
h('', {class: 'foo bar baz'}),
608608
{
@@ -639,7 +639,7 @@ test('hastscript', function (t) {
639639
t.end()
640640
})
641641

642-
t.test('style', function (t) {
642+
t.test('style', (t) => {
643643
t.deepEqual(
644644
h('', {style: {color: 'red', '-webkit-border-radius': '3px'}}),
645645
{
@@ -672,7 +672,7 @@ test('hastscript', function (t) {
672672
t.end()
673673
})
674674

675-
t.test('children', function (t) {
675+
t.test('children', (t) => {
676676
t.deepEqual(
677677
h('div', {}, []),
678678
{
@@ -949,7 +949,7 @@ test('hastscript', function (t) {
949949
)
950950

951951
t.throws(
952-
function () {
952+
() => {
953953
// @ts-ignore runtime.
954954
h('foo', {}, true)
955955
},
@@ -960,7 +960,7 @@ test('hastscript', function (t) {
960960
t.end()
961961
})
962962

963-
t.test('<template>', function (t) {
963+
t.test('<template>', (t) => {
964964
t.deepEqual(
965965
h('template'),
966966
{
@@ -1018,7 +1018,7 @@ test('hastscript', function (t) {
10181018
t.end()
10191019
})
10201020

1021-
t.test('svg', function (t) {
1021+
t.test('svg', (t) => {
10221022
t.deepEqual(
10231023
s(),
10241024
{type: 'root', children: []},
@@ -1146,7 +1146,7 @@ test('hastscript', function (t) {
11461146
t.end()
11471147
})
11481148

1149-
t.test('tag names', function (t) {
1149+
t.test('tag names', (t) => {
11501150
t.deepEqual(
11511151
h('', [h('DIV'), h('dIv'), h('div')]),
11521152
{

Diff for: test/jsx.jsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import test from 'tape'
22
import {u} from 'unist-builder'
33
import {h} from '../index.js'
44

5-
test('name', function (t) {
5+
test('name', (t) => {
66
t.deepEqual(<a />, h('a'), 'should support a self-closing element')
77

88
t.deepEqual(<a>b</a>, h('a', 'b'), 'should support a value as a child')
99

10-
var A = 'a'
10+
const A = 'a'
1111

1212
t.deepEqual(<A />, h(A), 'should support an uppercase tag name')
1313

@@ -39,7 +39,7 @@ test('name', function (t) {
3939
'should support a fragment with an expression'
4040
)
4141

42-
var com = {acme: {a: 'A', b: 'B'}}
42+
const com = {acme: {a: 'A', b: 'B'}}
4343

4444
t.deepEqual(
4545
<com.acme.a />,
@@ -67,7 +67,7 @@ test('name', function (t) {
6767
'should support expression value attributes'
6868
)
6969

70-
var props = {a: 1, b: 2}
70+
const props = {a: 1, b: 2}
7171

7272
t.deepEqual(
7373
<a {...props} />,
@@ -92,7 +92,7 @@ test('name', function (t) {
9292
'should support a fragment in an element (#1)'
9393
)
9494

95-
var dl = [
95+
const dl = [
9696
['Firefox', 'A red panda.'],
9797
['Chrome', 'A chemical element.']
9898
]

0 commit comments

Comments
 (0)