[pug] Create pug definition (#11258)

* Create pug.d.ts

* Create pug-test.ts

* Add project in comments

* Rename
This commit is contained in:
TonyYang
2016-09-19 15:23:24 +08:00
committed by Masahiro Wakame
parent 4c7b1220e0
commit b36bf17397
2 changed files with 153 additions and 0 deletions

103
pug/pug-tests.ts Normal file
View File

@@ -0,0 +1,103 @@
/// <reference path="pug.d.ts"/>
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;
});
}
}

50
pug/pug.d.ts vendored Normal file
View File

@@ -0,0 +1,50 @@
// Type definitions for pug 2.0.0-beta6
// Project: https://github.com/pugjs/pug
// Definitions by: TonyYang <https://github.com/TonyPythoneer>
// 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;
}