diff --git a/pug/pug-tests.ts b/pug/pug-tests.ts new file mode 100644 index 0000000000..5ba7d9ad06 --- /dev/null +++ b/pug/pug-tests.ts @@ -0,0 +1,103 @@ +/// +import * as pug from 'pug'; + + +//////////////////////////////////////////////////////////// +/// Options https://pugjs.org/api/reference.html#options /// +//////////////////////////////////////////////////////////// +namespace options_tests { + let opts: pug.Options; + let str = 'string' + let bool = false; + let strArray = ['string']; + + opts.filename = str; + + opts.basedir = str; + + opts.doctype = str; + + opts.pretty = str; + opts.pretty = bool; + + opts.filters = {}; + + opts.self = bool; + + opts.debug = bool; + opts.compileDebug = bool; + + opts.globals = strArray; + + opts.cache = bool; + + opts.inlineRuntimeFunctions = bool; + + opts.name = str; +} + +//////////////////////////////////////////////////////////// +/// Methods https://pugjs.org/api/reference.html#methods /// +//////////////////////////////////////////////////////////// +namespace methods_tests { + let source = `p #{ name } 's Pug source code!`; + let path = "foo.pug"; + let compileTemplate: pug.compileTemplate; + let template: string; + let clientFunctionString: pug.ClientFunctionString; + let str: string; + + { + /// pug.compile(source, ?options) https://pugjs.org/api/reference.html#pugcompilesource-options + compileTemplate = pug.compile(source); + template = compileTemplate(); + } + + { + /// pug.compileFile(path, ?options) https://pugjs.org/api/reference.html#pugcompilefilepath-options + compileTemplate = pug.compileFile(path); + template = compileTemplate(); + } + + { + /// pug.compileClient(source, ?options) https://pugjs.org/api/reference.html#pugcompileclientsource-options + clientFunctionString = pug.compileClient(path); + str = pug.compileClient(path); + } + + { + /// pug.compileClientWithDependenciesTracked(source, ?options) https://pugjs.org/api/reference.html#pugcompileclientwithdependenciestrackedsource-options + let obj = pug.compileClientWithDependenciesTracked(source); + clientFunctionString = obj.body; + str = obj.body; + let strArray: string[] = obj.dependencies; + } + + { + /// pug.compileFileClient(path, ?options) https://pugjs.org/api/reference.html#pugcompilefileclientpath-options + clientFunctionString = pug.compileFileClient(path); + str = pug.compileFileClient(path); + } + + { + /// pug.render(source, ?options, ?callback) https://pugjs.org/api/reference.html#pugrendersource-options-callback + str = pug.render(source); + + // test type for callback paraments + pug.render(source, {}, (err, html) => { + let e: Error = err; + str = html; + }); + } + + { + /// pug.renderFile(path, ?options, ?callback) https://pugjs.org/api/reference.html#pugrenderfilepath-options-callback + str = pug.renderFile(path); + + // test type for callback paraments + pug.renderFile(path, {}, (err, html) => { + let e: Error = err; + str = html; + }); + } +} diff --git a/pug/pug.d.ts b/pug/pug.d.ts new file mode 100644 index 0000000000..8902aa7cf4 --- /dev/null +++ b/pug/pug.d.ts @@ -0,0 +1,50 @@ +// Type definitions for pug 2.0.0-beta6 +// Project: https://github.com/pugjs/pug +// Definitions by: TonyYang +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/** + * Table of Contents + * + * - Options https://pugjs.org/api/reference.html#options + * - Methods https://pugjs.org/api/reference.html#methods + * + * The order of contents is according to pugjs API document. + */ +declare module 'pug' { + //////////////////////////////////////////////////////////// + /// Options https://pugjs.org/api/reference.html#options /// + //////////////////////////////////////////////////////////// + export interface Options { + filename?: string; + basedir?: string; + doctype?: string; + pretty?: boolean | string; + filters?: any; + self?: boolean; + debug?: boolean; + compileDebug?: boolean; + globals?: string[]; + cache?: boolean; + inlineRuntimeFunctions?: boolean; + name?: string; + } + + //////////////////////////////////////////////////////////// + /// Methods https://pugjs.org/api/reference.html#methods /// + //////////////////////////////////////////////////////////// + export function compile(source: string, options?: Options): (locals?: any) => string; + export function compileFile(path: string, options?: Options): (locals?: any) => string; + export function compileClient(source: string, options?: Options): ClientFunctionString; + export function compileClientWithDependenciesTracked(source: string, options?: Options): { + body: ClientFunctionString; + dependencies: string[]; + }; + export function compileFileClient(path: string, options?: Options): ClientFunctionString; + export function render(source: string, options?: Options, callback?: (err: Error, html: string) => void): string; + export function renderFile(path: string, options?: Options, callback?: (err: Error, html: string) => void): string; + + // else + export type ClientFunctionString = string; // ex: 'function (locals) {...}' + export type compileTemplate = (locals?: any) => string; +}