Skip to content

Commit 63e73cf

Browse files
committed
Refactor code-style
1 parent 1343abc commit 63e73cf

13 files changed

+74
-82
lines changed

index.js

+11-14
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
* @typedef {Node & {children: never, value: never}} _Void
99
*/
1010

11-
import nodeAssert from 'assert'
11+
import nodeAssert from 'node:assert'
1212
import {inspect} from './inspect.js'
1313

14-
var own = {}.hasOwnProperty
14+
const own = {}.hasOwnProperty
1515

1616
/**
1717
* Assert that `node` is a valid unist node.
@@ -60,10 +60,10 @@ export function _void(node, parent) {
6060
}
6161

6262
// Identifier to check if a value is seen.
63-
var ID = '__unist__'
63+
const ID = '__unist__'
6464

6565
// List of specced properties.
66-
var defined = new Set(['type', 'value', 'children', 'position'])
66+
const defined = new Set(['type', 'value', 'children', 'position'])
6767

6868
/**
6969
* Wrapper that adds the current node (and parent, if available) to error
@@ -103,13 +103,7 @@ export function wrap(fn) {
103103
* @returns {asserts node is Node}
104104
*/
105105
function assertNode(node) {
106-
var index = -1
107-
/** @type {Parent} */
108-
var parent
109-
/** @type {unknown} */
110-
var child
111-
/** @type {string} */
112-
var key
106+
let index = -1
113107

114108
nodeAssert.ok(
115109
node && typeof node === 'object' && !Array.isArray(node),
@@ -139,6 +133,9 @@ function assertNode(node) {
139133
// @ts-expect-error Looks like an indexed object.
140134
position(node.position)
141135

136+
/** @type {string} */
137+
let key
138+
142139
for (key in node) {
143140
if (!defined.has(key)) {
144141
vanilla(key, node[key])
@@ -147,17 +144,17 @@ function assertNode(node) {
147144

148145
// @ts-expect-error Looks like an indexed object.
149146
if (node.children !== null && node.children !== undefined) {
147+
/** @type {Parent} */
150148
// @ts-expect-error Looks like parent.
151-
parent = node
149+
const parent = node
152150
nodeAssert.ok(
153151
Array.isArray(parent.children),
154152
'`children` should be an array'
155153
)
156154
index = -1
157155

158156
while (++index < parent.children.length) {
159-
child = parent.children[index]
160-
assert(child, parent)
157+
assert(parent.children[index], parent)
161158
}
162159
}
163160
}

index.test-d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import {expectType, expectNotType} from 'tsd'
22
import {Node, Parent} from 'unist'
33
import {assert, parent} from './index.js'
44

5-
var emptyNode = {type: 'a'}
6-
var literalNode = {type: 'b', value: 'c'}
7-
var parentNode = {type: 'd', children: [emptyNode, literalNode]}
5+
const emptyNode = {type: 'a'}
6+
const literalNode = {type: 'b', value: 'c'}
7+
const parentNode = {type: 'd', children: [emptyNode, literalNode]}
88

99
expectNotType<Node>(emptyNode)
1010
expectNotType<Node>(literalNode)

inspect.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {inspect as utilInspect} from 'util'
1+
import {inspect as utilInspect} from 'node:util'
22

33
/**
44
* @param {unknown} value

package.json

+1-6
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,7 @@
7373
"trailingComma": "none"
7474
},
7575
"xo": {
76-
"prettier": true,
77-
"rules": {
78-
"no-var": "off",
79-
"prefer-arrow-callback": "off",
80-
"unicorn/prefer-node-protocol": "off"
81-
}
76+
"prettier": true
8277
},
8378
"remarkConfig": {
8479
"plugins": [

test/children.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
import test from 'tape'
22
import {assert} from '../index.js'
33

4-
test('children', function (t) {
4+
test('children', (t) => {
55
t.throws(
6-
function () {
6+
() => {
77
assert({type: 'foo', children: {alpha: 'bravo'}})
88
},
99
/`children` should be an array: `{ type: 'foo', children: { alpha: 'bravo' } }`$/,
1010
'should throw if given a non-node child in children'
1111
)
1212

1313
t.throws(
14-
function () {
14+
() => {
1515
assert({type: 'foo', children: ['one']})
1616
},
1717
/node should be an object: `'one'` in `{ type: 'foo', children: \[ 'one' ] }`$/,
1818
'should throw if given a non-node child in children'
1919
)
2020

21-
t.doesNotThrow(function () {
21+
t.doesNotThrow(() => {
2222
assert({type: 'parent', children: [{type: 'text', value: 'alpha'}]})
2323
}, 'should not throw on vald children')
2424

2525
t.throws(
26-
function () {
26+
() => {
2727
assert({
2828
type: 'foo',
2929
children: [

test/literal.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
import test from 'tape'
22
import {literal} from '../index.js'
33

4-
test('literal()', function (t) {
4+
test('literal()', (t) => {
55
t.throws(
6-
function () {
6+
() => {
77
literal({})
88
},
99
/node should have a type: `{}`$/,
1010
'should throw the same errors as `assert()`'
1111
)
1212

1313
t.throws(
14-
function () {
14+
() => {
1515
literal({type: 'strong', children: []})
1616
},
1717
/literal should not have `children`: `{ type: 'strong', children: \[] }`$/,
1818
'should throw if the given node has `children`'
1919
)
2020

2121
t.throws(
22-
function () {
22+
() => {
2323
literal({type: 'break'})
2424
},
2525
/literal should have `value`: `{ type: 'break' }`$/,
2626
'should throw if the given node has no `value`'
2727
)
2828

29-
t.doesNotThrow(function () {
29+
t.doesNotThrow(() => {
3030
literal({type: 'text', value: 'foo'})
3131
}, 'should not throw on valid text nodes')
3232

test/node.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
import test from 'tape'
22
import {assert} from '../index.js'
33

4-
test('node', function (t) {
4+
test('node', (t) => {
55
t.throws(
6-
function () {
6+
() => {
77
assert()
88
},
99
/node should be an object: `undefined`$/,
1010
'should throw if not given a node (#1)'
1111
)
1212

1313
t.throws(
14-
function () {
14+
() => {
1515
assert(null)
1616
},
1717
/node should be an object: `null`$/,
1818
'should throw if not given a node (#2)'
1919
)
2020

2121
t.throws(
22-
function () {
22+
() => {
2323
assert('foo')
2424
},
2525
/node should be an object: `'foo'`$/,
2626
'should throw if given a non-node (#1)'
2727
)
2828

2929
t.throws(
30-
function () {
30+
() => {
3131
assert(6)
3232
},
3333
/node should be an object: `6`$/,

test/non-defined.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import test from 'tape'
22
import {assert} from '../index.js'
33

4-
test('non-defined', function (t) {
5-
t.doesNotThrow(function () {
4+
test('non-defined', (t) => {
5+
t.doesNotThrow(() => {
66
assert({
77
type: 'element',
88
properties: {
@@ -18,7 +18,7 @@ test('non-defined', function (t) {
1818
}, 'should not throw if non-defined properties are found')
1919

2020
t.throws(
21-
function () {
21+
() => {
2222
assert({
2323
type: 'break',
2424
data: {foo: Function}

test/parent.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
import test from 'tape'
22
import {parent} from '../index.js'
33

4-
test('parent()', function (t) {
4+
test('parent()', (t) => {
55
t.throws(
6-
function () {
6+
() => {
77
parent({})
88
},
99
/node should have a type: `{}`$/,
1010
'should throw the same errors as `assert()`'
1111
)
1212

1313
t.throws(
14-
function () {
14+
() => {
1515
parent({type: 'text', value: 'foo'})
1616
},
1717
/parent should not have `value`: `{ type: 'text', value: 'foo' }`$/,
1818
'should throw if the given node has a `value`'
1919
)
2020

2121
t.throws(
22-
function () {
22+
() => {
2323
parent({type: 'break'})
2424
},
2525
/parent should have `children`: `{ type: 'break' }`$/,
2626
'should throw if the given node has `children`'
2727
)
2828

29-
t.doesNotThrow(function () {
29+
t.doesNotThrow(() => {
3030
parent({type: 'strong', children: []})
3131
}, 'should not throw on valid void nodes')
3232

test/position.js

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,97 @@
11
import test from 'tape'
22
import {assert} from '../index.js'
33

4-
test('position', function (t) {
4+
test('position', (t) => {
55
t.throws(
6-
function () {
6+
() => {
77
assert({type: 'foo', position: 1})
88
},
99
/`position` should be an object: `{ type: 'foo', position: 1 }`$/,
1010
'should throw if given a non-object `position`'
1111
)
1212

13-
t.doesNotThrow(function () {
13+
t.doesNotThrow(() => {
1414
assert({type: 'foo', position: null})
1515
}, 'should not throw if given a null `position`')
1616

17-
t.doesNotThrow(function () {
17+
t.doesNotThrow(() => {
1818
assert({type: 'foo', position: {}})
1919
}, 'should not throw if given an empty `position` object')
2020

2121
t.throws(
22-
function () {
22+
() => {
2323
assert({type: 'foo', position: {start: 1}})
2424
},
2525
/`position.start` should be an object: `{ type: 'foo', position: { start: 1 } }`$/,
2626
'should throw if given a non-object `position.start`'
2727
)
2828

29-
t.doesNotThrow(function () {
29+
t.doesNotThrow(() => {
3030
assert({type: 'foo', position: {start: null}})
3131
}, 'should not throw if given a null `position.start`')
3232

33-
t.doesNotThrow(function () {
33+
t.doesNotThrow(() => {
3434
assert({type: 'foo', position: {start: {}}})
3535
}, 'should not throw if given an empty `position.start` object')
3636

3737
t.throws(
38-
function () {
38+
() => {
3939
assert({type: 'foo', position: {end: 1}})
4040
},
4141
/`position.end` should be an object: `{ type: 'foo', position: { end: 1 } }`$/,
4242
'should throw if given a non-object `position.end`'
4343
)
4444

45-
t.doesNotThrow(function () {
45+
t.doesNotThrow(() => {
4646
assert({type: 'foo', position: {end: null}})
4747
}, 'should not throw if given a null `position.end`')
4848

49-
t.doesNotThrow(function () {
49+
t.doesNotThrow(() => {
5050
assert({type: 'foo', position: {end: {}}})
5151
}, 'should not throw if given an empty `position.end` object')
5252

53-
t.doesNotThrow(function () {
53+
t.doesNotThrow(() => {
5454
assert({type: 'foo', position: {start: {line: null}}})
5555
}, 'should not throw if given a `position.start.line` to `null`')
5656

57-
t.doesNotThrow(function () {
57+
t.doesNotThrow(() => {
5858
assert({type: 'foo', position: {start: {column: null}}})
5959
}, 'should not throw if given a `position.start.column` to `null`')
6060

61-
t.doesNotThrow(function () {
61+
t.doesNotThrow(() => {
6262
assert({type: 'foo', position: {end: {line: null}}})
6363
}, 'should not throw if given a `position.end.line` to `null`')
6464

65-
t.doesNotThrow(function () {
65+
t.doesNotThrow(() => {
6666
assert({type: 'foo', position: {end: {column: null}}})
6767
}, 'should not throw if given a `position.end.column` to `null`')
6868

6969
t.throws(
70-
function () {
70+
() => {
7171
assert({type: 'foo', position: {start: {line: 0}}})
7272
},
7373
/`position.start.line` should be gte `1`: `{ type: 'foo', position: { start: { line: 0 } } }`$/,
7474
'should throw if `position.start.line` is less than 1'
7575
)
7676

7777
t.throws(
78-
function () {
78+
() => {
7979
assert({type: 'foo', position: {start: {column: 0}}})
8080
},
8181
/`position.start.column` should be gte `1`: `{ type: 'foo', position: { start: { column: 0 } } }`$/,
8282
'should throw if `position.start.column` is less than 1'
8383
)
8484

8585
t.throws(
86-
function () {
86+
() => {
8787
assert({type: 'foo', position: {end: {line: 0}}})
8888
},
8989
/`position.end.line` should be gte `1`: `{ type: 'foo', position: { end: { line: 0 } } }`$/,
9090
'should throw if `position.end.line` is less than 1'
9191
)
9292

9393
t.throws(
94-
function () {
94+
() => {
9595
assert({type: 'foo', position: {end: {column: 0}}})
9696
},
9797
/`position.end.column` should be gte `1`: `{ type: 'foo', position: { end: { column: 0 } } }`$/,

0 commit comments

Comments
 (0)