[idyll-compiler] add types definition for idyll-compiler (#25397)

* add types for idyll-compiler

* remove unnecessary tslint rule for new typedefs
This commit is contained in:
Thanh Ngo
2018-05-01 12:23:25 -04:00
committed by Andy
parent b408a4c1b5
commit 01ee3702e4
4 changed files with 87 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
import compile from "idyll-compiler";
// Dummy log
const log = (msg: string) => {};
// $ExpectType Node[] | Promise<Node[]>
compile("*italics*", { async: false }, () => log("window"));
// $ExpectType Node[] | Promise<Node[]>
compile("Simple **idyll**", { async: false });
// $ExpectType Node[] | Promise<Node[]>
compile(`# Test`);

55
types/idyll-compiler/index.d.ts vendored Normal file
View File

@@ -0,0 +1,55 @@
// Type definitions for idyll-compiler 3.1
// Project: https://github.com/idyll-lang/idyll/tree/master/packages/idyll-compiler
// Definitions by: Thanh Ngo <https://github.com/iocat>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
export type PropType = "variable" | "value" | "expression";
export type PropData = string | number | boolean;
export type PropKey = string;
export type PropValue = [PropType, PropData];
export type Property = [PropKey, PropValue];
type __RecursiveNode<T> = [string, Property[], T[]];
export interface TreeNode extends __RecursiveNode<Node> {}
export type Node = TreeNode | string;
export type AST = Node[];
export type PostProcessor =
| ((ast: AST) => AST)
| ((ast: AST, callback: (err: any, value: AST) => void) => void);
export interface Options {
spellcheck?: boolean;
smartquotes?: boolean;
/**
* If false and there is no postprocessors, compiler returns the AST synchronously
* Otherwise, a promise is returned
*
*/
async?: boolean;
/**
* compiler plugins
* If provided, compiler always compiles asynchronously
*/
postProcessors?: PostProcessor[];
}
/**
* Compiles the given idyllMarkup and returns an AST either synchronously
* or asynchronously.
*
* If postProcessors are provided or options.async is set to true:
* compiler returns a promise
* Otherwise, compile returns the AST synchronously
*
*/
declare function compiler(
input: string,
options?: Options,
callback?: () => void
): Promise<AST> | AST;
export default compiler;

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-compiler-tests.ts"]
}

View File

@@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}