add type declarations for idyll-ast (#25453)

This commit is contained in:
Thanh Ngo
2018-05-03 09:46:08 -07:00
committed by Andy
parent 38a91aa931
commit b7759e6aab
4 changed files with 255 additions and 0 deletions
+99
View File
@@ -0,0 +1,99 @@
import { AST, Property, TreeNode, Node } from "idyll-compiler";
import {
appendNode,
getNodesByName,
appendNodes,
prependNode,
prependNodes,
createNode,
getChildren,
walkNodes,
findNodes,
modifyChildren,
filterChildren,
filterNodes,
modifyNodesByName,
getProperty,
getProperties,
getPropertiesByType,
removeNodesByName,
setProperty,
setProperties,
removeProperty
} from "idyll-ast";
const ast: AST = [
["h2", [], []],
"world",
["h1", [], ["child1", ["child2", [], []]]]
];
const prop: Property = ["className", ["value", "hello"]];
// $ExpectType Node[]
appendNode(ast, "hello");
// $ExpectType Node[]
appendNode(getNodesByName(ast, "div"), "test");
// $ExpectType Node[]
appendNodes(ast, [["strong", [], ["div", ["pre", [], []]]], "test"]);
// $ExpectType Node[]
prependNode(getNodesByName(ast, "div"), "test");
// $ExpectType Node[]
prependNodes(ast, [["strong", [], ["div", ["pre", [], []]]], "test"]);
// $ExpectType TreeNode
createNode("div", { prop1: "prop1", prop2: ["expression", "x=2"] }, []);
// $ExpectType Node[]
getChildren(ast[0]);
// $ExpectType void
walkNodes(ast, (n: Node) => {
(n as TreeNode)[0] = "funky-name";
});
// $ExpectType Node[]
findNodes(ast, n => n instanceof Array);
// $ExpectType Node
modifyChildren(ast[0], (n: Node) => {
if (typeof n === "object") {
n[0] = "somename";
}
});
// $ExpectType Node[]
getNodesByName(ast, "h1");
// $ExpectType Node
filterChildren(ast[1], n => n === "world");
// $ExpectType Node[]
filterNodes(ast, n => (n instanceof Object ? n[0] === "h1" : false));
// $ExpectType Node[]
modifyNodesByName(ast, "h2", n => {
typeof n === "object" ? (n[1] = []) : undefined;
});
// $ExpectType [PropType, PropData] | null
getProperty(ast[1], "someProp");
// $ExpectType [string, [PropType, PropData]]
getProperties(ast[1])[0];
// $ExpectType [string, [PropType, PropData]][]
getPropertiesByType(["h1", [], []], "variable");
// $ExpectType Node[]
removeNodesByName(ast, "h1");
// $ExpectType Node
setProperty(ast[0], "prop", 9);
// $ExpectType Node
setProperties(ast[1], { prop1: ["expression", "x"], prop2: 3 });
// $ExpectType Node
removeProperty(ast[0], "prop1");
+137
View File
@@ -0,0 +1,137 @@
// Type definitions for idyll-ast 1.3
// Project: https://github.com/idyll-lang/idyll/tree/master/packages/idyll-ast
// Definitions by: Thanh Ngo <https://github.com/iocat>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
import {
AST,
Node,
TreeNode,
PropValue,
Property,
PropType
} from "idyll-compiler";
/**
* appends a single node to the AST
*/
export function appendNode(ast: AST, node: Node): AST;
/**
* appends multiple nodes to the AST
*/
export function appendNodes(ast: AST, nodes: AST): AST;
/**
* creates a node
*/
export function createNode(
name: string,
props: Record<string, PropValue | PropValue[1]>,
children: Node[]
): TreeNode;
/**
* gets all children of the node if there is any
* returns an empty array if there is no child
*/
export function getChildren(node: Node): Node[];
/**
* executes func against every node
*/
export function walkNodes(ast: AST | null, func: (n: Node) => void): void;
/**
* deeply walks the AST tree and returns all the nodes that satisfies the
* given filter
*/
export function findNodes(ast: AST, filter: (n: Node) => boolean): Node[];
/**
* visits each child of the node and modifies them using the modifier
*/
export function modifyChildren(node: Node, modifier: (n: Node) => void): Node;
/**
* get all nodes by name
*/
export function getNodesByName(ast: AST, name: string): Node[];
/**
* filters and returns the same node where all children
* satisfy the given filter
*/
export function filterChildren(
node: Node,
filter: (child: Node) => boolean
): Node;
/**
* filters every node in the AST and returns a new AST whose nodes
* satisfy the given filter
*/
export function filterNodes(ast: AST, filter: (node: Node) => boolean): AST;
/**
* applies the modifier against node whose name is the given name
* Returns a new ast with the modified nodes
*
* Names are case-insensitive
*/
export function modifyNodesByName(
ast: AST,
name: string,
modifier: (node: Node) => void
): AST;
/**
* gets the node's property value
*
* returns null if node is a string node
*/
export function getProperty(node: Node, key: string): PropValue | null;
/**
* returns all node's properties
*
* returns empty array if node is a string node
*/
export function getProperties(node: Node): Property[];
/**
* returns all properties having the given property's type
*/
export function getPropertiesByType(node: Node, type: PropType): Property[];
export function prependNode(ast: AST, node: Node): AST;
export function prependNodes(ast: AST, nodes: Node[]): AST;
/**
* returns a new AST with nodes having the given name
*
* Names are case-insensitive
*/
export function removeNodesByName(ast: AST, name: string): AST;
/**
* sets the property of the node
*
* if value is an array, the property's type is assumed to be included in it
* otherwise, "value" will be the property's type, and parameter value
* is the property's value
*
*/
export function setProperty(
node: Node,
key: string,
value: PropValue | PropValue[1]
): Node;
/**
* sets the properties of the node
*
* also see setProperty()
*/
export function setProperties(
node: Node,
properties: Record<string, PropValue | PropValue[1]>
): Node;
/**
* removes the node's property which has the given key
*/
export function removeProperty(node: Node, key: string): Node;
+16
View File
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"strictFunctionTypes": true
},
"files": ["index.d.ts", "idyll-ast-tests.ts"]
}
+3
View File
@@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}