Skip to content

Commit 87bc514

Browse files
committed
Refactor code-style
1 parent 65a3ed9 commit 87bc514

File tree

3 files changed

+74
-81
lines changed

3 files changed

+74
-81
lines changed

Diff for: lib/index.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
* @typedef {import('@rgrove/parse-xml').XmlCdata} XmlCdata
33
* @typedef {import('@rgrove/parse-xml').XmlComment} XmlComment
44
* @typedef {import('@rgrove/parse-xml').XmlDeclaration} XmlDeclaration
5+
* @typedef {import('@rgrove/parse-xml').XmlError} XmlError
56
* @typedef {import('@rgrove/parse-xml').XmlDocument} XmlDocument
67
* @typedef {import('@rgrove/parse-xml').XmlDocumentType} XmlDocumentType
78
* @typedef {import('@rgrove/parse-xml').XmlElement} XmlElement
89
* @typedef {import('@rgrove/parse-xml').XmlProcessingInstruction} XmlProcessingInstruction
910
* @typedef {import('@rgrove/parse-xml').XmlText} XmlText
10-
* @typedef {import('@rgrove/parse-xml').XmlError} XmlError
11+
*
12+
* @typedef {import('vfile-location').Location} Location
1113
*
1214
* @typedef {import('xast').Cdata} Cdata
1315
* @typedef {import('xast').Comment} Comment
@@ -28,7 +30,7 @@
2830
*
2931
* @typedef State
3032
* Info passed around.
31-
* @property {ReturnType<location>} location
33+
* @property {Location} location
3234
* Interface to translate between offsets and points.
3335
*/
3436

