Skip to content

Literals such as "let", "const" not supported anymore in registerHelpers() API. #657

@swjain

Description

@swjain

To keep client and server side behavior consistent, we have this nice API to register helpers written in javascript, which was working absolutely fine until at least version 4.0.6.

Example helper.js file being loaded through registerHelpers API:

helpers.and = function () {
    const len = arguments.length - 1;
    const options = arguments[len];
    let val = true;
    for (var i = 0; i < len; i++) {
        if (!arguments[i]) {
            val = false;
            break;
        }
    }
    return util.value(val, this, options);
};

Handlebars.registerHelper('and', helpers.and);

Getting this error on v4.1.0

Caused by: jdk.nashorn.internal.runtime.ParserException: <eval>:78:4 Expected an operand but found const
    const len = arguments.length - 1;
    ^
	at jdk.nashorn.internal.parser.AbstractParser.error(AbstractParser.java:294)
	at jdk.nashorn.internal.parser.AbstractParser.error(AbstractParser.java:279)
	at jdk.nashorn.internal.parser.Parser.unaryExpression(Parser.java:3182)
	at jdk.nashorn.internal.parser.Parser.expression(Parser.java:3282)
	at jdk.nashorn.internal.parser.Parser.expressionStatement(Parser.java:1150)
	at jdk.nashorn.internal.parser.Parser.statement(Parser.java:967)
	at jdk.nashorn.internal.parser.Parser.sourceElements(Parser.java:773)
	at jdk.nashorn.internal.parser.Parser.functionBody(Parser.java:2901)
	at jdk.nashorn.internal.parser.Parser.functionExpression(Parser.java:2663)
	at jdk.nashorn.internal.parser.Parser.memberExpression(Parser.java:2506)
	at jdk.nashorn.internal.parser.Parser.leftHandSideExpression(Parser.java:2372)
	at jdk.nashorn.internal.parser.Parser.unaryExpression(Parser.java:3147)
	at jdk.nashorn.internal.parser.Parser.expression(Parser.java:3325)
	at jdk.nashorn.internal.parser.Parser.expression(Parser.java:3282)
	at jdk.nashorn.internal.parser.Parser.expressionStatement(Parser.java:1150)
	at jdk.nashorn.internal.parser.Parser.statement(Parser.java:967)
	at jdk.nashorn.internal.parser.Parser.sourceElements(Parser.java:773)
	at jdk.nashorn.internal.parser.Parser.program(Parser.java:709)
	at jdk.nashorn.internal.parser.Parser.parse(Parser.java:283)
	at jdk.nashorn.internal.parser.Parser.parse(Parser.java:249)
	at jdk.nashorn.internal.runtime.Context.compile(Context.java:1284)
	at jdk.nashorn.internal.runtime.Context.compileScript(Context.java:1251)
	at jdk.nashorn.internal.runtime.Context.compileScript(Context.java:627)
	at jdk.nashorn.api.scripting.NashornScriptEngine.compileImpl(NashornScriptEngine.java:535)
	... 26 more

Note: Though consuming scripts from lib area is a good practice but node 10 onwards the transpiled scripts themselves contains let or const literals and those aren't translated to var.
Hence, handlebars.java should continue allowing let and const while reading javacsipt source for helpers.

Or alternatively, we can always rewrite helpers sources to use var literal but let or const were already supported in earlier versions so why not in latest?
Also, the error message is not clear as to understand "what to fix?"

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions