UNPKG

12.6 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 log(level: number, obj: any): void;
70 export function parse(input: string, options?: ParseOptions): hbs.AST.Program;
71 export function parseWithoutProcessing(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 Exception {
90 constructor(message: string, node?: hbs.AST.Node);
91 description: string;
92 fileName: string;
93 lineNumber?: any;
94 endLineNumber?: any;
95 message: string;
96 name: string;
97 number: number;
98 stack?: string;
99 column?: any;
100 endColumn?: any;
101 }
102
103 export class SafeString {
104 constructor(str: string);
105 toString(): string;
106 toHTML(): string;
107 }
108
109 export namespace Utils {
110 export function escapeExpression(str: string): string;
111 export function createFrame(object: any): any;
112 export function blockParams(obj: any[], ids: any[]): any[];
113 export function isEmpty(obj: any) : boolean;
114 export function extend(obj: any, ...source: any[]): any;
115 export function toString(obj: any): string;
116 export function isArray(obj: any): boolean;
117 export function isFunction(obj: any): boolean;
118 }
119
120 export namespace AST {
121 export const helpers: hbs.AST.helpers;
122 }
123
124 interface ICompiler {
125 accept(node: hbs.AST.Node): void;
126 Program(program: hbs.AST.Program): void;
127 BlockStatement(block: hbs.AST.BlockStatement): void;
128 PartialStatement(partial: hbs.AST.PartialStatement): void;
129 PartialBlockStatement(partial: hbs.AST.PartialBlockStatement): void;
130 DecoratorBlock(decorator: hbs.AST.DecoratorBlock): void;
131 Decorator(decorator: hbs.AST.Decorator): void;
132 MustacheStatement(mustache: hbs.AST.MustacheStatement): void;
133 ContentStatement(content: hbs.AST.ContentStatement): void;
134 CommentStatement(comment?: hbs.AST.CommentStatement): void;
135 SubExpression(sexpr: hbs.AST.SubExpression): void;
136 PathExpression(path: hbs.AST.PathExpression): void;
137 StringLiteral(str: hbs.AST.StringLiteral): void;
138 NumberLiteral(num: hbs.AST.NumberLiteral): void;
139 BooleanLiteral(bool: hbs.AST.BooleanLiteral): void;
140 UndefinedLiteral(): void;
141 NullLiteral(): void;
142 Hash(hash: hbs.AST.Hash): void;
143 }
144
145 export class Visitor implements ICompiler {
146 accept(node: hbs.AST.Node): void;
147 acceptKey(node: hbs.AST.Node, name: string): void;
148 acceptArray(arr: hbs.AST.Expression[]): void;
149 Program(program: hbs.AST.Program): void;
150 BlockStatement(block: hbs.AST.BlockStatement): void;
151 PartialStatement(partial: hbs.AST.PartialStatement): void;
152 PartialBlockStatement(partial: hbs.AST.PartialBlockStatement): void;
153 DecoratorBlock(decorator: hbs.AST.DecoratorBlock): void;
154 Decorator(decorator: hbs.AST.Decorator): void;
155 MustacheStatement(mustache: hbs.AST.MustacheStatement): void;
156 ContentStatement(content: hbs.AST.ContentStatement): void;
157 CommentStatement(comment?: hbs.AST.CommentStatement): void;
158 SubExpression(sexpr: hbs.AST.SubExpression): void;
159 PathExpression(path: hbs.AST.PathExpression): void;
160 StringLiteral(str: hbs.AST.StringLiteral): void;
161 NumberLiteral(num: hbs.AST.NumberLiteral): void;
162 BooleanLiteral(bool: hbs.AST.BooleanLiteral): void;
163 UndefinedLiteral(): void;
164 NullLiteral(): void;
165 Hash(hash: hbs.AST.Hash): void;
166 }
167
168
169 export interface ResolvePartialOptions {
170 name: string;
171 helpers?: { [name: string]: Function };
172 partials?: { [name: string]: HandlebarsTemplateDelegate };
173 decorators?: { [name: string]: Function };
174 data?: any;
175 }
176
177 export namespace VM {
178 /**
179 * @deprecated
180 */
181 export function resolvePartial<T = any>(partial: HandlebarsTemplateDelegate<T> | undefined, context: any, options: ResolvePartialOptions): HandlebarsTemplateDelegate<T>;
182 }
183}
184
185/**
186* Implement this interface on your MVW/MVVM/MVC views such as Backbone.View
187**/
188interface HandlebarsTemplatable {
189 template: HandlebarsTemplateDelegate;
190}
191
192// NOTE: for backward compatibility of this typing
193type HandlebarsTemplateDelegate<T = any> = Handlebars.TemplateDelegate<T>;
194
195interface HandlebarsTemplates {
196 [index: string]: HandlebarsTemplateDelegate;
197}
198
199interface TemplateSpecification {
200
201}
202
203// for backward compatibility of this typing
204type RuntimeOptions = Handlebars.RuntimeOptions;
205
206interface CompileOptions {
207 data?: boolean;
208 compat?: boolean;
209 knownHelpers?: KnownHelpers;
210 knownHelpersOnly?: boolean;
211 noEscape?: boolean;
212 strict?: boolean;
213 assumeObjects?: boolean;
214 preventIndent?: boolean;
215 ignoreStandalone?: boolean;
216 explicitPartialContext?: boolean;
217}
218
219type KnownHelpers = {
220 [name in BuiltinHelperName | CustomHelperName]: boolean;
221};
222
223type BuiltinHelperName =
224 "helperMissing"|
225 "blockHelperMissing"|
226 "each"|
227 "if"|
228 "unless"|
229 "with"|
230 "log"|
231 "lookup";
232
233type CustomHelperName = string;
234
235interface PrecompileOptions extends CompileOptions {
236 srcName?: string;
237 destName?: string;
238}
239
240declare namespace hbs {
241 // for backward compatibility of this typing
242 type SafeString = Handlebars.SafeString;
243
244 type Utils = typeof Handlebars.Utils;
245}
246
247interface Logger {
248 DEBUG: number;
249 INFO: number;
250 WARN: number;
251 ERROR: number;
252 level: number;
253
254 methodMap: { [level: number]: string };
255
256 log(level: number, obj: string): void;
257}
258
259type CompilerInfo = [number/* revision */, string /* versions */];
260
261declare namespace hbs {
262 namespace AST {
263 interface Node {
264 type: string;
265 loc: SourceLocation;
266 }
267
268 interface SourceLocation {
269 source: string;
270 start: Position;
271 end: Position;
272 }
273
274 interface Position {
275 line: number;
276 column: number;
277 }
278
279 interface Program extends Node {
280 body: Statement[];
281 blockParams: string[];
282 }
283
284 interface Statement extends Node {}
285
286 interface MustacheStatement extends Statement {
287 type: 'MustacheStatement';
288 path: PathExpression | Literal;
289 params: Expression[];
290 hash: Hash;
291 escaped: boolean;
292 strip: StripFlags;
293 }
294
295 interface Decorator extends MustacheStatement { }
296
297 interface BlockStatement extends Statement {
298 type: 'BlockStatement';
299 path: PathExpression;
300 params: Expression[];
301 hash: Hash;
302 program: Program;
303 inverse: Program;
304 openStrip: StripFlags;
305 inverseStrip: StripFlags;
306 closeStrip: StripFlags;
307 }
308
309 interface DecoratorBlock extends BlockStatement { }
310
311 interface PartialStatement extends Statement {
312 type: 'PartialStatement';
313 name: PathExpression | SubExpression;
314 params: Expression[];
315 hash: Hash;
316 indent: string;
317 strip: StripFlags;
318 }
319
320 interface PartialBlockStatement extends Statement {
321 type: 'PartialBlockStatement';
322 name: PathExpression | SubExpression;
323 params: Expression[];
324 hash: Hash;
325 program: Program;
326 openStrip: StripFlags;
327 closeStrip: StripFlags;
328 }
329
330 interface ContentStatement extends Statement {
331 type: 'ContentStatement';
332 value: string;
333 original: StripFlags;
334 }
335
336 interface CommentStatement extends Statement {
337 type: 'CommentStatement';
338 value: string;
339 strip: StripFlags;
340 }
341
342 interface Expression extends Node {}
343
344 interface SubExpression extends Expression {
345 type: 'SubExpression';
346 path: PathExpression;
347 params: Expression[];
348 hash: Hash;
349 }
350
351 interface PathExpression extends Expression {
352 type: 'PathExpression';
353 data: boolean;
354 depth: number;
355 parts: string[];
356 original: string;
357 }
358
359 interface Literal extends Expression {}
360 interface StringLiteral extends Literal {
361 type: 'StringLiteral';
362 value: string;
363 original: string;
364 }
365
366 interface BooleanLiteral extends Literal {
367 type: 'BooleanLiteral';
368 value: boolean;
369 original: boolean;
370 }
371
372 interface NumberLiteral extends Literal {
373 type: 'NumberLiteral';
374 value: number;
375 original: number;
376 }
377
378 interface UndefinedLiteral extends Literal {
379 type: 'UndefinedLiteral';
380 }
381
382 interface NullLiteral extends Literal {
383 type: 'NullLiteral';
384 }
385
386 interface Hash extends Node {
387 type: 'Hash';
388 pairs: HashPair[];
389 }
390
391 interface HashPair extends Node {
392 type: 'HashPair';
393 key: string;
394 value: Expression;
395 }
396
397 interface StripFlags {
398 open: boolean;
399 close: boolean;
400 }
401
402 interface helpers {
403 helperExpression(node: Node): boolean;
404 scopeId(path: PathExpression): boolean;
405 simpleId(path: PathExpression): boolean;
406 }
407 }
408}
409
410declare module "handlebars" {
411 export = Handlebars;
412}
413
414declare module "handlebars/runtime" {
415 export = Handlebars;
416}