@@ -39,7 +41,7 @@ import {VFileMessage} from 'vfile-message'
3941
/**
4042
* Parse a string of XML to a xast tree.
4143
*
42-
* @param {string | Uint8Array} value
44+
* @param {Uint8Array | string} value
4345
* Serialized XML.
4446
* @returns {Root}
4547
* xast root.
@@ -271,7 +273,7 @@ function transformChildren(children, state) {
271273
* xast node.
272274
* @param {State} state
273275
* Info passed around.
274-
* @returns {void}
276+
* @returns {undefined}
275277
* Nothing.
276278
*/
277279
function patch(from, to, state) {

Diff for: readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ import {fromXml} from 'xast-util-from-xml'
8383

8484
const tree = fromXml(await fs.readFile('example.xml'))
8585

86-
console.dir(tree, {depth: null})
86+
console.dir(tree, {depth: undefined})
8787
```
8888

8989
…now running `node example.js` yields (positional info removed for brevity):

Diff for: test/index.js

+67-76
Original file line numberDiff line numberDiff line change
@@ -8,72 +8,61 @@ import process from 'node:process'
88
import test from 'node:test'
99
import {isHidden} from 'is-hidden'
1010
import {fromXml} from '../index.js'
11-
import * as mod from '../index.js'
1211

13-
test('fromXml', () => {
14-
assert.deepEqual(
15-
Object.keys(mod).sort(),
16-
['fromXml'],
17-
'should expose the public api'
18-
)
12+
test('fromXml', async function (t) {
13+
await t.test('should expose the public api', async function () {
14+
assert.deepEqual(Object.keys(await import('../index.js')).sort(), [
15+
'fromXml'
16+
])
17+
})
1918

20-
try {
21-
fromXml('<root unquoted=attribute>')
22-
assert.fail('should fail (1)')
23-
} catch (error) {
24-
assert.match(
25-
String(error),
26-
/^1:16: Attribute value expected/,
27-
'should throw messages'
28-
)
29-
}
19+
await t.test('should throw messages', async function () {
20+
try {
21+
fromXml('<root unquoted=attribute>')
22+
assert.fail()
23+
} catch (error) {
24+
assert.match(String(error), /^1:16: Attribute value expected/)
25+
}
26+
})
3027

31-
try {
32-
fromXml('<!ENTITY>')
33-
assert.fail('should fail (2)')
34-
} catch (error) {
35-
assert.match(
36-
String(error),
37-
/^1:1: Root element is missing or invalid/,
38-
'should throw for SGML directives'
39-
)
40-
}
28+
await t.test('should throw for SGML directives', async function () {
29+
try {
30+
fromXml('<!ENTITY>')
31+
assert.fail()
32+
} catch (error) {
33+
assert.match(String(error), /^1:1: Root element is missing or invalid/)
34+
}
35+
})
4136

42-
try {
43-
fromXml('<root>&foo;</root>')
44-
assert.fail('should fail (3)')
45-
} catch (error) {
46-
assert.match(
47-
String(error),
48-
/^1:7: Named entity isn't defined/,
49-
'should throw for unknown entities (1)'
50-
)
51-
}
37+
await t.test('should throw for unknown entities (1)', async function () {
38+
try {
39+
fromXml('<root>&foo;</root>')
40+
assert.fail()
41+
} catch (error) {
42+
assert.match(String(error), /^1:7: Named entity isn't defined/)
43+
}
44+
})
5245

53-
try {
54-
fromXml('<root>&copy;</root>')
55-
assert.fail('should fail (4)')
56-
} catch (error) {
57-
assert.match(
58-
String(error),
59-
/^1:7: Named entity isn't defined/,
60-
'should throw for unknown entities (2)'
61-
)
62-
}
46+
await t.test('should throw for unknown entities (2)', async function () {
47+
try {
48+
fromXml('<root>&copy;</root>')
49+
assert.fail()
50+
} catch (error) {
51+
assert.match(String(error), /^1:7: Named entity isn't defined/)
52+
}
53+
})
6354

64-
try {
65-
fromXml('<root><a><b><c/></a></b></root>')
66-
assert.fail('should fail (5)')
67-
} catch (error) {
68-
assert.match(
69-
String(error),
70-
/^1:17: Missing end tag for element/,
71-
'should throw on invalid nesting'
72-
)
73-
}
55+
await t.test('should throw on invalid nesting', async function () {
56+
try {
57+
fromXml('<root><a><b><c/></a></b></root>')
58+
assert.fail()
59+
} catch (error) {
60+
assert.match(String(error), /^1:17: Missing end tag for element/)
61+
}
62+
})
7463
})
7564

76-
test('fixtures', async () => {
65+
test('fixtures', async function (t) {
7766
const base = new URL('fixtures/', import.meta.url)
7867
const files = await fs.readdir(base)
7968
let index = -1
@@ -83,27 +72,29 @@ test('fixtures', async () => {
8372

8473
if (isHidden(folder)) continue
8574

86-
const inputUrl = new URL(folder + '/index.xml', base)
87-
const treeUrl = new URL(folder + '/index.json', base)
88-
const input = await fs.readFile(inputUrl)
89-
/** @type {Root} */
90-
// Remove `undefined`s.
91-
const actual = JSON.parse(JSON.stringify(fromXml(input)))
92-
/** @type {Root} */
93-
let expected
75+
await t.test(folder, async function () {
76+
const inputUrl = new URL(folder + '/index.xml', base)
77+
const treeUrl = new URL(folder + '/index.json', base)
78+
const input = await fs.readFile(inputUrl)
79+
/** @type {Root} */
80+
// Remove `undefined`s.
81+
const actual = JSON.parse(JSON.stringify(fromXml(input)))
82+
/** @type {Root} */
83+
let expected
9484

95-
try {
96-
expected = JSON.parse(String(await fs.readFile(treeUrl)))
85+
try {
86+
expected = JSON.parse(String(await fs.readFile(treeUrl)))
9787

98-
if ('UPDATE' in process.env) {
99-
throw new Error('Update')
88+
if ('UPDATE' in process.env) {
89+
throw new Error('Update')
90+
}
91+
} catch {
92+
// New folder.
93+
await fs.writeFile(treeUrl, JSON.stringify(actual, undefined, 2) + '\n')
94+
return
10095
}
101-
} catch {
102-
// New folder.
103-
await fs.writeFile(treeUrl, JSON.stringify(actual, null, 2) + '\n')
104-
continue
105-
}
10696

107-
assert.deepEqual(actual, expected, folder)
97+
assert.deepEqual(actual, expected)
98+
})
10899
}
109100
})

0 commit comments

Comments
 (0)