Unclosed mega literal

When do I get this error?

The "Unclosed mega literal" error is thrown when JSLint encounters an unclosed template string literal. JSHint raises the "Unclosed template literal" error in the same situation. Note that because template string literals are an ES2015 (ES6) feature this error should only appear when linting ES2015 code with the appropriate option set in the linter.

In the following example we attempt to assign an unclosed template string literal to a:

/*jslint es6: true */
let x = `unclosed;

Why do I get this error?

This error is raised to highlight a fatal JavaScript syntax error. Your code will not run unless you fix this issue. The ECMAScript grammar states that any template literal must be closed by the backtick character (ES2015 §11.8.68):

Template ::
    NoSubstitutionTemplate
    TemplateHead

NoSubstitutionTemplate ::
    ` TemplateCharactersopt `

The grammar for NoSubstitutionTemplate is straightforward and shows the necessary backticks. The second production is far more complicated and beyond the scope of this article but does also require an opening and closing backtick.

To fix the error, simply close any unclosed template strings:

/*jslint es6: true */
let x = `unclosed`;

In JSHint 1.0.0 and above you have the ability to ignore any warning with a special option syntax. Since this message relates to a fatal syntax error you cannot disable it.


About the author

James Allardice

This article was written by James Allardice, Software engineer at Tesco and orangejellyfish in London. Passionate about React, Node and writing clean and maintainable JavaScript. Uses linters (currently ESLint) every day to help achieve this.