Skip to content

Commit ca404c7

Browse files
committed
Refactor code-style
1 parent 7ce85ee commit ca404c7

File tree

9 files changed

+404
-407
lines changed

9 files changed

+404
-407
lines changed

lib/index.js

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
/**
2-
* @typedef {import('vfile').VFile} VFile
3-
* @typedef {import('property-information').Schema} Schema
4-
* @typedef {import('unist').Position} Position
5-
* @typedef {import('unist').Point} Point
62
* @typedef {import('hast').Element} Element
3+
* @typedef {import('hast').Nodes} Nodes
74
* @typedef {import('hast').Root} Root
85
* @typedef {import('hast').RootContent} RootContent
9-
* @typedef {import('hast').Nodes} Nodes
6+
*
107
* @typedef {import('parse5').DefaultTreeAdapterMap} DefaultTreeAdapterMap
118
* @typedef {import('parse5').Token.ElementLocation} P5ElementLocation
129
* @typedef {import('parse5').Token.Location} P5Location
10+
*
11+
* @typedef {import('property-information').Schema} Schema
12+
*
13+
* @typedef {import('unist').Point} Point
14+
* @typedef {import('unist').Position} Position
15+
*
16+
* @typedef {import('vfile').VFile} VFile
1317
*/
1418

1519
/**
@@ -21,42 +25,45 @@
2125
* @typedef {DefaultTreeAdapterMap['element']} P5Element
2226
* @typedef {DefaultTreeAdapterMap['node']} P5Node
2327
* @typedef {DefaultTreeAdapterMap['template']} P5Template
24-
*
25-
* @typedef {'html' | 'svg'} Space
26-
* Namespace.
27-
*
28+
*/
29+
30+
/**
2831
* @typedef Options
2932
* Configuration.
3033
* @property {Space | null | undefined} [space='html']
31-
* Which space the document is in.
34+
* Which space the document is in (default: `'html'`).
3235
*
3336
* When an `<svg>` element is found in the HTML space, this package already
3437
* automatically switches to and from the SVG space when entering and exiting
3538
* it.
3639
* @property {VFile | null | undefined} [file]
37-
* File used to add positional info to nodes.
40+
* File used to add positional info to nodes (optional).
3841
*
3942
* If given, the file should represent the original HTML source.
4043
* @property {boolean} [verbose=false]
4144
* Whether to add extra positional info about starting tags, closing tags,
42-
* and attributes to elements.
45+
* and attributes to elements (default: `false`).
4346
*
4447
* > 👉 **Note**: only used when `file` is given.
4548
*
49+
* @typedef {'html' | 'svg'} Space
50+
* Namespace.
51+
*
4652
* @typedef State
4753
* Info passed around about the current state.
48-
* @property {Schema} schema
49-
* Current schema.
5054
* @property {VFile | undefined} file
5155
* Corresponding file.
52-
* @property {boolean | undefined} verbose
53-
* Add extra positional info.
5456
* @property {boolean} location
5557
* Whether location info was found.
58+
* @property {Schema} schema
59+
* Current schema.
60+
* @property {boolean | undefined} verbose
61+
* Add extra positional info.
5662
*/
5763

64+
import {ok as assert} from 'devlop'
5865
import {h, s} from 'hastscript'
59-
import {html, svg, find} from 'property-information'
66+
import {find, html, svg} from 'property-information'
6067
import {location} from 'vfile-location'
6168
import {webNamespaces} from 'web-namespaces'
6269

