Skip to content

Commit 860a8e5

Browse files
committed
Refactor code-style
1 parent ea32494 commit 860a8e5

File tree

4 files changed

+57
-81
lines changed

4 files changed

+57
-81
lines changed

lib/index.js

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
* @typedef {import('unist').Node} UnistNode
33
* @typedef {import('hast').Parent} Parent
44
* @typedef {import('hast').Root} Root
5-
* @typedef {import('hast').DocType} Doctype
65
* @typedef {import('hast').Element} Element
7-
* @typedef {import('hast').Text} Text
8-
* @typedef {import('hast').Comment} Comment
96
* @typedef {Parent['children'][number]} Child
107
* @typedef {Element['children'][number]} ElementChild
118
* @typedef {Child|Root} Node
@@ -31,20 +28,20 @@ import {toString as nlcstToString} from 'nlcst-to-string'
3128
import {pointStart} from 'unist-util-position'
3229
import vfileLocation from 'vfile-location'
3330

34-
var push = [].push
31+
const push = [].push
3532

36-
var source = convertElement(['code', dataNlcstSourced])
37-
var ignore = convertElement([
33+
const source = convertElement(['code', dataNlcstSourced])
34+
const ignore = convertElement([
3835
'script',
3936
'style',
4037
'svg',
4138
'math',
4239
'del',
4340
dataNlcstIgnore
4441
])
45-
var explicit = convertElement(['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'])
42+
const explicit = convertElement(['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'])
4643

47-
var flowAccepting = convertElement([
44+
const flowAccepting = convertElement([
4845
'body',
4946
'article',
5047
'section',
@@ -71,7 +68,7 @@ var flowAccepting = convertElement([
7168
])
7269

7370
// See: <https://html.spec.whatwg.org/multipage/dom.html#paragraphs>
74-
var unravelInParagraph = convertElement(['a', 'ins', 'del', 'map'])
71+
const unravelInParagraph = convertElement(['a', 'ins', 'del', 'map'])
7572

7673
/**
7774
* Transform `tree` to nlcst.
@@ -81,15 +78,6 @@ var unravelInParagraph = convertElement(['a', 'ins', 'del', 'map'])
8178
* @param {ParserInstance|ParserConstructor} Parser
8279
*/
8380
export function toNlcst(tree, file, Parser) {
84-
/** @type {ParserInstance} */
85-
var parser
86-
/** @type {Location} */
87-
var location
88-
/** @type {Array.<UnistNode>} */
89-
var results
90-
/** @type {string} */
91-
var doc
92-
9381
// Warn for invalid parameters.
9482
if (!tree || !tree.type) {
9583
throw new Error('hast-util-to-nlcst expected node')
@@ -108,13 +96,11 @@ export function toNlcst(tree, file, Parser) {
10896
throw new Error('hast-util-to-nlcst expected position on nodes')
10997
}
11098

111-
doc = String(file)
112-
location = vfileLocation(doc)
113-
parser = 'parse' in Parser ? Parser : new Parser()
114-
115-
// Transform hast to nlcst, and pass these into `parser.parse` to insert
116-
// sentences, paragraphs where needed.
117-
results = []
99+
const doc = String(file)
100+
const location = vfileLocation(doc)
101+
const parser = 'parse' in Parser ? Parser : new Parser()
102+
/** @type {Array.<UnistNode>} */
103+
const results = []
118104

119105
find(tree)
120106

@@ -148,7 +134,7 @@ export function toNlcst(tree, file, Parser) {
148134
* @param {Array.<Child>} children
149135
*/
150136
function findAll(children) {
151-
var index = -1
137+
let index = -1
152138

153139
while (++index < children.length) {
154140
find(children[index])
@@ -161,8 +147,8 @@ export function toNlcst(tree, file, Parser) {
161147
*/
162148
function flattenAll(children) {
163149
/** @type {Array.<ElementChild>} */
164-
var results = []
165-
var index = -1
150+
const results = []
151+
let index = -1
166152

167153
while (++index < children.length) {
168154
if (unravelInParagraph(children[index])) {
@@ -182,7 +168,7 @@ export function toNlcst(tree, file, Parser) {
182168
function add(node) {
183169
/** @type {Array.<UnistNode>} */
184170
// @ts-ignore Assume child.
185-
var result = Array.isArray(node) ? all(node) : one(node)
171+
const result = Array.isArray(node) ? all(node) : one(node)
186172

187173
if (result.length > 0) {
188174
results.push(parser.tokenizeParagraph(result))
@@ -193,15 +179,13 @@ export function toNlcst(tree, file, Parser) {
193179
* @param {Array.<ElementChild>} children
194180
*/
195181
function implicit(children) {
196-
var index = -1
197-
var start = -1
182+
let index = -1
183+
let start = -1
198184
/** @type {boolean} */
199-
var viable
200-
/** @type {ElementChild} */
201-
var child
185+
let viable
202186

203187
while (++index <= children.length) {
204-
child = children[index]
188+
const child = children[index]
205189

206190
if (child && phrasing(child)) {
207191
if (start === -1) start = index
@@ -233,9 +217,9 @@ export function toNlcst(tree, file, Parser) {
233217
*/
234218
function one(node) {
235219
/** @type {Array.<UnistNode>} */
236-
var replacement
220+
let replacement
237221
/** @type {boolean} */
238-
var change
222+
let change
239223

240224
if (node.type === 'text') {
241225
replacement = parser.tokenize(node.value)
@@ -268,8 +252,8 @@ export function toNlcst(tree, file, Parser) {
268252
*/
269253
function all(children) {
270254
/** @type {Array.<UnistNode>} */
271-
var results = []
272-
var index = -1
255+
const results = []
256+
let index = -1
273257

274258
while (++index < children.length) {
275259
push.apply(results, one(children[index]) || [])
@@ -292,22 +276,18 @@ export function toNlcst(tree, file, Parser) {
292276
* @returns {T}
293277
*/
294278
function patch(nodes, location, offset) {
295-
var index = -1
296-
var start = offset
297-
/** @type {number} */
298-
var end
299-
/** @type {UnistNode} */
300-
var node
279+
let index = -1
280+
let start = offset
301281

302282
while (++index < nodes.length) {
303-
node = nodes[index]
283+
const node = nodes[index]
304284

305285
if ('children' in node) {
306286
// @ts-ignore Looks like a parent.
307287
patch(node.children, location, start)
308288
}
309289

310-
end = start + nlcstToString(node).length
290+
const end = start + nlcstToString(node).length
311291

312292
node.position = {
313293
start: location.toPoint(start),

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@
8282
"trailingComma": "none"
8383
},
8484
"xo": {
85-
"prettier": true,
86-
"rules": {
87-
"no-var": "off",
88-
"prefer-arrow-callback": "off"
89-
}
85+
"prettier": true
9086
},
9187
"remarkConfig": {
9288
"plugins": [

readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ Say we have the following `example.html`:
3838
…and next to it, `index.js`:
3939

4040
```js
41-
import rehype from 'rehype'
42-
import vfile from 'to-vfile'
43-
import {ParseEnglish} from 'parse-english'
41+
import {readSync} from 'to-vfile'
4442
import {inspect} from 'unist-util-inspect'
4543
import {toNlcst} from 'hast-util-to-nlcst'
44+
import {ParseEnglish} from 'parse-english'
45+
import rehype from 'rehype'
4646

47-
var file = vfile.readSync('example.html')
48-
var tree = rehype().parse(file)
47+
const file = readSync('example.html')
48+
const tree = rehype().parse(file)
4949

5050
console.log(inspect(toNlcst(tree, file, ParseEnglish)))
5151
```

test/index.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import {ParseEnglish} from 'parse-english'
99
import {isHidden} from 'is-hidden'
1010
import {toNlcst} from '../index.js'
1111

12-
test('hast-util-to-nlcst', function (t) {
12+
test('hast-util-to-nlcst', (t) => {
1313
t.throws(
14-
function () {
14+
() => {
1515
// @ts-ignore runtime.
1616
toNlcst()
1717
},
@@ -20,7 +20,7 @@ test('hast-util-to-nlcst', function (t) {
2020
)
2121

2222
t.throws(
23-
function () {
23+
() => {
2424
// @ts-ignore runtime.
2525
toNlcst({})
2626
},
@@ -29,7 +29,7 @@ test('hast-util-to-nlcst', function (t) {
2929
)
3030

3131
t.throws(
32-
function () {
32+
() => {
3333
// @ts-ignore runtime.
3434
toNlcst({type: 'foo'})
3535
},
@@ -38,7 +38,7 @@ test('hast-util-to-nlcst', function (t) {
3838
)
3939

4040
t.throws(
41-
function () {
41+
() => {
4242
// @ts-ignore runtime.
4343
toNlcst({type: 'foo'})
4444
},
@@ -47,7 +47,7 @@ test('hast-util-to-nlcst', function (t) {
4747
)
4848

4949
t.throws(
50-
function () {
50+
() => {
5151
// @ts-ignore runtime.
5252
toNlcst({type: 'text', value: 'foo'}, {foo: 'bar'})
5353
},
@@ -56,7 +56,7 @@ test('hast-util-to-nlcst', function (t) {
5656
)
5757

5858
t.throws(
59-
function () {
59+
() => {
6060
// @ts-ignore runtime.
6161
toNlcst({type: 'text', value: 'foo'}, vfile('foo'))
6262
},
@@ -65,14 +65,14 @@ test('hast-util-to-nlcst', function (t) {
6565
)
6666

6767
t.throws(
68-
function () {
68+
() => {
6969
toNlcst({type: 'text', value: 'foo'}, vfile(), ParseLatin)
7070
},
7171
/hast-util-to-nlcst expected position on nodes/,
7272
'should fail when not given positional information'
7373
)
7474

75-
t.doesNotThrow(function () {
75+
t.doesNotThrow(() => {
7676
toNlcst(
7777
{
7878
type: 'text',
@@ -87,7 +87,7 @@ test('hast-util-to-nlcst', function (t) {
8787
)
8888
}, 'should accept a parser constructor')
8989

90-
t.doesNotThrow(function () {
90+
t.doesNotThrow(() => {
9191
toNlcst(
9292
{
9393
type: 'text',
@@ -103,7 +103,7 @@ test('hast-util-to-nlcst', function (t) {
103103
}, 'should accept a parser instance')
104104

105105
t.throws(
106-
function () {
106+
() => {
107107
toNlcst(
108108
{
109109
type: 'text',
@@ -119,8 +119,8 @@ test('hast-util-to-nlcst', function (t) {
119119
'should fail when not given positional information (#2)'
120120
)
121121

122-
t.test('should accept nodes without offsets', function (st) {
123-
var node = toNlcst(
122+
t.test('should accept nodes without offsets', (st) => {
123+
const node = toNlcst(
124124
{
125125
type: 'text',
126126
value: 'foo',
@@ -139,8 +139,8 @@ test('hast-util-to-nlcst', function (t) {
139139
st.end()
140140
})
141141

142-
t.test('should accept comments', function (st) {
143-
var node = toNlcst(
142+
t.test('should accept comments', (st) => {
143+
const node = toNlcst(
144144
{
145145
type: 'comment',
146146
value: 'a',
@@ -169,20 +169,20 @@ test('hast-util-to-nlcst', function (t) {
169169
t.end()
170170
})
171171

172-
test('Fixtures', function (t) {
173-
var root = path.join('test', 'fixtures')
174-
var files = fs.readdirSync(root)
175-
var index = -1
172+
test('Fixtures', (t) => {
173+
const root = path.join('test', 'fixtures')
174+
const files = fs.readdirSync(root)
175+
let index = -1
176176
/** @type {string} */
177-
var input
177+
let input
178178
/** @type {string} */
179-
var output
179+
let output
180180
/** @type {import('vfile').VFile} */
181-
var file
181+
let file
182182
/** @type {import('unist').Node} */
183-
var actual
183+
let actual
184184
/** @type {import('unist').Node} */
185-
var expected
185+
let expected
186186

187187
while (++index < files.length) {
188188
if (isHidden(files[index])) continue

0 commit comments

Comments
 (0)