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 18, 2021
1 parent 3cc5f98 commit fcc0040
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 7 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
33 changes: 28 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
/**
* @typedef {import('unist').Parent} Parent
* @typedef {import('unist').Node} Node
*
* @typedef {Node} Proxy
* @property {parent|null} parent
*/

/** @type {WeakMap<Node, Proxy>} */
var cache = new WeakMap()

export function parents(ast) {
return wrapNode(ast, null)
/**
* @param {Node} tree
* @returns {Proxy}
*/
export function parents(tree) {
return wrapNode(tree, null)
}

/**
* @param {Node} node
* @param {Parent|null} parent
* @returns {Proxy}
*/
function wrapNode(node, parent) {
/** @type {Node} */
var proxy
/** @type {string} */
var key

if (cache.has(node)) {
return cache.get(node)
}

// @ts-ignore Assume `node` is a valid node.
proxy = {}

for (key in node) {
Expand Down Expand Up @@ -48,13 +69,15 @@ function wrapNode(node, parent) {
return parent
}

/**
* @param {Parent} newParent
*/
function setParent(newParent) {
parent = newParent
}

function getChildren() {
return node.children.map(function (child) {
return wrapNode(child, proxy)
})
// @ts-ignore `node` is a parent.
return node.children.map((/** @type {Node} */ c) => wrapNode(c, proxy))
}
}
19 changes: 18 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,34 @@
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
],
"dependencies": {
"@types/unist": "^2.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"clone": "^2.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",
"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 @@ -59,6 +70,7 @@
"xo": {
"prettier": true,
"rules": {
"capitalized-comments": "off",
"no-var": "off",
"prefer-arrow-callback": "off"
}
Expand All @@ -67,5 +79,10 @@
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true
}
}
7 changes: 6 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ test('immutable', function (t) {
t.deepEqual(ast, original, 'original AST is unchanged')
t.notEqual(root, ast, 'returns a different object')
t.deepEqual(root, ast, 'structurally equivalent')
t.false(ast.children[0].parent, 'original AST does not obtain parent links')
t.equal(
// type-coverage:ignore-next-line, yeah, that’s expected.
ast.children[0].parent,
undefined,
'original AST does not obtain parent links'
)
t.end()
})

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 fcc0040

Please sign in to comment.