Skip to content

Add JSDoc based types #6

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

Merged
merged 3 commits into from
Apr 19, 2021
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
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
60 changes: 43 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,46 @@
export function u(type, props, value) {
var node = {type: String(type)}
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
* @typedef {import('unist').Literal} Literal
* @typedef {Object.<string, unknown>} Props
* @typedef {Array.<Node>|string} ChildrenOrValue
*
* @typedef {(<T extends string, P extends Record<string, unknown>, C extends Node[]>(type: T, props: P, children: C) => {type: T, children: C} & P)} BuildParentWithProps
* @typedef {(<T extends string, P extends Record<string, unknown>>(type: T, props: P, value: string) => {type: T, value: string} & P)} BuildLiteralWithProps
* @typedef {(<T extends string, P extends Record<string, unknown>>(type: T, props: P) => {type: T} & P)} BuildVoidWithProps
* @typedef {(<T extends string, C extends Node[]>(type: T, children: C) => {type: T, children: C})} BuildParent
* @typedef {(<T extends string>(type: T, value: string) => {type: T, value: string})} BuildLiteral
* @typedef {(<T extends string>(type: T) => {type: T})} BuildVoid
*/

if (
(value === undefined || value === null) &&
(typeof props !== 'object' || Array.isArray(props))
) {
value = props
} else {
Object.assign(node, props)
}
export var u = /**
* @type {BuildVoid & BuildVoidWithProps & BuildLiteral & BuildLiteralWithProps & BuildParent & BuildParentWithProps}
*/ (
/**
* @param {string} type Type of node
* @param {Props|ChildrenOrValue} [props] Additional properties for node (or `children` or `value`)
* @param {ChildrenOrValue} [value] `children` or `value` of node
* @returns {Node}
*/
function (type, props, value) {
/** @type {Node} */
var node = {type: String(type)}

if (Array.isArray(value)) {
node.children = value
} else if (value !== undefined && value !== null) {
node.value = String(value)
}
if (
(value === undefined || value === null) &&
(typeof props === 'string' || Array.isArray(props))
) {
value = props
} else {
Object.assign(node, props)
}

return node
}
if (Array.isArray(value)) {
node.children = value
} else if (value !== undefined && value !== null) {
node.value = String(value)
}

return node
}
)
26 changes: 26 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {expectType, expectAssignable} from 'tsd'
import {Text, List, ListItem, HTML} from 'mdast'
import {u} from './index.js'

expectType<{type: 'example'}>(u('example'))
expectType<{type: 'example'} & {property: true}>(u('example', {property: true}))

const node1 = u('text', 'text')

expectType<{type: 'text'; value: string}>(node1)
expectAssignable<Text>(node1)

const node2 = u('list', [
u('listItem', [u('html', {checked: true}, '<strong>text</strong>')])
])

expectType<{
type: 'list'
children: Array<{
type: 'listItem'
children: Array<{type: 'html'; value: string} & {checked: boolean}>
}>
}>(node2)
expectAssignable<List>(node2)
expectAssignable<ListItem>(node2.children[0])
expectAssignable<HTML>(node2.children[0].children[0])
27 changes: 21 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,35 @@
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "types/index.d.ts",
"types": "index.d.ts",
"files": [
"index.js",
"types/index.d.ts"
"index.d.ts",
"index.js"
],
"dependencies": {
"@types/unist": "^2.0.0"
},
"devDependencies": {
"@types/mdast": "^3.0.0",
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"dtslint": "^4.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",
"tsd": "^0.14.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 && tsd && 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-types": "dtslint types",
"test": "npm run format && npm run test-coverage && npm run test-types"
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
Expand All @@ -66,6 +74,8 @@
"xo": {
"prettier": true,
"rules": {
"capitalized-comments": "off",
"import/no-mutable-exports": "off",
"no-var": "off",
"prefer-arrow-callback": "off"
}
Expand All @@ -74,5 +84,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
}
}
77 changes: 0 additions & 77 deletions types/index.d.ts

This file was deleted.

10 changes: 0 additions & 10 deletions types/tsconfig.json

This file was deleted.

7 changes: 0 additions & 7 deletions types/tslint.json

This file was deleted.

16 changes: 0 additions & 16 deletions types/unist-builder-tests.ts

This file was deleted.