@@ -71,7 +78,7 @@ const proto = Object.prototype
7178
* @param {P5Node} tree
7279
* `parse5` tree to transform.
7380
* @param {Options | VFile | null | undefined} [options]
74-
* Configuration.
81+
* Configuration (optional).
7582
* @returns {Nodes}
7683
* hast tree.
7784
*/
@@ -92,10 +99,10 @@ export function fromParse5(tree, options) {
9299

93100
return one(
94101
{
95-
schema: settings.space === 'svg' ? svg : html,
96102
file,
97-
verbose: settings.verbose,
98-
location: false
103+
location: false,
104+
schema: settings.space === 'svg' ? svg : html,
105+
verbose: settings.verbose
99106
},
100107
tree
101108
)
@@ -142,7 +149,9 @@ function one(state, node) {
142149
const loc = location(doc)
143150
const start = loc.toPoint(0)
144151
const end = loc.toPoint(doc.length)
145-
// @ts-expect-error: always defined as we give valid input.
152+
// Always defined as we give valid input.
153+
assert(start, 'expected `start`')
154+
assert(end, 'expected `end`')
146155
result.position = {start, end}
147156
}
148157

@@ -185,14 +194,15 @@ function one(state, node) {
185194
function all(state, nodes) {
186195
let index = -1
187196
/** @type {Array<RootContent>} */
188-
const result = []
197+
const results = []
189198

190199
while (++index < nodes.length) {
191-
// @ts-expect-error Assume no roots in `nodes`.
192-
result[index] = one(state, nodes[index])
200+
// Assume no roots in `nodes`.
201+
const result = /** @type {RootContent} */ (one(state, nodes[index]))
202+
results.push(result)
193203
}
194204

195-
return result
205+
return results
196206
}
197207

198208
/**
@@ -236,9 +246,8 @@ function element(state, node) {
236246
const startTag = pos && pos.startTag && position(pos.startTag)
237247
const endTag = pos && pos.endTag && position(pos.endTag)
238248

239-
/** @type {Root} */
240-
// @ts-expect-error Types are wrong.
241-
const content = one(state, reference.content)
249+
// Root in, root out.
250+
const content = /** @type {Root} */ (one(state, reference.content))
242251

243252
if (startTag && endTag && state.file) {
244253
content.position = {start: startTag.end, end: endTag.start}
@@ -261,7 +270,7 @@ function element(state, node) {
261270
* p5 node.
262271
* @param {Nodes} to
263272
* hast node.
264-
* @returns {void}
273+
* @returns {undefined}
265274
* Nothing.
266275
*/
267276
function patch(state, from, to) {
@@ -321,14 +330,15 @@ function createLocation(state, node, location) {
321330
}
322331
}
323332

324-
node.data = {
325-
position: {
326-
// @ts-expect-error: assume not `undefined`.
327-
opening: position(location.startTag),
328-
closing: location.endTag ? position(location.endTag) : null,
329-
properties: props
330-
}
331-
}
333+
assert(location.startTag, 'a start tag should exist')
334+
const opening = position(location.startTag)
335+
const closing = location.endTag ? position(location.endTag) : undefined
336+
/** @type {Record<string, unknown>} */
337+
const data = {opening}
338+
if (closing) data.closing = closing
339+
data.properties = props
340+
341+
node.data = {position: data}
332342
}
333343
}
334344

@@ -354,7 +364,9 @@ function position(loc) {
354364
column: loc.endCol,
355365
offset: loc.endOffset
356366
})
357-
// @ts-expect-error `undefined` is fine.
367+
368+
// @ts-expect-error: we do use `undefined` for points if one or the other
369+
// exists.
358370
return start || end ? {start, end} : undefined
359371
}
360372

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"dependencies": {
3636
"@types/hast": "^3.0.0",
3737
"@types/unist": "^3.0.0",
38+
"devlop": "^1.0.0",
3839
"hastscript": "^8.0.0",
3940
"property-information": "^6.0.0",
4041
"vfile": "^6.0.0",

test/fixtures/attributes/index.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@
221221
"offset": 90
222222
}
223223
},
224-
"closing": null,
225224
"properties": {
226225
"alt": {
227226
"start": {

test/fixtures/element-void-close/index.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
"offset": 4
3737
}
3838
},
39-
"closing": null,
4039
"properties": {}
4140
}
4241
},
@@ -110,7 +109,6 @@
110109
"offset": 19
111110
}
112111
},
113-
"closing": null,
114112
"properties": {}
115113
}
116114
},

