Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Do not use declare module in first-party type declarations #2053

Open
wants to merge 1 commit into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions runtime.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import Handlebars = require('handlebars')
import Handlebars = require('handlebars');

declare module "handlebars/runtime" {

}
export = Handlebars;
294 changes: 146 additions & 148 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
*/
// TypeScript Version: 2.3

declare namespace Handlebars {
export interface TemplateDelegate<T = any> {
declare global { // global for backwards compatibility

namespace Handlebars {
export interface TemplateDelegate<T = any> {
(context: T, options?: RuntimeOptions): string;
}
}

export type Template<T = any> = TemplateDelegate<T>|string;
export type Template<T = any> = TemplateDelegate<T>|string;

export interface RuntimeOptions {
export interface RuntimeOptions {
partial?: boolean;
depths?: any[];
helpers?: { [name: string]: Function };
Expand All @@ -34,65 +36,65 @@ declare namespace Handlebars {
allowedProtoMethods?: { [name: string]: boolean };
allowProtoPropertiesByDefault?: boolean;
allowProtoMethodsByDefault?: boolean;
}
}

export interface HelperOptions {
export interface HelperOptions {
fn: TemplateDelegate;
inverse: TemplateDelegate;
hash: Record<string, any>;
data?: any;
}
}

export interface HelperDelegate {
export interface HelperDelegate {
(context?: any, arg1?: any, arg2?: any, arg3?: any, arg4?: any, arg5?: any, options?: HelperOptions): any;
}
export interface HelperDeclareSpec {
}
export interface HelperDeclareSpec {
[key: string]: HelperDelegate;
}
}

export interface ParseOptions {
export interface ParseOptions {
srcName?: string;
ignoreStandalone?: boolean;
}

export function registerHelper(name: string, fn: HelperDelegate): void;
export function registerHelper(name: HelperDeclareSpec): void;
export function unregisterHelper(name: string): void;

export function registerPartial(name: string, fn: Template): void;
export function registerPartial(spec: { [name: string]: Template }): void;
export function unregisterPartial(name: string): void;

// TODO: replace Function with actual signature
export function registerDecorator(name: string, fn: Function): void;
export function unregisterDecorator(name: string): void;

export function K(): void;
export function createFrame(object: any): any;
export function blockParams(obj: any[], ids: any[]): any[];
export function log(level: number, obj: any): void;
export function parse(input: string, options?: ParseOptions): hbs.AST.Program;
export function parseWithoutProcessing(input: string, options?: ParseOptions): hbs.AST.Program;
export function compile<T = any>(input: any, options?: CompileOptions): HandlebarsTemplateDelegate<T>;
export function precompile(input: any, options?: PrecompileOptions): TemplateSpecification;
export function template<T = any>(precompilation: TemplateSpecification): HandlebarsTemplateDelegate<T>;

export function create(): typeof Handlebars;

export const escapeExpression: typeof Utils.escapeExpression;
//export const Utils: typeof hbs.Utils;
export const logger: Logger;
export const templates: HandlebarsTemplates;
export const helpers: { [name: string]: HelperDelegate };
export const partials: { [name: string]: any };
// TODO: replace Function with actual signature
export const decorators: { [name: string]: Function };

export const VERSION: string;

export function noConflict(): typeof Handlebars;

export class Exception {
}

export function registerHelper(name: string, fn: HelperDelegate): void;
export function registerHelper(name: HelperDeclareSpec): void;
export function unregisterHelper(name: string): void;

export function registerPartial(name: string, fn: Template): void;
export function registerPartial(spec: { [name: string]: Template }): void;
export function unregisterPartial(name: string): void;

// TODO: replace Function with actual signature
export function registerDecorator(name: string, fn: Function): void;
export function unregisterDecorator(name: string): void;

export function K(): void;
export function createFrame(object: any): any;
export function blockParams(obj: any[], ids: any[]): any[];
export function log(level: number, obj: any): void;
export function parse(input: string, options?: ParseOptions): hbs.AST.Program;
export function parseWithoutProcessing(input: string, options?: ParseOptions): hbs.AST.Program;
export function compile<T = any>(input: any, options?: CompileOptions): HandlebarsTemplateDelegate<T>;
export function precompile(input: any, options?: PrecompileOptions): TemplateSpecification;
export function template<T = any>(precompilation: TemplateSpecification): HandlebarsTemplateDelegate<T>;

export function create(): typeof Handlebars;

export const escapeExpression: typeof Utils.escapeExpression;
//export const Utils: typeof hbs.Utils;
export const logger: Logger;
export const templates: HandlebarsTemplates;
export const helpers: { [name: string]: HelperDelegate };
export const partials: { [name: string]: any };
// TODO: replace Function with actual signature
export const decorators: { [name: string]: Function };

export const VERSION: string;

export function noConflict(): typeof Handlebars;

export class Exception {
constructor(message: string, node?: hbs.AST.Node);
description: string;
fileName: string;
Expand All @@ -104,15 +106,15 @@ declare namespace Handlebars {
stack?: string;
column?: any;
endColumn?: any;
}
}

export class SafeString {
export class SafeString {
constructor(str: string);
toString(): string;
toHTML(): string;
}
}

export namespace Utils {
export namespace Utils {
export function escapeExpression(str: string): string;
export function createFrame(object: any): any;
export function blockParams(obj: any[], ids: any[]): any[];
Expand All @@ -121,13 +123,13 @@ declare namespace Handlebars {
export function toString(obj: any): string;
export function isArray(obj: any): boolean;
export function isFunction(obj: any): boolean;
}
}

export namespace AST {
export namespace AST {
export const helpers: hbs.AST.helpers;
}
}

interface ICompiler {
interface ICompiler {
accept(node: hbs.AST.Node): void;
Program(program: hbs.AST.Program): void;
BlockStatement(block: hbs.AST.BlockStatement): void;
Expand All @@ -146,9 +148,9 @@ declare namespace Handlebars {
UndefinedLiteral(): void;
NullLiteral(): void;
Hash(hash: hbs.AST.Hash): void;
}
}

export class Visitor implements ICompiler {
export class Visitor implements ICompiler {
accept(node: hbs.AST.Node): void;
acceptKey(node: hbs.AST.Node, name: string): void;
acceptArray(arr: hbs.AST.Expression[]): void;
Expand All @@ -169,103 +171,103 @@ declare namespace Handlebars {
UndefinedLiteral(): void;
NullLiteral(): void;
Hash(hash: hbs.AST.Hash): void;
}
}


export interface ResolvePartialOptions {
name: string;
helpers?: { [name: string]: Function };
partials?: { [name: string]: HandlebarsTemplateDelegate };
decorators?: { [name: string]: Function };
data?: any;
export interface ResolvePartialOptions {
name: string;
helpers?: { [name: string]: Function };
partials?: { [name: string]: HandlebarsTemplateDelegate };
decorators?: { [name: string]: Function };
data?: any;
}

export namespace VM {
/**
* @deprecated
*/
export function resolvePartial<T = any>(partial: HandlebarsTemplateDelegate<T> | undefined, context: any, options: ResolvePartialOptions): HandlebarsTemplateDelegate<T>;
}
}

export namespace VM {
/**
* @deprecated
*/
export function resolvePartial<T = any>(partial: HandlebarsTemplateDelegate<T> | undefined, context: any, options: ResolvePartialOptions): HandlebarsTemplateDelegate<T>;
/**
* Implement this interface on your MVW/MVVM/MVC views such as Backbone.View
**/
interface HandlebarsTemplatable {
template: HandlebarsTemplateDelegate;
}
}

/**
* Implement this interface on your MVW/MVVM/MVC views such as Backbone.View
**/
interface HandlebarsTemplatable {
template: HandlebarsTemplateDelegate;
}

// NOTE: for backward compatibility of this typing
type HandlebarsTemplateDelegate<T = any> = Handlebars.TemplateDelegate<T>;
// NOTE: for backward compatibility of this typing
type HandlebarsTemplateDelegate<T = any> = Handlebars.TemplateDelegate<T>;

interface HandlebarsTemplates {
[index: string]: HandlebarsTemplateDelegate;
}
interface HandlebarsTemplates {
[index: string]: HandlebarsTemplateDelegate;
}

interface TemplateSpecification {
interface TemplateSpecification {

}
}

// for backward compatibility of this typing
type RuntimeOptions = Handlebars.RuntimeOptions;

interface CompileOptions {
data?: boolean;
compat?: boolean;
knownHelpers?: KnownHelpers;
knownHelpersOnly?: boolean;
noEscape?: boolean;
strict?: boolean;
assumeObjects?: boolean;
preventIndent?: boolean;
ignoreStandalone?: boolean;
explicitPartialContext?: boolean;
}
// for backward compatibility of this typing
type RuntimeOptions = Handlebars.RuntimeOptions;

interface CompileOptions {
data?: boolean;
compat?: boolean;
knownHelpers?: KnownHelpers;
knownHelpersOnly?: boolean;
noEscape?: boolean;
strict?: boolean;
assumeObjects?: boolean;
preventIndent?: boolean;
ignoreStandalone?: boolean;
explicitPartialContext?: boolean;
}

type KnownHelpers = {
[name in BuiltinHelperName | CustomHelperName]: boolean;
};

type BuiltinHelperName =
"helperMissing"|
"blockHelperMissing"|
"each"|
"if"|
"unless"|
"with"|
"log"|
"lookup";

type CustomHelperName = string;

interface PrecompileOptions extends CompileOptions {
srcName?: string;
destName?: string;
}
type KnownHelpers = {
[name in BuiltinHelperName | CustomHelperName]: boolean;
};

type BuiltinHelperName =
"helperMissing"|
"blockHelperMissing"|
"each"|
"if"|
"unless"|
"with"|
"log"|
"lookup";

type CustomHelperName = string;

interface PrecompileOptions extends CompileOptions {
srcName?: string;
destName?: string;
}

declare namespace hbs {
// for backward compatibility of this typing
type SafeString = Handlebars.SafeString;
namespace hbs {
// for backward compatibility of this typing
type SafeString = Handlebars.SafeString;

type Utils = typeof Handlebars.Utils;
}
type Utils = typeof Handlebars.Utils;
}

interface Logger {
DEBUG: number;
INFO: number;
WARN: number;
ERROR: number;
level: number;
interface Logger {
DEBUG: number;
INFO: number;
WARN: number;
ERROR: number;
level: number;

methodMap: { [level: number]: string };
methodMap: { [level: number]: string };

log(level: number, obj: string): void;
}
log(level: number, obj: string): void;
}

type CompilerInfo = [number/* revision */, string /* versions */];
type CompilerInfo = [number/* revision */, string /* versions */];

declare namespace hbs {
namespace AST {
namespace hbs {
namespace AST {
interface Node {
type: string;
loc: SourceLocation;
Expand Down Expand Up @@ -410,13 +412,9 @@ declare namespace hbs {
scopeId(path: PathExpression): boolean;
simpleId(path: PathExpression): boolean;
}
}
}
}

declare module "handlebars" {
export = Handlebars;
}

declare module "handlebars/runtime" {
export = Handlebars;
}
export = Handlebars;