Skip to content

Commit

Permalink
Fix for #2536 to allow multiple arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Aug 17, 2022
1 parent 9bbdcd7 commit 972e9fb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
14 changes: 8 additions & 6 deletions src/UserConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,14 @@ class UserConfig {

// This method *requires* `async function` and will not work with `function` that returns a promise
if (callback instanceof ComparisonAsyncFunction) {
this.addNunjucksAsyncFilter(name, async function (value, cb) {
let ret = await callback(value);
this.addNunjucksAsyncFilter(name, async function (...args) {
let cb = args.pop();
let ret = await callback.call(this, ...args);
cb(null, ret);
});
} else {
this.addNunjucksFilter(name, function (value) {
let ret = callback(value);
this.addNunjucksFilter(name, function (...args) {
let ret = callback.call(this, ...args);
if (ret instanceof Promise) {
throw new Error(
`Nunjucks *is* async-friendly with \`addFilter("${name}", async function() {})\` but you need to supply an \`async function\`. You returned a promise from \`addFilter("${name}", function() {})\`. Alternatively, use the \`addAsyncFilter("${name}")\` configuration API method.`
Expand All @@ -269,8 +270,9 @@ class UserConfig {
// namespacing happens downstream
this.addLiquidFilter(name, callback);
this.addJavaScriptFunction(name, callback);
this.addNunjucksAsyncFilter(name, async function (value, cb) {
let ret = await callback(value);
this.addNunjucksAsyncFilter(name, async function (...args) {
let cb = args.pop();
let ret = await callback.call(this, ...args);
cb(null, ret);
});
}
Expand Down
16 changes: 8 additions & 8 deletions test/TemplateRenderNunjucksTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1074,14 +1074,14 @@ test("Use a precompiled Nunjucks template", async (t) => {
test("Make sure addFilter is async-friendly for Nunjucks", async (t) => {
let templateConfig = new TemplateConfig();
// requires async function
templateConfig.userConfig.addFilter("fortytwo", async function () {
return getPromise(42);
templateConfig.userConfig.addFilter("fortytwo", async function (val, val2) {
return getPromise(val + val2);
});

let tr = getNewTemplateRender("njk", null, templateConfig);

let fn = await tr.getCompiledTemplate("<p>{{ 'hi' | fortytwo }}</p>");
t.is(await fn(), "<p>42</p>");
let fn = await tr.getCompiledTemplate("<p>{{ 10 | fortytwo(2) }}</p>");
t.is(await fn(), "<p>12</p>");
});

test("Throw an error when you return a promise in addFilter for Nunjucks", async (t) => {
Expand All @@ -1099,12 +1099,12 @@ test("Throw an error when you return a promise in addFilter for Nunjucks", async
test("addAsyncFilter for Nunjucks", async (t) => {
let templateConfig = new TemplateConfig();
// works without async function (can return promise)
templateConfig.userConfig.addAsyncFilter("fortytwo", function () {
return getPromise(42);
templateConfig.userConfig.addAsyncFilter("fortytwo", function (val, val2) {
return getPromise(val + val2);
});

let tr = getNewTemplateRender("njk", null, templateConfig);

let fn = await tr.getCompiledTemplate("<p>{{ 'hi' | fortytwo }}</p>");
t.is(await fn(), "<p>42</p>");
let fn = await tr.getCompiledTemplate("<p>{{ 10 | fortytwo(2) }}</p>");
t.is(await fn(), "<p>12</p>");
});

0 comments on commit 972e9fb

Please sign in to comment.