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 19, 2021
1 parent a069621 commit d8075a8
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 31 deletions.
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
76 changes: 50 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,58 @@
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
*
* @typedef {import('unist-util-is').Type} Type
* @typedef {import('unist-util-is').Props} Props
* @typedef {import('unist-util-is').TestFunctionAnything} TestFunctionAnything
*/

import {convert} from 'unist-util-is'

export function findBefore(parent, index, test) {
var is = convert(test)
export var findBefore =
/**
* @type {(
* (<T extends Node>(node: Parent, index: Node|number, test: T['type']|Partial<T>|import('unist-util-is').TestFunctionPredicate<T>|Array.<T['type']|Partial<T>|import('unist-util-is').TestFunctionPredicate<T>>) => T|null) &
* ((node: Parent, index: Node|number, test?: null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>) => Node|null)
* )}
*/
(
/**
* @param {Parent} parent Parent node
* @param {Node|number} index Child of `parent`, or it’s index
* @param {null|undefined|Type|Props|TestFunctionAnything|Array<Type|Props|TestFunctionAnything>} [test] is-compatible test (such as a type)
* @returns {Node|null}
*/
function (parent, index, test) {
var is = convert(test)

if (!parent || !parent.type || !parent.children) {
throw new Error('Expected parent node')
}
if (!parent || !parent.type || !parent.children) {
throw new Error('Expected parent node')
}

if (typeof index === 'number') {
if (index < 0 || index === Number.POSITIVE_INFINITY) {
throw new Error('Expected positive finite number as index')
}
} else {
index = parent.children.indexOf(index)
if (typeof index === 'number') {
if (index < 0 || index === Number.POSITIVE_INFINITY) {
throw new Error('Expected positive finite number as index')
}
} else {
index = parent.children.indexOf(index)

if (index < 0) {
throw new Error('Expected child node or index')
}
}
if (index < 0) {
throw new Error('Expected child node or index')
}
}

// Performance.
if (index > parent.children.length) {
index = parent.children.length
}
// Performance.
if (index > parent.children.length) {
index = parent.children.length
}

while (index--) {
if (is(parent.children[index], index, parent)) {
return parent.children[index]
}
}
while (index--) {
if (is(parent.children[index], index, parent)) {
return parent.children[index]
}
}

return null
}
return null
}
)
17 changes: 16 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,35 @@
"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-is": "^5.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"remark": "^13.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",
"xo": "^0.38.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",
"test": "npm run format && npm run test-coverage"
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -57,6 +66,7 @@
"xo": {
"prettier": true,
"rules": {
"import/no-mutable-exports": "off",
"no-var": "off",
"prefer-arrow-callback": "off"
}
Expand All @@ -65,5 +75,10 @@
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true
}
}
19 changes: 15 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @typedef {import('unist').Node} Node
*/

import test from 'tape'
import remark from 'remark'
import {findBefore} from './index.js'
Expand All @@ -9,6 +13,7 @@ var children = paragraph.children
test('unist-util-find-before', function (t) {
t.throws(
function () {
// @ts-ignore runtime
findBefore()
},
/Expected parent node/,
Expand All @@ -17,16 +22,16 @@ test('unist-util-find-before', function (t) {

t.throws(
function () {
findBefore({
type: 'foo'
})
// @ts-ignore runtime
findBefore({type: 'foo'})
},
/Expected parent node/,
'should fail without parent node'
)

t.throws(
function () {
// @ts-ignore runtime
findBefore({type: 'foo', children: []})
},
/Expected child node or index/,
Expand All @@ -51,6 +56,7 @@ test('unist-util-find-before', function (t) {

t.throws(
function () {
// @ts-ignore runtime
findBefore({type: 'foo', children: [{type: 'bar'}]}, 1, false)
},
/Expected function, string, or object as test/,
Expand All @@ -59,6 +65,7 @@ test('unist-util-find-before', function (t) {

t.throws(
function () {
// @ts-ignore runtime
findBefore({type: 'foo', children: [{type: 'bar'}]}, 1, true)
},
/Expected function, string, or object as test/,
Expand Down Expand Up @@ -154,7 +161,11 @@ test('unist-util-find-before', function (t) {
'should return a child when given a `test` and existing (#4)'
)

function test(node, n) {
/**
* @param {Node} _
* @param {number} n
*/
function test(_, n) {
return n === 3
}

Expand Down
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 d8075a8

Please sign in to comment.