diff --git a/types/idyll-compiler/idyll-compiler-tests.ts b/types/idyll-compiler/idyll-compiler-tests.ts new file mode 100644 index 0000000000..c131a8600d --- /dev/null +++ b/types/idyll-compiler/idyll-compiler-tests.ts @@ -0,0 +1,13 @@ +import compile from "idyll-compiler"; + +// Dummy log +const log = (msg: string) => {}; + +// $ExpectType Node[] | Promise +compile("*italics*", { async: false }, () => log("window")); + +// $ExpectType Node[] | Promise +compile("Simple **idyll**", { async: false }); + +// $ExpectType Node[] | Promise +compile(`# Test`); diff --git a/types/idyll-compiler/index.d.ts b/types/idyll-compiler/index.d.ts new file mode 100644 index 0000000000..577dae2070 --- /dev/null +++ b/types/idyll-compiler/index.d.ts @@ -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 +// 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 = [string, Property[], T[]]; +export interface TreeNode extends __RecursiveNode {} +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; + +export default compiler; diff --git a/types/idyll-compiler/tsconfig.json b/types/idyll-compiler/tsconfig.json new file mode 100644 index 0000000000..bb08224d15 --- /dev/null +++ b/types/idyll-compiler/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-compiler-tests.ts"] +} diff --git a/types/idyll-compiler/tslint.json b/types/idyll-compiler/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/idyll-compiler/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}