Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typings #7

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,37 @@
"Titus Wormer <[email protected]> (https://wooorm.com)"
],
"files": [
"index.js"
"index.js",
"types/index.d.ts"
],
"types": "types/index.d.ts",
"dependencies": {
"unist-util-is": "^3.0.0"
"@types/unist": "^2.0.3",
"unist-util-is": "^4.0.0"
},
"devDependencies": {
"browserify": "^16.0.0",
"dtslint": "^0.9.0",
"nyc": "^14.0.0",
"prettier": "^1.0.0",
"remark": "^10.0.0",
"remark-cli": "^6.0.0",
"remark-preset-wooorm": "^5.0.0",
"tape": "^4.0.0",
"tinyify": "^2.0.0",
"typescript": "^3.5.3",
"unified": "^8.3.2",
"xo": "^0.24.0"
},
"scripts": {
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
"format": "remark . -qfo && prettier --write \"**/*.{js,ts}\" && xo --fix",
"build-bundle": "browserify index.js -s unistUtilVisitParents > unist-util-visit-parents.js",
"build-mangle": "browserify index.js -s unistUtilVisitParents -p tinyify > unist-util-visit-parents.min.js",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run format && npm run build && npm run test-coverage"
"test-types": "dtslint types",
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
},
"nyc": {
"check-coverage": true,
Expand Down
111 changes: 111 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// TypeScript Version: 3.5

import {Node, Parent} from 'unist'
import {Test} from 'unist-util-is'

declare namespace visitParents {
/**
* Continue traversing as normal
*/
type Continue = true

/**
* Do not traverse this node’s children
*/
type Skip = 'skip'

/**
* Stop traversing immediately
*/
type Exit = false

/**
* Union of the action types
*/
type Action = Continue | Skip | Exit

/**
* List with one or two values, the first an action, the second an index.
*/
type ActionTuple = [Action, Index]

/**
* Move to the sibling at index next (after node itself is completely traversed).
* Useful if mutating the tree, such as removing the node the visitor is currently on,
* or any of its previous siblings (or next siblings, in case of reverse)
* Results less than 0 or greater than or equal to children.length stop traversing the parent
*/
type Index = number

/**
* Invoked when a node (matching test, if given) is found.
* Visitors are free to transform node.
* They can also transform the parent of node (the last of ancestors).
* Replacing node itself, if visit.SKIP is not returned, still causes its descendants to be visited.
* If adding or removing previous siblings (or next siblings, in case of reverse) of node,
* visitor should return a new index (number) to specify the sibling to traverse after node is traversed.
* Adding or removing next siblings of node (or previous siblings, in case of reverse)
* is handled as expected without needing to return a new index.
* Removing the children property of an ancestor still results in them being traversed.
*
* @param node Found node
* @param ancestors Ancestors of node
* @paramType V node type found
* @returns
* When Action is passed, treated as a tuple of [Action]
* When Index is passed, treated as a tuple of [CONTINUE, Index]
* When ActionTuple is passed,
* Note that passing a tuple only makes sense if the action is SKIP.
* If the action is EXIT, that action can be returned.
* If the action is CONTINUE, index can be returned.
*/
type Visitor<V extends Node> = (
node: V,
ancestors: Node[]
) => void | Action | Index | ActionTuple
}

declare const visitParents: {
/**
* Visit children of tree which pass a test
*
* @param tree abstract syntax tree to visit
* @param test test node
* @param visitor function to run for each node
* @param reverse visit the tree in reverse, defaults to false
* @typeParam T tree node
* @typeParam V node type found
*/
<V extends Node>(
tree: Node,
test: Test<V> | Array<Test<any>>,
visitor: visitParents.Visitor<V>,
reverse?: boolean
): void

/**
* Visit children of a tree
*
* @param tree abstract syntax tree to visit
* @param visitor function to run for each node
* @param reverse visit the tree in reverse, defaults to false
*/
(tree: Node, visitor: visitParents.Visitor<Node>, reverse?: boolean): void

/**
* Continue traversing as normal
*/
CONTINUE: visitParents.Continue

/**
* Do not traverse this node’s children
*/
SKIP: visitParents.Skip

/**
* Stop traversing immediately
*/
EXIT: visitParents.Exit
}

export = visitParents
14 changes: 14 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"lib": ["es2015"],
"strict": true,
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": ".",
"paths": {
"unist-util-visit-parents": ["index.d.ts"]
}
}
}
15 changes: 15 additions & 0 deletions types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"callable-types": false,
"max-line-length": false,
"no-redundant-jsdoc": false,
"no-void-expression": false,
"only-arrow-functions": false,
"semicolon": false,
"unified-signatures": false,
"whitespace": false,
"interface-over-type-literal": false,
"no-unnecessary-generics": false
}
}
Loading