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

Breaking change when upgrading from 4.5.3 #1922

Closed
4 tasks done
pgrever opened this issue Feb 24, 2023 · 5 comments
Closed
4 tasks done

Breaking change when upgrading from 4.5.3 #1922

pgrever opened this issue Feb 24, 2023 · 5 comments

Comments

@pgrever
Copy link

pgrever commented Feb 24, 2023

Before filing issues, please check the following points first:

The following code worked in 4.5.3. When I tried to update to the latest it is failing with the error "TypeError: expected the first argument to be a number". It appears that this started failing in 4.6.0.

const Handlebars = require('handlebars');
const handlebarsHelpers = require('handlebars-helpers');

handlebarsHelpers({ handlebars: Handlebars });

Handlebars.registerHelper('for', (from, to, incr, block) => {
    let accum = '';
    for (let i = from; i < to; i += incr) {
        accum += block.fn(i);
    }
    return accum;
});

const template =
'{\
    "pages": [\
        {{#for 0 pageCount 1}}\
            {{#unless (eq this (subtract pageCount 1))}},{{/unless}}\
        {{/for}}\
    ]\
}';

const data = { pageCount: 1 };
const templateFn = Handlebars.compile(template, { noEscape: true, compat: true });
templateFn(data);

Version of handlebars-helpers is 0.10.0

@jaylinski
Copy link
Member

jaylinski commented Feb 24, 2023

This is an error from the handlebars-helpers library:

https://github.com/helpers/handlebars-helpers/blob/5d3405f178a9ec4c90df3c7dc07b4faf9a09dfd7/lib/math.js#L86

I guess this error is thrown because there is now a strict type-check somewhere.

The reason for the error is that pageCount is undefined inside your for-helper.
You have to make variables available in your helper like this:

https://handlebarsjs.com/playground.html#format=1&currentExample=%7B%22template%22%3A%22%7B%7B%23for%200%20pageCount%201%7D%7D%5Cn%20%20pageCount%3A%20%7B%7Bcount%7D%7D%20%7B%7Bindex%7D%7D%5Cn%20%20%7B%7B%23unless%20(eq%20index%20(subtract%20count%201))%7D%7D%2C%7B%7B%2Funless%7D%7D%5Cn%7B%7B%2Ffor%7D%7D%22%2C%22partials%22%3A%5B%5D%2C%22input%22%3A%22%7B%20pageCount%3A%205%20%7D%22%2C%22output%22%3A%22%20%20pageCount%3A%205%200%5Cn%20%20%2C%5Cn%20%20pageCount%3A%205%201%5Cn%20%20%2C%5Cn%20%20pageCount%3A%205%202%5Cn%20%20%2C%5Cn%20%20pageCount%3A%205%203%5Cn%20%20%2C%5Cn%20%20pageCount%3A%205%204%5Cn%20%20%5Cn%22%2C%22preparationScript%22%3A%22Handlebars.registerHelper('for'%2C%20(from%2C%20to%2C%20incr%2C%20block)%20%3D%3E%20%7B%5Cn%20%20%20%20let%20accum%20%3D%20''%3B%5Cn%20%20%20%20for%20(let%20i%20%3D%20from%3B%20i%20%3C%20to%3B%20i%20%2B%3D%20incr)%20%7B%5Cn%20%20%20%20%20%20%20%20accum%20%2B%3D%20block.fn(%7Bindex%3A%20i%2C%20count%3A%20to%7D)%3B%5Cn%20%20%20%20%7D%5Cn%20%20%20%20return%20accum%3B%5Cn%7D)%3B%5Cn%5CnHandlebars.registerHelper('subtract'%2C%20(a%2C%20b)%20%3D%3E%20%7B%5Cn%20%20return%20a%20-%20b%3B%5Cn%7D)%3B%5Cn%5CnHandlebars.registerHelper('eq'%2C%20(a%2C%20b%2C%20options)%20%3D%3E%20%7B%5Cn%20%20return%20a%20%3D%3D%20b%3B%5Cn%7D)%3B%22%2C%22handlebarsVersion%22%3A%224.7.7%22%7D

@pgrever
Copy link
Author

pgrever commented Feb 24, 2023

Just to clarify... This was working without error in 4.5.3. But as of 4.6.0 it does not. The release notes indicate that there were not any breaking changes between these versions (hence only a minor version roll). However, this appears to be a breaking change to me. Am I missing something, or should this still behave the same?

@pgrever
Copy link
Author

pgrever commented Feb 24, 2023

FYI, I have verified that in 4.5.3 pageCount was available with the the for helper. Is there some reason that data properties are no longer available inside the helpers? This definitely seems like a breaking change.

@jaylinski
Copy link
Member

Just to clarify... This was working without error in 4.5.3. But as of 4.6.0 it does not. The release notes indicate that there were not any breaking changes between these versions (hence only a minor version roll). However, this appears to be a breaking change to me. Am I missing something, or should this still behave the same?

There was a breaking change (for security reasons):

@pgrever
Copy link
Author

pgrever commented Feb 27, 2023

OK, thank you. Based upon terminology I did not realize that our use case was the same as the one mentioned in the release notes.

@pgrever pgrever closed this as completed Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants