Skip to content

Commit

Permalink
feat: enhance typescript definition for knownHelpers
Browse files Browse the repository at this point in the history
- Add support for custom helpers while keeping
  the list of builtin-helpers to maintain for
  autocompletion support

closes #1544
  • Loading branch information
nknapp committed Aug 29, 2019
1 parent f98c6a5 commit 16572cd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
27 changes: 17 additions & 10 deletions types/index.d.ts
Expand Up @@ -173,16 +173,7 @@ type RuntimeOptions = Handlebars.RuntimeOptions;
interface CompileOptions {
data?: boolean;
compat?: boolean;
knownHelpers?: {
helperMissing?: boolean;
blockHelperMissing?: boolean;
each?: boolean;
if?: boolean;
unless?: boolean;
with?: boolean;
log?: boolean;
lookup?: boolean;
};
knownHelpers?: KnownHelpers;
knownHelpersOnly?: boolean;
noEscape?: boolean;
strict?: boolean;
Expand All @@ -192,6 +183,22 @@ interface CompileOptions {
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;
Expand Down
12 changes: 10 additions & 2 deletions types/test.ts
Expand Up @@ -76,7 +76,7 @@ Handlebars.registerHelper('list', (context, options: Handlebars.HelperOptions) =
}
return ret + "</ul>";
});
template6([{url:"", title:""}])
template6([{url:"", title:""}]);


const escapedExpression = Handlebars.Utils.escapeExpression('<script>alert(\'xss\');</script>');
Expand All @@ -88,4 +88,12 @@ const parsedTmpl = Handlebars.parse('<p>Hello, my name is {{name}}.</p>', {
ignoreStandalone: true
});

const parsedTmplWithoutOptions = Handlebars.parse('<p>Hello, my name is {{name}}.</p>');
const parsedTmplWithoutOptions = Handlebars.parse('<p>Hello, my name is {{name}}.</p>');

// #1544, allow custom helpers in knownHelpers
Handlebars.compile('test', {
knownHelpers: {
each: true,
customHelper: true
}
});

0 comments on commit 16572cd

Please sign in to comment.