test/fixtures/element-void/index.json

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
"offset": 60
4141
}
4242
},
43-
"closing": null,
4443
"properties": {
4544
"src": {
4645
"start": {
@@ -129,7 +128,6 @@
129128
"offset": 66
130129
}
131130
},
132-
"closing": null,
133131
"properties": {}
134132
}
135133
},
@@ -202,7 +200,6 @@
202200
"offset": 80
203201
}
204202
},
205-
"closing": null,
206203
"properties": {}
207204
}
208205
},
@@ -334,7 +331,6 @@
334331
"offset": 105
335332
}
336333
},
337-
"closing": null,
338334
"properties": {}
339335
}
340336
},
@@ -465,7 +461,6 @@
465461
"offset": 147
466462
}
467463
},
468-
"closing": null,
469464
"properties": {}
470465
}
471466
},
@@ -497,7 +492,6 @@
497492
"offset": 141
498493
}
499494
},
500-
"closing": null,
501495
"properties": {}
502496
}
503497
},
@@ -529,7 +523,6 @@
529523
"offset": 138
530524
}
531525
},
532-
"closing": null,
533526
"properties": {}
534527
}
535528
},
@@ -561,7 +554,6 @@
561554
"offset": 130
562555
}
563556
},
564-
"closing": null,
565557
"properties": {}
566558
}
567559
},
@@ -661,7 +653,6 @@
661653
"offset": 167
662654
}
663655
},
664-
"closing": null,
665656
"properties": {}
666657
}
667658
},
@@ -697,7 +688,6 @@
697688
"offset": 176
698689
}
699690
},
700-
"closing": null,
701691
"properties": {}
702692
}
703693
},
@@ -733,7 +723,6 @@
733723
"offset": 180
734724
}
735725
},
736-
"closing": null,
737726
"properties": {}
738727
}
739728
},
@@ -769,7 +758,6 @@
769758
"offset": 187
770759
}
771760
},
772-
"closing": null,
773761
"properties": {}
774762
}
775763
},
@@ -879,7 +867,6 @@
879867
"offset": 229
880868
}
881869
},
882-
"closing": null,
883870
"properties": {}
884871
}
885872
},
@@ -911,7 +898,6 @@
911898
"offset": 217
912899
}
913900
},
914-
"closing": null,
915901
"properties": {}
916902
}
917903
},
@@ -943,7 +929,6 @@
943929
"offset": 209
944930
}
945931
},
946-
"closing": null,
947932
"properties": {}
948933
}
949934
},
@@ -1043,7 +1028,6 @@
10431028
"offset": 253
10441029
}
10451030
},
1046-
"closing": null,
10471031
"properties": {}
10481032
}
10491033
},
@@ -1079,7 +1063,6 @@
10791063
"offset": 262
10801064
}
10811065
},
1082-
"closing": null,
10831066
"properties": {}
10841067
}
10851068
},
@@ -1115,7 +1098,6 @@
11151098
"offset": 275
11161099
}
11171100
},
1118-
"closing": null,
11191101
"properties": {}
11201102
}
11211103
},

test/fixtures/simple/index.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@
167167
"offset": 51
168168
}
169169
},
170-
"closing": null,
171170
"properties": {
172171
"id": {
173172
"start": {

test/fixtures/svg/index.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@
8989
"offset": 187
9090
}
9191
},
92-
"closing": null,
9392
"properties": {
9493
"cx": {
9594
"start": {
@@ -195,7 +194,6 @@
195194
"offset": 238
196195
}
197196
},
198-
"closing": null,
199197
"properties": {
200198
"cx": {
201199
"start": {

0 commit comments

Comments
 (0)