question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ERROR - This language feature is only supported for ECMASCRIPT6 mode or better: block-scoped function declaration.

See original GitHub issue

On

$ npx google-closure-compiler --version
Closure Compiler (http://github.com/google/closure-compiler)
Version: v20181210
Built on: 2018-12-12 22:34

compiling

if (performance.now() >= 0) {
  function foo() {
    console.log(performance.now());
  }
  foo();
}

gives

$ npx google-closure-compiler --js=src.js --js_output_file=out.js --language_in ECMASCRIPT5
src.js:2: ERROR - This language feature is only supported for ECMASCRIPT6 mode or better: block-scoped function declaration.
	function foo() {
	^

1 error(s), 0 warning(s)

I don’t think this error is correct. Functions defined inside a block do work in non-ES6 browsers, and the code used to compile properly in older version of Closure compiler when using --language_in ECMASCRIPT5 flag. This issue started to appear after I updated Closure to a newer version. My older Closure version was

Closure Compiler (http://github.com/google/closure-compiler)
Version: v20171023
Built on: 2017-10-26 19:00

I do not expect the function foo in above example to have block scope visibility, but due to reasons X beyond my control, I am compiling code that has instances of functions defined inside blocks - the code as written does not care about scope (in the instances that occur, the functions are used locally at block scope, and don’t mind if the visibility of the functions extends to function scope)

Did Closure drop support for defining functions with function scope in ES5? Or is there a flag to allow enabling this? I’d like to update to a newer Closure compiler version, but this causing a bit of headache.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
jujcommented, Jan 12, 2019

There’s not a lot of reason to supply --language_in unless you’re trying to enforce that your input definitely won’t use any newer features.

This is the exact reason why Emscripten is using --language_in ECMASCRIPT5 - it wants to enforce that user JS code does not use new ES6 features when many developers want to build projects that have backwards compatibility far back to old browsers.

The issue isn’t older browsers, it’s newer ones and differences in behavior between strict and sloppy mode.

I do appreciate and acknowledge the semantical difference with the two scoping rules, but the issue here is not old browsers, or the difference in behavior, but the fact that Closure no longer is able to process valid ES5 code whereas it used to before. Surely Closure does model (used to model?) sloppy mode scope for function definitions e.g. when --language_in=ES5 --language_out=ES5 is used? It feels like a regression that it no longer does.

Could Closure treat the scope according to the --language_in= directive that is passed? I.e. --language_in=ES6 would treat functions with block scope, and --language_in=ES5 (or whichever ES spec first had the semantical change) would treat them with function scope?

2reactions
brad4dcommented, Jan 11, 2019

@shicks may have more context

The change to consider block scoped function declarations was made about a year ago. The issue isn’t older browsers, it’s newer ones and differences in behavior between strict and sloppy mode. For example:

(function() {
   var x = [];
   f();
   {
     function f() { x.push(2) };
     f();
   }
   function f() { x.push(1) };
   f();
   // logs [1, 2, 1] in strict mode
   // logs [1, 2, 2] in sloppy mode due to hoisting of outer definition
   console.log(x);
})();

More details available here. https://www.ecma-international.org/ecma-262/6.0/#sec-block-level-function-declarations-web-legacy-compatibility-semantics

We basically decided that the best way to get consistent behavior and avoid nasty surprises for the user was to insist on treating block-scoped function definitions as an ES6 feature.

Since you say you cannot modify the code you’re working with, the best option is probably to just set --language_in=ES6 --language_out=ES5.

Read more comments on GitHub >

github_iconTop Results From Across the Web

this language feature is only supported for ECMASCRIPT6 ...
Error at line 6, character 282 : this language feature is only supported for ECMASCRIPT6 mode or better: block-scoped function declaration. Use ...
Read more >
ECMASCRIPT6 error while building image on adobe as...
js:2394 : ERROR - This language feature is only supported for ECMASCRIPT6 mode or better: block-scoped function declaration. function h(a, b, c) {....
Read more >
3. One JavaScript: avoiding versioning in ECMAScript 6
ECMAScript 5 strict mode forbids function declarations in blocks. The specification allowed them in sloppy mode, but didn't specify how they should behave....
Read more >
Solve for AEM client lib minification for ES6 compiler using GCC
... ERROR - this language feature is only supported for ECMASCRIPT6 mode or better: let declaration. Use --language_in=ECMASCRIPT6 or ECMASCRIPT6_STRICT or ...
Read more >
tag manager scripts
error - this language feature is only supported for ecmascript6 mode or better ... or better: block-scoped function declaration. when using CS with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found