Skip to content

Commit 433309d

Browse files
committed
Refactor code-style
1 parent e69b3a8 commit 433309d

File tree

4 files changed

+41
-56
lines changed

4 files changed

+41
-56
lines changed

index.js

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ import {location} from 'vfile-location'
4040
* @param {Options} [options]
4141
*/
4242
export function toNlcst(tree, file, Parser, options = {}) {
43-
/** @type {ParserInstance} */
44-
var parser
45-
4643
// Crash on invalid parameters.
4744
if (!tree || !tree.type) {
4845
throw new Error('mdast-util-to-nlcst expected node')
@@ -66,7 +63,7 @@ export function toNlcst(tree, file, Parser, options = {}) {
6663
throw new Error('mdast-util-to-nlcst expected position on nodes')
6764
}
6865

69-
parser = 'parse' in Parser ? Parser : new Parser()
66+
const parser = 'parse' in Parser ? Parser : new Parser()
7067

7168
// Transform mdast into nlcst tokens, and pass these into `parser.parse` to
7269
// insert sentences, paragraphs where needed.
@@ -98,7 +95,7 @@ export function toNlcst(tree, file, Parser, options = {}) {
9895
*/
9996
function one(config, node) {
10097
/** @type {number} */
101-
var start
98+
let start
10299

103100
if (!config.ignore.includes(node.type)) {
104101
start = node.position.start.offset
@@ -141,25 +138,20 @@ function one(config, node) {
141138
* @returns {Array.<Node>}
142139
*/
143140
function all(config, parent) {
141+
let index = -1
144142
/** @type {Array.<Node>} */
145-
var result = []
146-
var index = -1
147-
/** @type {Node} */
148-
var lineEnding
149-
/** @type {Content} */
150-
var child
151-
/** @type {Point} */
152-
var end
143+
const results = []
153144
/** @type {Point} */
154-
var start
145+
let end
155146

156147
while (++index < parent.children.length) {
148+
/** @type {Content} */
157149
// @ts-ignore Assume `parent` is an mdast parent.
158-
child = parent.children[index]
159-
start = pointStart(child)
150+
const child = parent.children[index]
151+
const start = pointStart(child)
160152

161153
if (end && start.line !== end.line) {
162-
lineEnding = config.parser.tokenizeWhiteSpace(
154+
const lineEnding = config.parser.tokenizeWhiteSpace(
163155
repeat('\n', start.line - end.line)
164156
)
165157
patch(config, [lineEnding], end.offset)
@@ -168,14 +160,15 @@ function all(config, parent) {
168160
lineEnding.value = '\n\n'
169161
}
170162

171-
result.push(lineEnding)
163+
results.push(lineEnding)
172164
}
173165

174-
result = result.concat(one(config, child) || [])
166+
const result = one(config, child)
167+
if (result) results.push(...result)
175168
end = pointEnd(child)
176169
}
177170

178-
return result
171+
return results
179172
}
180173

181174
/**
@@ -189,22 +182,18 @@ function all(config, parent) {
189182
* @returns {T}
190183
*/
191184
function patch(config, nodes, offset) {
192-
var index = -1
193-
var start = offset
194-
/** @type {number} */
195-
var end
196-
/** @type {Node} */
197-
var node
185+
let index = -1
186+
let start = offset
198187

199188
while (++index < nodes.length) {
200-
node = nodes[index]
189+
const node = nodes[index]
201190

202191
if ('children' in node) {
203192
// @ts-ignore looks like a parent.
204193
patch(config, node.children, start)
205194
}
206195

207-
end = start + toString(node).length
196+
const end = start + toString(node).length
208197

209198
node.position = {
210199
start: config.place.toPoint(start),

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,7 @@
8080
"trailingComma": "none"
8181
},
8282
"xo": {
83-
"prettier": true,
84-
"rules": {
85-
"no-var": "off",
86-
"prefer-arrow-callback": "off"
87-
}
83+
"prettier": true
8884
},
8985
"remarkConfig": {
9086
"plugins": [

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ import {inspect} from 'unist-util-inspect'
3232
import fromMarkdown from 'mdast-util-from-markdown'
3333
import {toNlcst} from 'mdast-util-to-nlcst'
3434

35-
var file = new VFile('Some *foo*sball.')
36-
var mdast = fromMarkdown(file)
37-
var nlcst = toNlcst(mdast, file, ParseEnglish)
35+
const file = new VFile('Some *foo*sball.')
36+
const mdast = fromMarkdown(file)
37+
const nlcst = toNlcst(mdast, file, ParseEnglish)
3838

3939
console.log(inspect(nlcst))
4040
```

test/index.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import {ParseEnglish} from 'parse-english'
1717
import {isHidden} from 'is-hidden'
1818
import {toNlcst} from '../index.js'
1919

20-
test('mdast-util-to-nlcst', function (t) {
20+
test('mdast-util-to-nlcst', (t) => {
2121
t.throws(
22-
function () {
22+
() => {
2323
// @ts-ignore runtime.
2424
toNlcst()
2525
},
@@ -28,7 +28,7 @@ test('mdast-util-to-nlcst', function (t) {
2828
)
2929

3030
t.throws(
31-
function () {
31+
() => {
3232
// @ts-ignore runtime.
3333
toNlcst({})
3434
},
@@ -37,7 +37,7 @@ test('mdast-util-to-nlcst', function (t) {
3737
)
3838

3939
t.throws(
40-
function () {
40+
() => {
4141
// @ts-ignore runtime.
4242
toNlcst({type: 'foo'})
4343
},
@@ -46,7 +46,7 @@ test('mdast-util-to-nlcst', function (t) {
4646
)
4747

4848
t.throws(
49-
function () {
49+
() => {
5050
// @ts-ignore runtime.
5151
toNlcst({type: 'foo'})
5252
},
@@ -55,7 +55,7 @@ test('mdast-util-to-nlcst', function (t) {
5555
)
5656

5757
t.throws(
58-
function () {
58+
() => {
5959
// @ts-ignore runtime.
6060
toNlcst({type: 'text', value: 'foo'}, {foo: 'bar'})
6161
},
@@ -64,7 +64,7 @@ test('mdast-util-to-nlcst', function (t) {
6464
)
6565

6666
t.throws(
67-
function () {
67+
() => {
6868
// @ts-ignore runtime.
6969
toNlcst({type: 'text', value: 'foo'}, vfile({contents: 'foo'}))
7070
},
@@ -73,7 +73,7 @@ test('mdast-util-to-nlcst', function (t) {
7373
)
7474

7575
t.throws(
76-
function () {
76+
() => {
7777
toNlcst(
7878
/** @type {Literal} */ ({type: 'text', value: 'foo'}),
7979
vfile(),
@@ -84,7 +84,7 @@ test('mdast-util-to-nlcst', function (t) {
8484
'should fail when not given positional information'
8585
)
8686

87-
t.doesNotThrow(function () {
87+
t.doesNotThrow(() => {
8888
toNlcst(
8989
/** @type {Literal} */ ({
9090
type: 'text',
@@ -96,7 +96,7 @@ test('mdast-util-to-nlcst', function (t) {
9696
)
9797
}, 'should accept a parser constructor')
9898

99-
t.doesNotThrow(function () {
99+
t.doesNotThrow(() => {
100100
toNlcst(
101101
/** @type {Literal} */ ({
102102
type: 'text',
@@ -109,7 +109,7 @@ test('mdast-util-to-nlcst', function (t) {
109109
}, 'should accept a parser instance')
110110

111111
t.throws(
112-
function () {
112+
() => {
113113
toNlcst(
114114
{
115115
type: 'text',
@@ -128,20 +128,20 @@ test('mdast-util-to-nlcst', function (t) {
128128
t.end()
129129
})
130130

131-
test('Fixtures', function (t) {
132-
var base = path.join('test', 'fixtures')
133-
var files = fs.readdirSync(base)
134-
var index = -1
131+
test('Fixtures', (t) => {
132+
const base = path.join('test', 'fixtures')
133+
const files = fs.readdirSync(base)
134+
let index = -1
135135
/** @type {string} */
136-
var name
136+
let name
137137
/** @type {VFile} */
138-
var input
138+
let input
139139
/** @type {Node} */
140-
var expected
140+
let expected
141141
/** @type {Node} */
142-
var mdast
142+
let mdast
143143
/** @type {Object.<string, unknown>} */
144-
var options
144+
let options
145145

146146
while (++index < files.length) {
147147
name = files[index]

0 commit comments

Comments
 (0)