Skip to content

Commit 1bba23e

Browse files
committed
Refactor code-style
1 parent 748d070 commit 1bba23e

File tree

4 files changed

+67
-84
lines changed

4 files changed

+67
-84
lines changed

index.js

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,12 @@ import {toString} from 'mdast-util-to-string'
3333
*/
3434
// eslint-disable-next-line complexity
3535
export function headingRange(node, options, handler) {
36-
var test = options
36+
let test = options
3737
/** @type {Array.<Node>} */
3838
// @ts-ignore looks like children.
39-
var children = node.children || []
40-
var index = -1
39+
const children = node.children || []
4140
/** @type {boolean} */
42-
var ignoreFinalDefinitions
43-
/** @type {number} */
44-
var depth
45-
/** @type {number} */
46-
var start
47-
/** @type {number} */
48-
var end
49-
/** @type {Array.<Node>} */
50-
var nodes
51-
/** @type {Array.<Node>} */
52-
var result
53-
/** @type {Node} */
54-
var child
41+
let ignoreFinalDefinitions
5542

5643
// Object, not regex.
5744
if (test && typeof test === 'object' && !('exec' in test)) {
@@ -77,9 +64,17 @@ export function headingRange(node, options, handler) {
7764
)
7865
}
7966

67+
let index = -1
68+
/** @type {number} */
69+
let start
70+
/** @type {number} */
71+
let end
72+
/** @type {number} */
73+
let depth
74+
8075
// Find the range.
8176
while (++index < children.length) {
82-
child = children[index]
77+
const child = children[index]
8378

8479
if (child.type === 'heading') {
8580
// @ts-expect-error: looks like a heading.
@@ -110,23 +105,21 @@ export function headingRange(node, options, handler) {
110105
}
111106
}
112107

113-
nodes = handler(
108+
/** @type {Array.<Node>} */
109+
const nodes = handler(
114110
// @ts-ignore `start` points to a heading.
115111
children[start],
116112
children.slice(start + 1, end),
117113
children[end],
118-
{
119-
parent: node,
120-
start,
121-
end: children[end] ? end : null
122-
}
114+
{parent: node, start, end: children[end] ? end : null}
123115
)
124116

125117
if (nodes) {
126118
// Ensure no empty nodes are inserted.
127119
// This could be the case if `end` is in `nodes` but no `end` node exists.
128-
result = []
129-
index = -1
120+
/** @type {Array.<Node>} */
121+
const result = []
122+
let index = -1
130123

131124
while (++index < nodes.length) {
132125
if (nodes[index]) result.push(nodes[index])

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,7 @@
6767
"trailingComma": "none"
6868
},
6969
"xo": {
70-
"prettier": true,
71-
"rules": {
72-
"no-var": "off",
73-
"prefer-arrow-callback": "off"
74-
}
70+
"prettier": true
7571
},
7672
"remarkConfig": {
7773
"plugins": [

readme.md

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,26 @@ Bar.
3636
And our script, `example.js`, looks as follows:
3737

3838
```js
39-
import toVFile from 'to-vfile'
40-
import remark from 'remark'
39+
import {readSync} from 'to-vfile'
40+
import {remark} from 'remark'
4141
import {headingRange} from 'mdast-util-heading-range'
4242

43+
const file = readSync('example.md')
44+
4345
remark()
4446
.use(plugin)
45-
.process(vfile.readSync('example.md'), function(err, file) {
46-
if (err) throw err
47+
.process(file)
48+
.then((file) => {
4749
console.log(String(file))
4850
})
4951

5052
function plugin() {
51-
return transform
52-
53-
function transform(tree) {
54-
headingRange(tree, 'foo', handler)
55-
}
56-
57-
function handler(start, nodes, end) {
58-
return [
53+
return (tree) => {
54+
headingRange(tree, 'foo', (start, nodes, end) => [
5955
start,
6056
{type: 'paragraph', children: [{type: 'text', value: 'Qux.'}]},
6157
end
62-
]
58+
])
6359
}
6460
}
6561
```

test.js

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,39 @@ import test from 'tape'
99
import remark from 'remark'
1010
import {headingRange} from './index.js'
1111

12-
test('mdast-util-heading-range()', function (t) {
13-
t.plan(57)
12+
test('mdast-util-heading-range()', (t) => {
13+
t.plan(58)
1414

1515
t.equal(typeof headingRange, 'function', 'should be a function')
1616

1717
t.throws(
18-
function () {
18+
() => {
1919
headingRange(
2020
/** @type {Root} */ ({type: 'root', children: []}),
2121
null,
22-
function () {}
22+
() => {}
2323
)
2424
},
2525
/^TypeError: Expected `string`, `regexp`, or `function` for `test`, not `null`$/,
2626
'should throw when `null` is passed in'
2727
)
2828

2929
t.throws(
30-
function () {
30+
() => {
3131
headingRange(
3232
/** @type {Root} */ ({type: 'root', children: []}),
3333
undefined,
34-
function () {}
34+
() => {}
3535
)
3636
},
3737
/^TypeError: Expected `string`, `regexp`, or `function` for `test`, not `undefined`$/,
3838
'should throw when `undefined` is passed in'
3939
)
4040

41+
t.doesNotThrow(() => {
42+
headingRange(/** @type {Root} */ ({type: 'root'}), 'x', () => {})
43+
}, 'should not throw when a non-parent is passed')
44+
4145
t.equal(
4246
process(
4347
t,
@@ -63,7 +67,7 @@ test('mdast-util-heading-range()', function (t) {
6367
t,
6468
['# Fo', '', '## Fooooo', '', 'Bar', '', '# Fo', ''].join('\n'),
6569
/** @type {Options} */
66-
function (value) {
70+
(value) => {
6771
return value.toLowerCase().indexOf('foo') === 0
6872
}
6973
),
@@ -133,58 +137,52 @@ test('mdast-util-heading-range()', function (t) {
133137
)
134138

135139
remark()
136-
.use(function () {
140+
.use(() => {
137141
return function (node) {
138-
headingRange(node, 'foo', function () {
142+
headingRange(node, 'foo', () => {
139143
return null
140144
})
141145
}
142146
})
143-
.process(
144-
['Foo', '', '## Foo', '', 'Bar', ''].join('\n'),
145-
function (error, file) {
146-
t.ifError(error, 'should not fail (#1)')
147+
.process(['Foo', '', '## Foo', '', 'Bar', ''].join('\n'), (error, file) => {
148+
t.ifError(error, 'should not fail (#1)')
147149

148-
t.equal(
149-
String(file),
150-
['Foo', '', '## Foo', '', 'Bar', ''].join('\n'),
151-
'should not remove anything when `null` is given'
152-
)
153-
}
154-
)
150+
t.equal(
151+
String(file),
152+
['Foo', '', '## Foo', '', 'Bar', ''].join('\n'),
153+
'should not remove anything when `null` is given'
154+
)
155+
})
155156

156157
remark()
157-
.use(function () {
158+
.use(() => {
158159
return function (node) {
159-
headingRange(node, 'foo', function () {
160+
headingRange(node, 'foo', () => {
160161
return []
161162
})
162163
}
163164
})
164-
.process(
165-
['Foo', '', '## Foo', '', 'Bar', ''].join('\n'),
166-
function (error, file) {
167-
t.ifError(error, 'should not fail (#2)')
165+
.process(['Foo', '', '## Foo', '', 'Bar', ''].join('\n'), (error, file) => {
166+
t.ifError(error, 'should not fail (#2)')
168167

169-
t.equal(
170-
String(file),
171-
['Foo', ''].join('\n'),
172-
'should replace all previous nodes otherwise'
173-
)
174-
}
175-
)
168+
t.equal(
169+
String(file),
170+
['Foo', ''].join('\n'),
171+
'should replace all previous nodes otherwise'
172+
)
173+
})
176174

177175
remark()
178-
.use(function () {
176+
.use(() => {
179177
return function (node) {
180-
headingRange(node, 'foo', function (start, _, end) {
178+
headingRange(node, 'foo', (start, _, end) => {
181179
return [start, {type: 'thematicBreak'}, end]
182180
})
183181
}
184182
})
185183
.process(
186184
['Foo', '', '## Foo', '', 'Bar', '', '## Baz', ''].join('\n'),
187-
function (error, file) {
185+
(error, file) => {
188186
t.ifError(error, 'should not fail (#3)')
189187

190188
t.equal(
@@ -196,9 +194,9 @@ test('mdast-util-heading-range()', function (t) {
196194
)
197195

198196
remark()
199-
.use(function () {
197+
.use(() => {
200198
return function (node) {
201-
headingRange(node, 'foo', function (start, nodes, end) {
199+
headingRange(node, 'foo', (start, nodes, end) => {
202200
t.equal(nodes.length, 3)
203201
return [start, ...nodes, end]
204202
})
@@ -208,7 +206,7 @@ test('mdast-util-heading-range()', function (t) {
208206
['# Alpha', '', '## Foo', '', 'one', '', 'two', '', 'three', ''].join(
209207
'\n'
210208
),
211-
function (error, file) {
209+
(error, file) => {
212210
t.ifError(error, 'should not fail (#4)')
213211

214212
t.equal(
@@ -325,9 +323,9 @@ test('mdast-util-heading-range()', function (t) {
325323
*/
326324
function process(t, value, options) {
327325
return remark()
328-
.use(function () {
326+
.use(() => {
329327
return function (node) {
330-
headingRange(node, options, function (start, _, end, scope) {
328+
headingRange(node, options, (start, _, end, scope) => {
331329
t.equal(typeof scope.start, 'number')
332330
t.assert(typeof scope.end === 'number' || scope.end === null)
333331
t.equal(scope.parent.type, 'root')

0 commit comments

Comments
 (0)