Skip to content

Commit 27ac1ee

Browse files
Timothy Lindvallnknapp
Timothy Lindvall
authored andcommitted
Feat: Import TypeScript typings
- Import typings from DefinitelyTyped into repo. - Update typings header to cite contributors from history and git blame. - Update package.json to add typings field.
1 parent 78dd89c commit 27ac1ee

File tree

2 files changed

+357
-0
lines changed

2 files changed

+357
-0
lines changed

lib/handlebars.d.ts

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

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"webpack-dev-server": "^1.12.1"
6060
},
6161
"main": "lib/index.js",
62+
"types": "lib/handlebars.d.ts",
6263
"bin": {
6364
"handlebars": "bin/handlebars"
6465
},

0 commit comments

Comments
 (0)