From 133b96a2ff463dda71febbdae434057271e025ed Mon Sep 17 00:00:00 2001 From: Andrew Leedham Date: Wed, 3 Jul 2019 20:37:42 +0100 Subject: [PATCH 1/2] Add "Handlebars.VM.resolvePartial" to type definitions - Handlebars.VM is actually not part of the API, but Handlebars.VM.resolvePartial is mentioned in the documentation and is thus now treated as part of the API. Closes #1534 --- lib/handlebars/runtime.js | 3 +++ types/index.d.ts | 11 +++++++++++ types/test.ts | 10 ++++++++++ 3 files changed, 24 insertions(+) diff --git a/lib/handlebars/runtime.js b/lib/handlebars/runtime.js index 3884e88c8..a73ebd280 100644 --- a/lib/handlebars/runtime.js +++ b/lib/handlebars/runtime.js @@ -209,6 +209,9 @@ export function wrapProgram(container, i, fn, data, declaredBlockParams, blockPa return prog; } +/** + * This is currently part of the official API, therefore implementation details should not be changed. + */ export function resolvePartial(partial, context, options) { if (!partial) { if (options.name === '@partial-block') { diff --git a/types/index.d.ts b/types/index.d.ts index d075e234a..99ec21364 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -7,6 +7,7 @@ * - Raanan Weber * - Sergei Dorogin * - webbiesdk + * - Andrew Leedham * For full history prior to their migration to handlebars.js, please see: * https://github.com/DefinitelyTyped/DefinitelyTyped/commits/1ce60bdc07f10e0b076778c6c953271c072bc894/types/handlebars/index.d.ts */ @@ -20,6 +21,7 @@ declare namespace Handlebars { export type Template = TemplateDelegate|string; export interface RuntimeOptions { + name?: string; partial?: boolean; depths?: any[]; helpers?: { [name: string]: Function }; @@ -147,6 +149,13 @@ declare namespace Handlebars { NullLiteral(): void; Hash(hash: hbs.AST.Hash): void; } + + export namespace VM { + /** + * @deprecated + */ + export function resolvePartial(partial: HandlebarsTemplateDelegate | undefined, context: any, options: RuntimeOptions): HandlebarsTemplateDelegate; + } } /** @@ -223,6 +232,8 @@ interface Logger { log(level: number, obj: string): void; } +type CompilerInfo = [number/* revision */, string /* versions */]; + declare namespace hbs { namespace AST { interface Node { diff --git a/types/test.ts b/types/test.ts index f242f55bc..24951f48b 100644 --- a/types/test.ts +++ b/types/test.ts @@ -90,6 +90,16 @@ const parsedTmpl = Handlebars.parse('

Hello, my name is {{name}}.

', { const parsedTmplWithoutOptions = Handlebars.parse('

Hello, my name is {{name}}.

'); +// Custom partial resolution. +const originalResolvePartial = Handlebars.VM.resolvePartial; +Handlebars.VM.resolvePartial = (partial: HandlebarsTemplateDelegate | undefined, context: any, options: RuntimeOptions): HandlebarsTemplateDelegate => { + const name = options.name; + // transform name. + options.name = name; + return originalResolvePartial(partial, context, options); +} + + // #1544, allow custom helpers in knownHelpers Handlebars.compile('test', { knownHelpers: { From 888750ec27e6b9126b3af61cf526590b09ef8bd9 Mon Sep 17 00:00:00 2001 From: Nils Knappmeier Date: Tue, 3 Sep 2019 21:32:41 +0200 Subject: [PATCH 2/2] fix typings of resolvePartial-options - derived from the object structure seen in the debugger closes #1534 --- Gruntfile.js | 6 +++--- types/index.d.ts | 13 +++++++++++-- types/test.ts | 4 ++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index fad1be391..7e028a052 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -5,15 +5,15 @@ module.exports = function(grunt) { pkg: grunt.file.readJSON('package.json'), eslint: { - options: { - }, files: [ '*.js', 'bench/**/*.js', 'tasks/**/*.js', 'lib/**/!(*.min|parser).js', 'spec/**/!(*.amd|json2|require).js', - 'integration-testing/**/*.js' + 'integration-testing/multi-nodejs-test/*.js', + 'integration-testing/webpack-test/*.js', + 'integration-testing/webpack-test/src/*.js' ] }, diff --git a/types/index.d.ts b/types/index.d.ts index 99ec21364..a142119a4 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -8,6 +8,7 @@ * - Sergei Dorogin * - webbiesdk * - Andrew Leedham + * - Nils Knappmeier * For full history prior to their migration to handlebars.js, please see: * https://github.com/DefinitelyTyped/DefinitelyTyped/commits/1ce60bdc07f10e0b076778c6c953271c072bc894/types/handlebars/index.d.ts */ @@ -21,7 +22,6 @@ declare namespace Handlebars { export type Template = TemplateDelegate|string; export interface RuntimeOptions { - name?: string; partial?: boolean; depths?: any[]; helpers?: { [name: string]: Function }; @@ -150,11 +150,20 @@ declare namespace Handlebars { 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 namespace VM { /** * @deprecated */ - export function resolvePartial(partial: HandlebarsTemplateDelegate | undefined, context: any, options: RuntimeOptions): HandlebarsTemplateDelegate; + export function resolvePartial(partial: HandlebarsTemplateDelegate | undefined, context: any, options: ResolvePartialOptions): HandlebarsTemplateDelegate; } } diff --git a/types/test.ts b/types/test.ts index 24951f48b..341a3c2f9 100644 --- a/types/test.ts +++ b/types/test.ts @@ -92,8 +92,8 @@ const parsedTmplWithoutOptions = Handlebars.parse('

Hello, my name is {{name}} // Custom partial resolution. const originalResolvePartial = Handlebars.VM.resolvePartial; -Handlebars.VM.resolvePartial = (partial: HandlebarsTemplateDelegate | undefined, context: any, options: RuntimeOptions): HandlebarsTemplateDelegate => { - const name = options.name; +Handlebars.VM.resolvePartial = (partial: HandlebarsTemplateDelegate | undefined, context: any, options: Handlebars.ResolvePartialOptions): HandlebarsTemplateDelegate => { + const name = options.name.replace(/my/,'your'); // transform name. options.name = name; return originalResolvePartial(partial, context, options);