Skip to content

Commit

Permalink
change HelperThis type
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Dec 23, 2023
1 parent 441d48b commit db834cd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
3 changes: 2 additions & 1 deletion core/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,9 @@ export interface Engine<T = string | { toString(): string }> {
export interface HelperThis {
data: Data;
}

// deno-lint-ignore no-explicit-any
export type Helper = (this: HelperThis, ...args: any[]) => any;
export type Helper = (this: HelperThis | void, ...args: any[]) => any;

/** The options for a template helper */
export interface HelperOptions {
Expand Down
5 changes: 4 additions & 1 deletion plugins/pug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ export class PugEngine implements Engine {
case "filter": {
this.options.filters ||= {};

const filter: Helper = (text: string, opt: Record<string, unknown>) => {
const filter: Helper = function (
text: string,
opt: Record<string, unknown>,
) {
delete opt.filename;
const args = Object.values(opt);
return fn(text, ...args);
Expand Down
6 changes: 3 additions & 3 deletions tests/liquid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ Deno.test("build a site with liquid", async (t) => {
);

site.filter("fromPage", function (key) {
return this.data[key];
return this?.data[key];
});
site.filter("fromPageAsync", function (key) {
return Promise.resolve(this.data[key]);
return Promise.resolve(this?.data[key]);
}, true);
site.helper(
"fromPageTagAsync",
function (key) {
return Promise.resolve(`<strong>${this.data[key]}</strong>`);
return Promise.resolve(`<strong>${this?.data[key]}</strong>`);
},
{ type: "tag", body: true, async: true },
);
Expand Down
6 changes: 3 additions & 3 deletions tests/nunjucks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ Deno.test("build a site with nunjucks", async (t) => {
);

site.filter("fromPage", function (key) {
return this.data[key];
return this?.data[key];
});
site.filter("fromPageAsync", function (key) {
return Promise.resolve(this.data[key]);
return Promise.resolve(this?.data[key]);
}, true);
site.helper(
"fromPageTagAsync",
function (key) {
return Promise.resolve(`<strong>${this.data[key]}</strong>`);
return Promise.resolve(`<strong>${this?.data[key]}</strong>`);
},
{ type: "tag", body: true, async: true },
);
Expand Down

0 comments on commit db834cd

Please sign in to comment.