UNPKG

11.7 kBTypeScriptView Raw
1/* These definitions were imported from https://github.com/DefinitelyTyped/DefinitelyTyped
2 * and includes previous contributions from the DefinitelyTyped community by:
3 * - Albert Willemsen <https://github.com/AlbertWillemsen-Centric>
4 * - Boris Yankov <https://github.com/borisyankov>
5 * - Jessica Franco <https://github.com/Kovensky>
6 * - Masahiro Wakame <https://github.com/vvakame>
7 * - Raanan Weber <https://github.com/RaananW>
8 * - Sergei Dorogin <https://github.com/evil-shrike>
9 * - webbiesdk <https://github.com/webbiesdk>
10 * - Andrew Leedham <https://github.com/AndrewLeedham>
11 * - Nils Knappmeier <https://github.com/nknapp>
12 * For full history prior to their migration to handlebars.js, please see:
13 * https://github.com/DefinitelyTyped/DefinitelyTyped/commits/1ce60bdc07f10e0b076778c6c953271c072bc894/types/handlebars/index.d.ts
14 */
15// TypeScript Version: 2.3
16
17declare namespace Handlebars {
18 export interface TemplateDelegate<T = any> {
19 (context: T, options?: RuntimeOptions): string;
20 }
21
22 export type Template<T = any> = TemplateDelegate<T>|string;
23
24 export interface RuntimeOptions {
25 partial?: boolean;
26 depths?: any[];
27 helpers?: { [name: string]: Function };
28 partials?: { [name: string]: HandlebarsTemplateDelegate };
29 decorators?: { [name: string]: Function };
30 data?: any;
31 blockParams?: any[];
32 allowCallsToHelperMissing?: boolean;
33 }
34
35 export interface HelperOptions {
36 fn: TemplateDelegate;
37 inverse: TemplateDelegate;
38 hash: any;
39 data?: any;
40 }
41
42 export interface HelperDelegate {
43 (context?: any, arg1?: any, arg2?: any, arg3?: any, arg4?: any, arg5?: any, options?: HelperOptions): any;
44 }
45 export interface HelperDeclareSpec {
46 [key: string]: HelperDelegate;
47 }
48
49 export interface ParseOptions {
50 srcName?: string,
51 ignoreStandalone?: boolean
52 }
53
54 export function registerHelper(name: string, fn: HelperDelegate): void;
55 export function registerHelper(name: HelperDeclareSpec): void;
56 export function unregisterHelper(name: string): void;
57
58 export function registerPartial(name: string, fn: Template): void;
59 export function registerPartial(spec: { [name: string]: HandlebarsTemplateDelegate }): void;
60 export function unregisterPartial(name: string): void;
61
62 // TODO: replace Function with actual signature
63 export function registerDecorator(name: string, fn: Function): void;
64 export function unregisterDecorator(name: string): void;
65
66 export function K(): void;
67 export function createFrame(object: any): any;
68 export function blockParams(obj: any[], ids: any[]): any[];
69 export function Exception(message: string): void;
70 export function log(level: number, obj: any): void;
71 export function parse(input: string, options?: ParseOptions): hbs.AST.Program;
72 export function compile<T = any>(input: any, options?: CompileOptions): HandlebarsTemplateDelegate<T>;
73 export function precompile(input: any, options?: PrecompileOptions): TemplateSpecification;
74 export function template<T = any>(precompilation: TemplateSpecification): HandlebarsTemplateDelegate<T>;
75
76 export function create(): typeof Handlebars;
77
78 export const escapeExpression: typeof Utils.escapeExpression;
79 //export const Utils: typeof hbs.Utils;
80 export const logger: Logger;
81 export const templates: HandlebarsTemplates;
82 export const helpers: { [name: string]: HelperDelegate };
83 export const partials: { [name: string]: any };
84 // TODO: replace Function with actual signature
85 export const decorators: { [name: string]: Function };
86
87 export function noConflict(): typeof Handlebars;
88
89 export class SafeString {
90 constructor(str: string);
91 toString(): string;
92 toHTML(): string;
93 }
94
95 export namespace Utils {
96 export function escapeExpression(str: string): string;
97 export function createFrame(object: any): any;
98 export function blockParams(obj: any[], ids: any[]): any[];
99 export function isEmpty(obj: any) : boolean;
100 export function extend(obj: any, ...source: any[]): any;
101 export function toString(obj: any): string;
102 export function isArray(obj: any): boolean;
103 export function isFunction(obj: any): boolean;
104 }
105
106 export namespace AST {
107 export const helpers: hbs.AST.helpers;
108 }
109
110 interface ICompiler {
111 accept(node: hbs.AST.Node): void;
112 Program(program: hbs.AST.Program): void;
113 BlockStatement(block: hbs.AST.BlockStatement): void;
114 PartialStatement(partial: hbs.AST.PartialStatement): void;
115 PartialBlockStatement(partial: hbs.AST.PartialBlockStatement): void;
116 DecoratorBlock(decorator: hbs.AST.DecoratorBlock): void;
117 Decorator(decorator: hbs.AST.Decorator): void;
118 MustacheStatement(mustache: hbs.AST.MustacheStatement): void;
119 ContentStatement(content: hbs.AST.ContentStatement): void;
120 CommentStatement(comment?: hbs.AST.CommentStatement): void;
121 SubExpression(sexpr: hbs.AST.SubExpression): void;
122 PathExpression(path: hbs.AST.PathExpression): void;
123 StringLiteral(str: hbs.AST.StringLiteral): void;
124 NumberLiteral(num: hbs.AST.NumberLiteral): void;
125 BooleanLiteral(bool: hbs.AST.BooleanLiteral): void;
126 UndefinedLiteral(): void;
127 NullLiteral(): void;
128 Hash(hash: hbs.AST.Hash): void;
129 }
130
131 export class Visitor implements ICompiler {
132 accept(node: hbs.AST.Node): void;
133 acceptKey(node: hbs.AST.Node, name: string): void;
134 acceptArray(arr: hbs.AST.Expression[]): void;
135 Program(program: hbs.AST.Program): void;
136 BlockStatement(block: hbs.AST.BlockStatement): void;
137 PartialStatement(partial: hbs.AST.PartialStatement): void;
138 PartialBlockStatement(partial: hbs.AST.PartialBlockStatement): void;
139 DecoratorBlock(decorator: hbs.AST.DecoratorBlock): void;
140 Decorator(decorator: hbs.AST.Decorator): void;
141 MustacheStatement(mustache: hbs.AST.MustacheStatement): void;
142 ContentStatement(content: hbs.AST.ContentStatement): void;
143 CommentStatement(comment?: hbs.AST.CommentStatement): void;
144 SubExpression(sexpr: hbs.AST.SubExpression): void;
145 PathExpression(path: hbs.AST.PathExpression): void;
146 StringLiteral(str: hbs.AST.StringLiteral): void;
147 NumberLiteral(num: hbs.AST.NumberLiteral): void;
148 BooleanLiteral(bool: hbs.AST.BooleanLiteral): void;
149 UndefinedLiteral(): void;
150 NullLiteral(): void;
151 Hash(hash: hbs.AST.Hash): void;
152 }
153
154
155 export interface ResolvePartialOptions {
156 name: string;
157 helpers?: { [name: string]: Function };
158 partials?: { [name: string]: HandlebarsTemplateDelegate };
159 decorators?: { [name: string]: Function };
160 data?: any;
161 }
162
163 export namespace VM {
164 /**
165 * @deprecated
166 */
167 export function resolvePartial<T = any>(partial: HandlebarsTemplateDelegate<T> | undefined, context: any, options: ResolvePartialOptions): HandlebarsTemplateDelegate<T>;
168 }
169}
170
171/**
172* Implement this interface on your MVW/MVVM/MVC views such as Backbone.View
173**/
174interface HandlebarsTemplatable {
175 template: HandlebarsTemplateDelegate;
176}
177
178// NOTE: for backward compatibility of this typing
179type HandlebarsTemplateDelegate<T = any> = Handlebars.TemplateDelegate<T>;
180
181interface HandlebarsTemplates {
182 [index: string]: HandlebarsTemplateDelegate;
183}
184
185interface TemplateSpecification {
186
187}
188
189// for backward compatibility of this typing
190type RuntimeOptions = Handlebars.RuntimeOptions;
191
192interface CompileOptions {
193 data?: boolean;
194 compat?: boolean;
195 knownHelpers?: KnownHelpers;
196 knownHelpersOnly?: boolean;
197 noEscape?: boolean;
198 strict?: boolean;
199 assumeObjects?: boolean;
200 preventIndent?: boolean;
201 ignoreStandalone?: boolean;
202 explicitPartialContext?: boolean;
203}
204
205type KnownHelpers = {
206 [name in BuiltinHelperName | CustomHelperName]: boolean;
207};
208
209type BuiltinHelperName =
210 "helperMissing"|
211 "blockHelperMissing"|
212 "each"|
213 "if"|
214 "unless"|
215 "with"|
216 "log"|
217 "lookup";
218
219type CustomHelperName = string;
220
221interface PrecompileOptions extends CompileOptions {
222 srcName?: string;
223 destName?: string;
224}
225
226declare namespace hbs {
227 // for backward compatibility of this typing
228 type SafeString = Handlebars.SafeString;
229
230 type Utils = typeof Handlebars.Utils;
231}
232
233interface Logger {
234 DEBUG: number;
235 INFO: number;
236 WARN: number;
237 ERROR: number;
238 level: number;
239
240 methodMap: { [level: number]: string };
241
242 log(level: number, obj: string): void;
243}
244
245type CompilerInfo = [number/* revision */, string /* versions */];
246
247declare namespace hbs {
248 namespace AST {
249 interface Node {
250 type: string;
251 loc: SourceLocation;
252 }
253
254 interface SourceLocation {
255 source: string;
256 start: Position;
257 end: Position;
258 }
259
260 interface Position {
261 line: number;
262 column: number;
263 }
264
265 interface Program extends Node {
266 body: Statement[];
267 blockParams: string[];
268 }
269
270 interface Statement extends Node {}
271
272 interface MustacheStatement extends Statement {
273 path: PathExpression | Literal;
274 params: Expression[];
275 hash: Hash;
276 escaped: boolean;
277 strip: StripFlags;
278 }
279
280 interface Decorator extends MustacheStatement { }
281
282 interface BlockStatement extends Statement {
283 path: PathExpression;
284 params: Expression[];
285 hash: Hash;
286 program: Program;
287 inverse: Program;
288 openStrip: StripFlags;
289 inverseStrip: StripFlags;
290 closeStrip: StripFlags;
291 }
292
293 interface DecoratorBlock extends BlockStatement { }
294
295 interface PartialStatement extends Statement {
296 name: PathExpression | SubExpression;
297 params: Expression[];
298 hash: Hash;
299 indent: string;
300 strip: StripFlags;
301 }
302
303 interface PartialBlockStatement extends Statement {
304 name: PathExpression | SubExpression;
305 params: Expression[];
306 hash: Hash;
307 program: Program;
308 openStrip: StripFlags;
309 closeStrip: StripFlags;
310 }
311
312 interface ContentStatement extends Statement {
313 value: string;
314 original: StripFlags;
315 }
316
317 interface CommentStatement extends Statement {
318 value: string;
319 strip: StripFlags;
320 }
321
322 interface Expression extends Node {}
323
324 interface SubExpression extends Expression {
325 path: PathExpression;
326 params: Expression[];
327 hash: Hash;
328 }
329
330 interface PathExpression extends Expression {
331 data: boolean;
332 depth: number;
333 parts: string[];
334 original: string;
335 }
336
337 interface Literal extends Expression {}
338 interface StringLiteral extends Literal {
339 value: string;
340 original: string;
341 }
342
343 interface BooleanLiteral extends Literal {
344 value: boolean;
345 original: boolean;
346 }
347
348 interface NumberLiteral extends Literal {
349 value: number;
350 original: number;
351 }
352
353 interface UndefinedLiteral extends Literal {}
354
355 interface NullLiteral extends Literal {}
356
357 interface Hash extends Node {
358 pairs: HashPair[];
359 }
360
361 interface HashPair extends Node {
362 key: string;
363 value: Expression;
364 }
365
366 interface StripFlags {
367 open: boolean;
368 close: boolean;
369 }
370
371 interface helpers {
372 helperExpression(node: Node): boolean;
373 scopeId(path: PathExpression): boolean;
374 simpleId(path: PathExpression): boolean;
375 }
376 }
377}
378
379declare module "handlebars" {
380 export = Handlebars;
381}
382
383declare module "handlebars/runtime" {
384 export = Handlebars;
385}