Skip to content

Commit

Permalink
Add types of data fields
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 5, 2023
1 parent 81cde21 commit c6bd56c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
coverage/
node_modules/
yarn.lock
!/index.d.ts
49 changes: 49 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type {Position} from 'unist'

export type {Options, Space} from './lib/index.js'

export {fromParse5} from './lib/index.js'

// Register data on hast.
declare module 'hast' {
interface ElementData {
position: {
/**
* Positional info of the start tag of an element.
*
* Field added by `hast-util-from-parse5` (a utility used inside
* `rehype-parse` responsible for parsing HTML), when passing
* `verbose: true`.
*/
opening?: Position | undefined

/**
* Positional info of the end tag of an element.
*
* Field added by `hast-util-from-parse5` (a utility used inside
* `rehype-parse` responsible for parsing HTML), when passing
* `verbose: true`.
*/
closing?: Position | undefined

/**
* Positional info of the properties of an element.
*
* Field added by `hast-util-from-parse5` (a utility used inside
* `rehype-parse` responsible for parsing HTML), when passing
* `verbose: true`.
*/
properties?: Record<string, Position | undefined> | undefined
}
}

interface RootData {
/**
* Whether the document was using quirksmode.
*
* Field added by `hast-util-from-parse5` (a utility used inside
* `rehype-parse` responsible for parsing HTML).
*/
quirksMode?: boolean | undefined
}
}
6 changes: 1 addition & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
/**
* @typedef {import('./lib/index.js').Options} Options
* @typedef {import('./lib/index.js').Space} Space
*/

// Note: extra types exposed from `index.d.ts`.
export {fromParse5} from './lib/index.js'
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/**
* @typedef {import('hast').Element} Element
* @typedef {import('hast').ElementData} ElementData
* @typedef {import('hast').Nodes} Nodes
* @typedef {import('hast').Root} Root
* @typedef {import('hast').RootContent} RootContent
Expand Down Expand Up @@ -321,7 +322,7 @@ function createLocation(state, node, location) {
assert(location.startTag, 'a start tag should exist')
const opening = position(location.startTag)
const closing = location.endTag ? position(location.endTag) : undefined
/** @type {Record<string, unknown>} */
/** @type {ElementData['position']} */
const data = {opening}
if (closing) data.closing = closing
data.properties = props
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@
},
"xo": {
"overrides": [
{
"files": "**/*.ts",
"rules": {
"@typescript-eslint/consistent-type-definitions": "off"
}
},
{
"files": "test/**/*.js",
"rules": {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
"target": "es2020"
},
"exclude": ["coverage/", "node_modules/"],
"include": ["**/*.js"]
"include": ["**/*.js", "index.d.ts"]
}

0 comments on commit c6bd56c

Please sign in to comment.