From b7759e6aab983dfbbab5f18dd9cbd13adaabdd37 Mon Sep 17 00:00:00 2001 From: Thanh Ngo Date: Thu, 3 May 2018 12:46:08 -0400 Subject: [PATCH] add type declarations for idyll-ast (#25453) --- types/idyll-ast/idyll-ast-tests.ts | 99 +++++++++++++++++++++ types/idyll-ast/index.d.ts | 137 +++++++++++++++++++++++++++++ types/idyll-ast/tsconfig.json | 16 ++++ types/idyll-ast/tslint.json | 3 + 4 files changed, 255 insertions(+) create mode 100644 types/idyll-ast/idyll-ast-tests.ts create mode 100644 types/idyll-ast/index.d.ts create mode 100644 types/idyll-ast/tsconfig.json create mode 100644 types/idyll-ast/tslint.json diff --git a/types/idyll-ast/idyll-ast-tests.ts b/types/idyll-ast/idyll-ast-tests.ts new file mode 100644 index 0000000000..6b3eb7d651 --- /dev/null +++ b/types/idyll-ast/idyll-ast-tests.ts @@ -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"); diff --git a/types/idyll-ast/index.d.ts b/types/idyll-ast/index.d.ts new file mode 100644 index 0000000000..4a46320b82 --- /dev/null +++ b/types/idyll-ast/index.d.ts @@ -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 +// 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, + 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 +): Node; + +/** + * removes the node's property which has the given key + */ +export function removeProperty(node: Node, key: string): Node; diff --git a/types/idyll-ast/tsconfig.json b/types/idyll-ast/tsconfig.json new file mode 100644 index 0000000000..1e026c3377 --- /dev/null +++ b/types/idyll-ast/tsconfig.json @@ -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"] +} diff --git a/types/idyll-ast/tslint.json b/types/idyll-ast/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/idyll-ast/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}