Skip to content

Commit 84b8134

Browse files
committed
Refactor code-style
1 parent 127ca0d commit 84b8134

File tree

4 files changed

+56
-60
lines changed

4 files changed

+56
-60
lines changed

lib/index.js

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,26 @@
99
* @typedef {Root|Child} Node
1010
* @typedef {import('unist').Point} Point
1111
* @typedef {import('sax').Tag} Tag
12-
*
1312
*/
1413

1514
import sax from 'sax'
1615
import {VFileMessage} from 'vfile-message'
1716

18-
var Parser = sax.SAXParser
17+
const Parser = sax.SAXParser
1918

20-
var fromCharCode = String.fromCharCode
19+
const fromCharCode = String.fromCharCode
2120

22-
var search = /\r?\n|\r/g
21+
const search = /\r?\n|\r/g
2322

2423
/**
2524
* @param {string|Uint8Array} doc
2625
*/
2726
export function fromXml(doc) {
2827
// @ts-ignore `strictEntities` is most definitely fine.
29-
var parser = new Parser(true, {position: true, strictEntities: true})
28+
const parser = new Parser(true, {position: true, strictEntities: true})
3029
/** @type {Array.<Node>} */
31-
var stack = [{type: 'root', children: []}]
32-
var position = now()
30+
const stack = [{type: 'root', children: []}]
31+
let position = now()
3332

3433
parser.ondoctype = ondoctype
3534
// @ts-ignore `onsgmldeclaration` is most definitely fine.
@@ -54,7 +53,7 @@ export function fromXml(doc) {
5453
* @returns {never}
5554
*/
5655
function onerror(error) {
57-
var index = error.message.indexOf('\nLine')
56+
const index = error.message.indexOf('\nLine')
5857
// The substring should always be included, but this guards against
5958
// changes in newer sax versions.
6059
/* c8 ignore next */
@@ -75,24 +74,23 @@ export function fromXml(doc) {
7574
// eslint-disable-next-line complexity
7675
function ondoctype(value) {
7776
/** @type {Doctype} */
78-
var node = {type: 'doctype', name: '', public: null, system: null}
79-
var index = -1
80-
var state = 'BEGIN'
77+
const node = {type: 'doctype', name: '', public: null, system: null}
78+
let index = -1
79+
let state = 'BEGIN'
8180
/** @type {string} */
82-
var returnState
81+
let returnState
8382
/** @type {string} */
84-
var buffer
85-
/** @type {number} */
86-
var bufferIndex
83+
let buffer
8784
/** @type {number} */
88-
var start
85+
let bufferIndex
8986
/** @type {number} */
90-
var marker
87+
let start
9188
/** @type {number} */
92-
var code
89+
let marker
9390

9491
while (++index <= value.length) {
95-
code = index === value.length ? null /* EOF */ : value.charCodeAt(index)
92+
const code =
93+
index === value.length ? null /* EOF */ : value.charCodeAt(index)
9694

9795
switch (state) {
9896
case 'BEGIN':
@@ -311,11 +309,11 @@ export function fromXml(doc) {
311309
*/
312310
function oncomment(value) {
313311
/** @type {Comment} */
314-
var node = {type: 'comment', value}
312+
const node = {type: 'comment', value}
315313

316314
// Comment has a positional bug… 😢
317315
// They end right before the last character (`>`), so let’s add that:
318-
var actualEnd = now()
316+
const actualEnd = now()
319317
actualEnd.column++
320318
actualEnd.offset++
321319

@@ -348,18 +346,16 @@ export function fromXml(doc) {
348346
*/
349347
function ontext(value) {
350348
/** @type {Text} */
351-
var node = {type: 'text', value}
349+
const node = {type: 'text', value}
352350
// Text has a positional bug… 😢
353351
// When they are added, the position is already at the next token.
354352
// So let’s reverse that.
355-
var actualEnd = Object.assign({}, position)
356-
var start = 0
357-
/** @type {RegExpMatchArray} */
358-
var match
353+
const actualEnd = Object.assign({}, position)
354+
let start = 0
359355

360356
while (start < value.length) {
361357
search.lastIndex = start
362-
match = search.exec(value)
358+
const match = search.exec(value)
363359

364360
if (match) {
365361
actualEnd.line++
@@ -441,6 +437,7 @@ export function fromXml(doc) {
441437
* @returns {boolean}
442438
*/
443439
function isNameStartChar(code) {
440+
// eslint-disable-next-line no-misleading-character-class
444441
return /[:A-Z_a-z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02FF\u0370-\u037D\u037F-\u1FFF\u200C\u200D\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]/.test(
445442
fromCharCode(code)
446443
)
@@ -455,6 +452,7 @@ function isNameStartChar(code) {
455452
function isNameChar(code) {
456453
return (
457454
isNameStartChar(code) ||
455+
// eslint-disable-next-line no-misleading-character-class
458456
/[-.\d\u00B7\u0300-\u036F\u203F\u2040]/.test(fromCharCode(code))
459457
)
460458
}

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@
7676
"xo": {
7777
"prettier": true,
7878
"rules": {
79-
"no-misleading-character-class": "off",
80-
"no-var": "off",
81-
"prefer-arrow-callback": "off"
79+
"#no-misleading-character-class": "off"
8280
}
8381
},
8482
"remarkConfig": {

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ Say we have the following XML file, `example.xml`:
3636
And our script, `example.js`, looks as follows:
3737

3838
```js
39-
import fs from 'fs'
39+
import fs from 'node:fs'
4040
import {fromXml} from 'xast-util-from-xml'
4141

42-
var doc = fs.readFileSync('example.xml')
42+
const doc = fs.readFileSync('example.xml')
4343

44-
var tree = fromXml(doc)
44+
const tree = fromXml(doc)
4545

4646
console.log(tree)
4747
```

test/index.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import test from 'tape'
1010
import {isHidden} from 'is-hidden'
1111
import {fromXml} from '../index.js'
1212

13-
var join = path.join
13+
const join = path.join
1414

15-
test('xast-util-from-xml', function (t) {
15+
test('xast-util-from-xml', (t) => {
1616
t.equal(typeof fromXml, 'function', 'should expose a function')
1717

1818
try {
@@ -71,135 +71,135 @@ test('xast-util-from-xml', function (t) {
7171
}
7272

7373
t.throws(
74-
function () {
74+
() => {
7575
fromXml('<!doctype>')
7676
},
7777
/1:11: Expected doctype name/,
7878
'should throw on missing doctype name'
7979
)
8080

8181
t.throws(
82-
function () {
82+
() => {
8383
fromXml('<!doctype !>')
8484
},
8585
/1:13: Expected start of doctype name/,
8686
'should throw on invalid doctype name'
8787
)
8888

8989
t.throws(
90-
function () {
90+
() => {
9191
fromXml('<!DOCTYPE name[<!ELEMENT greeting (#PCDATA)>]>')
9292
},
9393
/1:47: Unexpected internal subset/,
9494
'should throw on internal subset directly after doctype name'
9595
)
9696

9797
t.throws(
98-
function () {
98+
() => {
9999
fromXml('<!DOCTYPE name [<!ELEMENT greeting (#PCDATA)>]>')
100100
},
101101
/1:48: Unexpected internal subset/,
102102
'should throw on internal subset after doctype name'
103103
)
104104

105105
t.throws(
106-
function () {
106+
() => {
107107
fromXml('<!DOCTYPE name!>')
108108
},
109109
/1:17: Expected doctype name character, whitespace, or doctype end/,
110110
'should throw on invalid character directly after doctype'
111111
)
112112

113113
t.throws(
114-
function () {
114+
() => {
115115
fromXml('<!DOCTYPE name !>')
116116
},
117117
/1:18: Expected external identifier \(`PUBLIC` or `SYSTEM`\), whitespace, or doctype end/,
118118
'should throw on invalid character after doctype'
119119
)
120120

121121
t.throws(
122-
function () {
122+
() => {
123123
fromXml('<!DOCTYPE name PUB>')
124124
},
125125
/1:20: Expected external identifier \(`PUBLIC` or `SYSTEM`\)/,
126126
'should throw on invalid external identifier (1)'
127127
)
128128

129129
t.throws(
130-
function () {
130+
() => {
131131
fromXml('<!DOCTYPE name SYSTEm>')
132132
},
133133
/1:23: Expected external identifier \(`PUBLIC` or `SYSTEM`\)/,
134134
'should throw on invalid external identifier (2)'
135135
)
136136

137137
t.throws(
138-
function () {
138+
() => {
139139
fromXml('<!DOCTYPE name PUBLIC>')
140140
},
141141
/1:23: Expected whitespace after `PUBLIC`/,
142142
'should throw on missing whitespace after public identifier'
143143
)
144144

145145
t.throws(
146-
function () {
146+
() => {
147147
fromXml('<!DOCTYPE name PUBLIC !>')
148148
},
149149
/1:25: Expected quote or apostrophe to start public literal/,
150150
'should throw on invalid character after public identifier'
151151
)
152152

153153
t.throws(
154-
function () {
154+
() => {
155155
fromXml('<!DOCTYPE name PUBLIC "🤔">')
156156
},
157157
/1:28: Expected pubid character in public literal/,
158158
'should throw on invalid character in public identifier'
159159
)
160160

161161
t.throws(
162-
function () {
162+
() => {
163163
fromXml('<!DOCTYPE name PUBLIC "literal"!>')
164164
},
165165
/1:34: Expected whitespace after public literal/,
166166
'should throw on invalid character after public literal'
167167
)
168168

169169
t.throws(
170-
function () {
170+
() => {
171171
fromXml('<!DOCTYPE name SYSTEM>')
172172
},
173173
/1:23: Expected whitespace after `SYSTEM`/,
174174
'should throw on missing whitespace after system identifier'
175175
)
176176

177177
t.throws(
178-
function () {
178+
() => {
179179
fromXml('<!DOCTYPE name SYSTEM !>')
180180
},
181181
/1:25: Expected quote or apostrophe to start system literal/,
182182
'should throw on invalid character after system identifier'
183183
)
184184

185185
t.throws(
186-
function () {
186+
() => {
187187
fromXml('<!DOCTYPE name SYSTEM "asd>')
188188
},
189189
/1:28: Unexpected end/,
190190
'should throw on unended system literal'
191191
)
192192

193193
t.throws(
194-
function () {
194+
() => {
195195
fromXml('<!DOCTYPE name SYSTEM "asd" [<!ELEMENT greeting (#PCDATA)>]>')
196196
},
197197
/1:61: Unexpected internal subset/,
198198
'should throw on internal subset after external id'
199199
)
200200

201201
t.throws(
202-
function () {
202+
() => {
203203
fromXml('<!DOCTYPE name SYSTEM "asd" !>')
204204
},
205205
/1:31: Expected whitespace or end of doctype/,
@@ -209,10 +209,10 @@ test('xast-util-from-xml', function (t) {
209209
t.end()
210210
})
211211

212-
test('fixtures', function (t) {
213-
var base = join('test', 'fixtures')
214-
var files = fs.readdirSync(base)
215-
var index = -1
212+
test('fixtures', (t) => {
213+
const base = join('test', 'fixtures')
214+
const files = fs.readdirSync(base)
215+
let index = -1
216216

217217
while (++index < files.length) {
218218
if (!isHidden(files[index])) {
@@ -223,11 +223,11 @@ test('fixtures', function (t) {
223223
t.end()
224224

225225
function each(/** @type {string} */ fixture) {
226-
var input = fs.readFileSync(join(base, fixture, 'index.xml'))
227-
var fp = join(base, fixture, 'index.json')
228-
var actual = fromXml(input)
226+
const input = fs.readFileSync(join(base, fixture, 'index.xml'))
227+
const fp = join(base, fixture, 'index.json')
228+
const actual = fromXml(input)
229229
/** @type {Node} */
230-
var expected
230+
let expected
231231

232232
try {
233233
expected = JSON.parse(String(fs.readFileSync(fp)))

0 commit comments

Comments
 (0)