Skip to content

Commit

Permalink
Add JSDoc based types
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Apr 23, 2021
1 parent 2d993f8 commit 583deff
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
*.d.ts
*.log
coverage/
node_modules/
Expand Down
16 changes: 15 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist-util-visit').Visitor<Node>} Visitor
*/

import {visit} from 'unist-util-visit'

// Make an mdast tree compact by merging adjacent text nodes.
/**
* Make an mdast tree compact by merging adjacent text nodes.
*
* @template {Node} T
* @param {T} tree
* @returns {T}
*/
export function compact(tree) {
visit(tree, visitor)

return tree

/** @type {Visitor} */
function visitor(child, index, parent) {
var siblings = parent ? parent.children : []
var previous = index && siblings[index - 1]
Expand All @@ -16,10 +28,12 @@ export function compact(tree) {
(child.type === 'text' || child.type === 'blockquote')
) {
if (child.value) {
// @ts-ignore must be text.
previous.value += child.value
}

if (child.children) {
// @ts-ignore must be block quote.
previous.children = previous.children.concat(child.children)
}

Expand Down
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,31 @@
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
],
"dependencies": {
"@types/unist": "^2.0.0",
"unist-util-visit": "^3.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"unist-builder": "^3.0.0",
"xo": "^0.39.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
Expand All @@ -66,5 +75,10 @@
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true
}
}
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"include": ["*.js"],
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020"],
"module": "ES2020",
"moduleResolution": "node",
"allowJs": true,
"checkJs": true,
"declaration": true,
"emitDeclarationOnly": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
}
}

0 comments on commit 583deff

Please sign in to comment.