-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes GH-3. Reviewed-by: Titus Wormer <tituswormer@gmail.com> Reviewed-by: Junyoung Choi <fluke8259@gmail.com>
- Loading branch information
1 parent
aeea007
commit 99082ac
Showing
5 changed files
with
120 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// TypeScript Version: 3.5 | ||
|
||
import {Node} from 'unist' | ||
|
||
// NOTE: namespace is needed to use `export = unistBuilder` | ||
declare namespace unistBuilder {} | ||
|
||
// NOTE: the order of the unistBuilder overloads is important. | ||
// Looking at the generics' "extends" left to right. | ||
// It should go from more specific types higher in the file, to more broad types lower in the file. | ||
|
||
/** | ||
* Creates a node, with a given type | ||
* | ||
* @param type type of node | ||
*/ | ||
declare function unistBuilder<T extends string>(type: T): {type: T} | ||
|
||
/** | ||
* Creates a node, with type and value | ||
* | ||
* @param type type of node | ||
* @param value value property of node | ||
*/ | ||
declare function unistBuilder<T extends string>( | ||
type: T, | ||
value: string | ||
): {type: T; value: string} | ||
|
||
/** | ||
* Creates a node, with type, props, and value | ||
* | ||
* @param type type of node | ||
* @param props additional properties for node | ||
* @param value value property of node | ||
*/ | ||
declare function unistBuilder<T extends string, P extends {}>( | ||
type: T, | ||
props: P, | ||
value: string | ||
): {type: T; value: string} & P | ||
|
||
/** | ||
* Creates a node, with type and children | ||
* | ||
* @param type type of node | ||
* @param children child nodes of the current node | ||
*/ | ||
declare function unistBuilder<T extends string, C extends Node[]>( | ||
type: T, | ||
children: C | ||
): {type: T; children: C} | ||
|
||
/** | ||
* Creates a node, with type, props, and children | ||
* | ||
* @param type type of node | ||
* @param props additional properties for node | ||
* @param children child nodes of the current node | ||
*/ | ||
declare function unistBuilder<T extends string, P extends {}, C extends Node[]>( | ||
type: T, | ||
props: P, | ||
children: C | ||
): {type: T; children: C} & P | ||
|
||
/** | ||
* Creates a node, with type and props | ||
* | ||
* @param type type of node | ||
* @param props additional properties for node | ||
*/ | ||
declare function unistBuilder<T extends string, P extends {}>( | ||
type: T, | ||
props: P | ||
): {type: T} & P | ||
|
||
export = unistBuilder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"lib": ["es2015"], | ||
"strict": true, | ||
"baseUrl": ".", | ||
"paths": { | ||
"unist-builder": ["index.d.ts"] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "dtslint/dtslint.json", | ||
"rules": { | ||
"whitespace": false, | ||
"semicolon": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as u from 'unist-builder' | ||
import {Text, List, ListItem, HTML} from 'mdast' | ||
|
||
u('example') // $ExpectType { type: "example"; } | ||
u('example', {property: true}) // $ExpectType { type: "example"; } & { property: boolean; } | ||
const node1 = u('text', 'text') // $ExpectType { type: "text"; value: string; } | ||
const text: Text = node1 | ||
|
||
// $ExpectType { type: "list"; children: { type: "listItem"; children: ({ type: "html"; value: string; } & { checked: boolean; })[]; }[]; } | ||
const node2 = u('list', [ | ||
u('listItem', [u('html', {checked: true}, '<strong>text</strong>')]) | ||
]) | ||
|
||
const list: List = node2 | ||
const listItem: ListItem = node2.children[0] | ||
const html: HTML = node2.children[0].children[0] |