Skip to content

Commit 5e813f0

Browse files
committed
Update types and tests for changes in parse5
1 parent f45d2ac commit 5e813f0

File tree

4 files changed

+30
-31
lines changed

4 files changed

+30
-31
lines changed

lib/index.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
* @typedef {Parent['children'][number]} Child
1313
* @typedef {Element['children'][number]} ElementChild
1414
* @typedef {Child|Root} Node
15-
* @typedef {import('parse5').Document} P5Document
16-
* @typedef {import('parse5').DocumentType} P5Doctype
17-
* @typedef {import('parse5').CommentNode} P5Comment
18-
* @typedef {import('parse5').TextNode} P5Text
19-
* @typedef {import('parse5').Element} P5Element
20-
* @typedef {import('parse5').ElementLocation} P5ElementLocation
21-
* @typedef {import('parse5').Location} P5Location
22-
* @typedef {import('parse5').Attribute} P5Attribute
23-
* @typedef {import('parse5').Node} P5Node
15+
* @typedef {import('parse5').DefaultTreeAdapterMap} DefaultTreeAdapterMap
16+
* @typedef {DefaultTreeAdapterMap['document']} P5Document
17+
* @typedef {DefaultTreeAdapterMap['documentFragment']} P5Fragment
18+
* @typedef {DefaultTreeAdapterMap['commentNode']} P5Comment
19+
* @typedef {DefaultTreeAdapterMap['textNode']} P5Text
20+
* @typedef {DefaultTreeAdapterMap['element']} P5Element
21+
* @typedef {import('parse5/dist/common/token.js').ElementLocation} P5ElementLocation
22+
* @typedef {import('parse5/dist/common/token.js').Location} P5Location
23+
* @typedef {import('parse5/dist/common/token.js').Attribute} P5Attribute
24+
* @typedef {DefaultTreeAdapterMap['node']} P5Node
2425
*
2526
* @typedef {'html'|'svg'} Space
2627
*
@@ -116,7 +117,6 @@ function transform(ctx, ast) {
116117
const result = fn(ctx, ast, children)
117118

118119
if ('sourceCodeLocation' in ast && ast.sourceCodeLocation && ctx.file) {
119-
// @ts-expect-error It’s fine.
120120
const position = createLocation(ctx, result, ast.sourceCodeLocation)
121121

122122
if (position) {
@@ -297,6 +297,7 @@ function createLocation(ctx, node, location) {
297297

298298
node.data = {
299299
position: {
300+
// @ts-expect-error: assume not `undefined`.
300301
opening: position(location.startTag),
301302
closing: location.endTag ? position(location.endTag) : null,
302303
properties: props

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
],
3535
"dependencies": {
3636
"@types/hast": "^2.0.0",
37-
"@types/parse5": "^6.0.0",
3837
"@types/unist": "^2.0.0",
3938
"hastscript": "^7.0.0",
4039
"property-information": "^6.0.0",
@@ -46,7 +45,7 @@
4645
"@types/tape": "^4.0.0",
4746
"c8": "^7.0.0",
4847
"is-hidden": "^2.0.0",
49-
"parse5": "^6.0.0",
48+
"parse5": "^7.0.0",
5049
"prettier": "^2.0.0",
5150
"remark-cli": "^10.0.0",
5251
"remark-preset-wooorm": "^9.0.0",

readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ Say we have the following file, `example.html`:
3333
And our script, `example.js`, looks as follows:
3434

3535
```js
36-
import parse5 from 'parse5'
36+
import {parse} from 'parse5'
3737
import {readSync} from 'to-vfile'
3838
import {inspect} from 'unist-util-inspect'
3939
import {fromParse5} from 'hast-util-from-parse5'
4040

4141
const file = readSync('example.html')
42-
const p5ast = parse5.parse(String(file), {sourceCodeLocationInfo: true})
42+
const p5ast = parse(String(file), {sourceCodeLocationInfo: true})
4343
const hast = fromParse5(p5ast, file)
4444

4545
console.log(inspect(hast))

test/index.js

+16-17
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import path from 'node:path'
1313
import assert from 'node:assert'
1414
import test from 'tape'
1515
import {isHidden} from 'is-hidden'
16-
import parse5 from 'parse5'
16+
import {parse, parseFragment} from 'parse5'
1717
import {visit} from 'unist-util-visit'
1818
import {toVFile} from 'to-vfile'
1919
import {fromParse5} from '../index.js'
@@ -24,7 +24,7 @@ test('hast-util-from-parse5', (t) => {
2424
const file = toVFile({value: '<title>Hello!</title><h1>World!'})
2525

2626
t.deepEqual(
27-
fromParse5(parse5.parse(String(file))),
27+
fromParse5(parse(String(file))),
2828
{
2929
type: 'root',
3030
children: [
@@ -68,7 +68,7 @@ test('hast-util-from-parse5', (t) => {
6868
)
6969

7070
t.deepEqual(
71-
fromParse5(parse5.parseFragment(String(file))),
71+
fromParse5(parseFragment(String(file))),
7272
{
7373
type: 'root',
7474
children: [
@@ -91,10 +91,7 @@ test('hast-util-from-parse5', (t) => {
9191
)
9292

9393
t.deepEqual(
94-
fromParse5(
95-
parse5.parse(String(file), {sourceCodeLocationInfo: true}),
96-
file
97-
),
94+
fromParse5(parse(String(file), {sourceCodeLocationInfo: true}), file),
9895
{
9996
type: 'root',
10097
children: [
@@ -168,7 +165,7 @@ test('hast-util-from-parse5', (t) => {
168165
)
169166

170167
t.deepEqual(
171-
fromParse5(parse5.parse(String(file)), file),
168+
fromParse5(parse(String(file)), file),
172169
{
173170
type: 'root',
174171
children: [
@@ -227,16 +224,17 @@ test('hast-util-from-parse5', (t) => {
227224
nodeName: 'title',
228225
tagName: 'title',
229226
attrs: [],
227+
// @ts-expect-error: fine.
230228
namespaceURI: 'http://www.w3.org/1999/xhtml',
231229
childNodes: [
232230
{
233231
nodeName: '#text',
234232
value: 'Hello!',
235-
// @ts-expect-error runtime.
233+
// @ts-expect-error: fine.
236234
sourceCodeLocation: {}
237235
}
238236
],
239-
// @ts-expect-error runtime.
237+
// @ts-expect-error: fine.
240238
sourceCodeLocation: {
241239
startLine: 1,
242240
startCol: 1,
@@ -266,11 +264,12 @@ test('hast-util-from-parse5', (t) => {
266264
nodeName: 'p',
267265
tagName: 'p',
268266
attrs: [],
267+
// @ts-expect-error: fine.
269268
namespaceURI: 'http://www.w3.org/1999/xhtml',
270269
childNodes: [
271-
// @ts-expect-error runtime.
272270
{
273271
nodeName: '#text',
272+
// @ts-expect-error: fine.
274273
value: 'Hello!',
275274
sourceCodeLocation: {
276275
startLine: 1,
@@ -282,7 +281,7 @@ test('hast-util-from-parse5', (t) => {
282281
}
283282
}
284283
],
285-
// @ts-expect-error runtime.
284+
// @ts-expect-error: fine.
286285
sourceCodeLocation: {
287286
startLine: 1,
288287
startCol: 1,
@@ -315,7 +314,7 @@ test('hast-util-from-parse5', (t) => {
315314

316315
t.deepEqual(
317316
fromParse5(
318-
parse5.parseFragment(
317+
parseFragment(
319318
[
320319
'<svg width="230" height="120" viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">',
321320
'<circle cx="60" cy="60" r="50" fill="red"/>',
@@ -394,7 +393,7 @@ test('fixtures', (t) => {
394393
* @param {Options} options
395394
*/
396395
function checkYesYes(t, options) {
397-
const input = parse5.parse(String(options.file), {
396+
const input = parse(String(options.file), {
398397
sourceCodeLocationInfo: true
399398
})
400399
const actual = fromParse5(input, {file: options.file, verbose: true})
@@ -418,7 +417,7 @@ test('fixtures', (t) => {
418417
* @param {Options} options
419418
*/
420419
function checkNoYes(t, options) {
421-
const input = parse5.parse(String(options.file))
420+
const input = parse(String(options.file))
422421
const actual = fromParse5(input, {file: options.file, verbose: true})
423422
/** @type {Node} */
424423
const expected = JSON.parse(String(fs.readFileSync(options.out)))
@@ -434,7 +433,7 @@ test('fixtures', (t) => {
434433
* @param {Options} options
435434
*/
436435
function checkYesNo(t, options) {
437-
const input = parse5.parse(String(options.file), {
436+
const input = parse(String(options.file), {
438437
sourceCodeLocationInfo: true
439438
})
440439
const actual = fromParse5(input)
@@ -452,7 +451,7 @@ test('fixtures', (t) => {
452451
* @param {Options} options
453452
*/
454453
function checkNoNo(t, options) {
455-
const input = parse5.parse(String(options.file))
454+
const input = parse(String(options.file))
456455
const actual = fromParse5(input)
457456
/** @type {Node} */
458457
const expected = JSON.parse(String(fs.readFileSync(options.out)))

0 commit comments

Comments
 (0)