Skip to content

Commit c6bd56c

Browse files
committed
Add types of data fields
1 parent 81cde21 commit c6bd56c

File tree

6 files changed

+60
-7
lines changed

6 files changed

+60
-7
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
coverage/
55
node_modules/
66
yarn.lock
7+
!/index.d.ts

index.d.ts

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import type {Position} from 'unist'
2+
3+
export type {Options, Space} from './lib/index.js'
4+
5+
export {fromParse5} from './lib/index.js'
6+
7+
// Register data on hast.
8+
declare module 'hast' {
9+
interface ElementData {
10+
position: {
11+
/**
12+
* Positional info of the start tag of an element.
13+
*
14+
* Field added by `hast-util-from-parse5` (a utility used inside
15+
* `rehype-parse` responsible for parsing HTML), when passing
16+
* `verbose: true`.
17+
*/
18+
opening?: Position | undefined
19+
20+
/**
21+
* Positional info of the end tag of an element.
22+
*
23+
* Field added by `hast-util-from-parse5` (a utility used inside
24+
* `rehype-parse` responsible for parsing HTML), when passing
25+
* `verbose: true`.
26+
*/
27+
closing?: Position | undefined
28+
29+
/**
30+
* Positional info of the properties of an element.
31+
*
32+
* Field added by `hast-util-from-parse5` (a utility used inside
33+
* `rehype-parse` responsible for parsing HTML), when passing
34+
* `verbose: true`.
35+
*/
36+
properties?: Record<string, Position | undefined> | undefined
37+
}
38+
}
39+
40+
interface RootData {
41+
/**
42+
* Whether the document was using quirksmode.
43+
*
44+
* Field added by `hast-util-from-parse5` (a utility used inside
45+
* `rehype-parse` responsible for parsing HTML).
46+
*/
47+
quirksMode?: boolean | undefined
48+
}
49+
}

index.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1-
/**
2-
* @typedef {import('./lib/index.js').Options} Options
3-
* @typedef {import('./lib/index.js').Space} Space
4-
*/
5-
1+
// Note: extra types exposed from `index.d.ts`.
62
export {fromParse5} from './lib/index.js'

lib/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @typedef {import('hast').Element} Element
3+
* @typedef {import('hast').ElementData} ElementData
34
* @typedef {import('hast').Nodes} Nodes
45
* @typedef {import('hast').Root} Root
56
* @typedef {import('hast').RootContent} RootContent
@@ -321,7 +322,7 @@ function createLocation(state, node, location) {
321322
assert(location.startTag, 'a start tag should exist')
322323
const opening = position(location.startTag)
323324
const closing = location.endTag ? position(location.endTag) : undefined
324-
/** @type {Record<string, unknown>} */
325+
/** @type {ElementData['position']} */
325326
const data = {opening}
326327
if (closing) data.closing = closing
327328
data.properties = props

package.json

+6
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@
8585
},
8686
"xo": {
8787
"overrides": [
88+
{
89+
"files": "**/*.ts",
90+
"rules": {
91+
"@typescript-eslint/consistent-type-definitions": "off"
92+
}
93+
},
8894
{
8995
"files": "test/**/*.js",
9096
"rules": {

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
"target": "es2020"
1212
},
1313
"exclude": ["coverage/", "node_modules/"],
14-
"include": ["**/*.js"]
14+
"include": ["**/*.js", "index.d.ts"]
1515
}

0 commit comments

Comments
 (0)