Skip to content

Commit f41b95b

Browse files
committed
Update @types/xast, utilities
1 parent 9152e8e commit f41b95b

File tree

8 files changed

+21
-34
lines changed

8 files changed

+21
-34
lines changed

lib/index.js

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
* @typedef {import('xast').Doctype} Doctype
1515
* @typedef {import('xast').Element} Element
1616
* @typedef {import('xast').Instruction} Instruction
17+
* @typedef {import('xast').Nodes} Nodes
1718
* @typedef {import('xast').Root} Root
19+
* @typedef {import('xast').RootContent} RootContent
1820
* @typedef {import('xast').Text} Text
1921
*/
2022

@@ -23,10 +25,6 @@
2325
* Nodes that occur in XML documents (`parse-xml`).
2426
* @typedef {XmlContent | XmlDocument} XmlNode
2527
* Nodes that occur (`parse-xml`).
26-
* @typedef {Cdata | Comment | Doctype | Element | Instruction | Text} Content
27-
* Nodes that occur in documents (`xast`).
28-
* @typedef {Content | Root} Node
29-
* Nodes that occur (`xast`).
3028
*
3129
* @typedef State
3230
* Info passed around.
@@ -47,7 +45,6 @@ import {VFileMessage} from 'vfile-message'
4745
* xast root.
4846
*/
4947
export function fromXml(value) {
50-
// @ts-expect-error: to do: update.
5148
const loc = location(value)
5249
/** @type {XmlDocument} */
5350
let xmlDocument
@@ -68,7 +65,6 @@ export function fromXml(value) {
6865
} catch (error_) {
6966
const error = /** @type {XmlError} */ (error_)
7067
const point = loc.toPoint(error.pos)
71-
// @ts-expect-error: to do: update.
7268
throw new VFileMessage(error.message, point, 'xast-util-from-xml:error')
7369
}
7470

@@ -82,7 +78,7 @@ export function fromXml(value) {
8278
* Transform CDATA.
8379
*
8480
* @param {XmlCdata} node
85-
* @returns {Content}
81+
* @returns {Cdata}
8682
*/
8783
function transformCdata(node) {
8884
return {type: 'cdata', value: node.text}
@@ -93,7 +89,7 @@ function transformCdata(node) {
9389
*
9490
* @param {XmlComment} node
9591
* XML node (`parse-xml`).
96-
* @returns {Content}
92+
* @returns {Comment}
9793
* xast node.
9894
*/
9995
function transformComment(node) {
@@ -120,17 +116,15 @@ function transformDocument(node, state) {
120116
*
121117
* @param {XmlDocumentType} node
122118
* XML node (`parse-xml`).
123-
* @returns {Content}
119+
* @returns {Doctype}
124120
* xast node.
125121
*/
126122
function transformDoctype(node) {
127123
return {
128124
type: 'doctype',
129125
name: node.name,
130-
// @ts-expect-error: `@types/xast` should allow `null`.
131-
public: node.publicId,
132-
// @ts-expect-error: `@types/xast` should allow `null`.
133-
system: node.systemId
126+
public: node.publicId || undefined,
127+
system: node.systemId || undefined
134128
}
135129
}
136130

@@ -141,7 +135,7 @@ function transformDoctype(node) {
141135
* XML node (`parse-xml`).
142136
* @param {State} state
143137
* Info passed around.
144-
* @returns {Content}
138+
* @returns {Element}
145139
* xast node.
146140
*/
147141
function transformElement(node, state) {
@@ -160,7 +154,7 @@ function transformElement(node, state) {
160154
*
161155
* @param {XmlProcessingInstruction} node
162156
* XML node (`parse-xml`).
163-
* @returns {Content}
157+
* @returns {Instruction}
164158
* xast node.
165159
*/
166160
function transformInstruction(node) {
@@ -172,7 +166,7 @@ function transformInstruction(node) {
172166
*
173167
* @param {XmlText} node
174168
* XML node (`parse-xml`).
175-
* @returns {Content}
169+
* @returns {Text}
176170
* xast node.
177171
*/
178172
function transformText(node) {
@@ -184,7 +178,7 @@ function transformText(node) {
184178
*
185179
* @param {XmlDeclaration} node
186180
* XML node (`parse-xml`).
187-
* @returns {Content}
181+
* @returns {Instruction}
188182
* xast node.
189183
*/
190184
function transformXmlDeclaration(node) {
@@ -222,17 +216,17 @@ function transformXmlDeclaration(node) {
222216
* Nodes to transform (`parse-xml`).
223217
* @param {State} state
224218
* Info passed around.
225-
* @returns {Array<Content>}
219+
* @returns {Array<RootContent>}
226220
* xast nodes.
227221
*/
228222
function transformChildren(children, state) {
229-
/** @type {Array<Content>} */
223+
/** @type {Array<RootContent>} */
230224
const results = []
231225
let index = -1
232226

233227
while (++index < children.length) {
234228
const from = children[index]
235-
/** @type {Content | undefined} */
229+
/** @type {RootContent | undefined} */
236230
let to
237231

238232
if (from.type === 'cdata') {
@@ -273,7 +267,7 @@ function transformChildren(children, state) {
273267
*
274268
* @param {XmlNode} from
275269
* XML node (`parse-xml`).
276-
* @param {Node} to
270+
* @param {Nodes} to
277271
* xast node.
278272
* @param {State} state
279273
* Info passed around.
@@ -292,7 +286,6 @@ function patch(from, to, state) {
292286
from.end === -1 ? undefined : state.location.toPoint(from.end)
293287

294288
if (start && end) {
295-
// @ts-expect-error: to do: update.
296289
to.position = {start, end}
297290
}
298291
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
],
4040
"dependencies": {
4141
"@rgrove/parse-xml": "^4.1.0",
42-
"@types/xast": "^1.0.0",
43-
"vfile-location": "^4.0.0",
44-
"vfile-message": "^3.0.0"
42+
"@types/xast": "^2.0.0",
43+
"vfile-location": "^5.0.0",
44+
"vfile-message": "^4.0.0"
4545
},
4646
"devDependencies": {
4747
"@types/node": "^20.0.0",

test/fixtures/doctype-name-only/index.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
{
55
"type": "doctype",
66
"name": "HTML",
7-
"public": null,
8-
"system": null,
97
"position": {
108
"start": {
119
"line": 1,

test/fixtures/doctype-name-trailing-whitespace/index.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
{
55
"type": "doctype",
66
"name": "HTML",
7-
"public": null,
8-
"system": null,
97
"position": {
108
"start": {
119
"line": 1,

test/fixtures/doctype-system-apostrophe/index.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
{
55
"type": "doctype",
66
"name": "HTML",
7-
"public": null,
87
"system": "http://www.w3.org/TR/REC-html40/loose.dtd",
98
"position": {
109
"start": {

test/fixtures/doctype-system/index.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
{
55
"type": "doctype",
66
"name": "HTML",
7-
"public": null,
87
"system": "http://www.w3.org/TR/REC-html40/loose.dtd",
98
"position": {
109
"start": {

test/fixtures/html-in-svg-in-html/index.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
{
55
"type": "doctype",
66
"name": "HTML",
7-
"public": null,
8-
"system": null,
97
"position": {
108
"start": {
119
"line": 1,

test/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ test('fixtures', async () => {
8686
const inputUrl = new URL(folder + '/index.xml', base)
8787
const treeUrl = new URL(folder + '/index.json', base)
8888
const input = await fs.readFile(inputUrl)
89-
const actual = fromXml(input)
89+
/** @type {Root} */
90+
// Remove `undefined`s.
91+
const actual = JSON.parse(JSON.stringify(fromXml(input)))
9092
/** @type {Root} */
9193
let expected
9294

0 commit comments

Comments
 (0)