google / google/closure-compiler

Local functions are wrongly made global in Strict mode

Open
#3,218 6 comments 0 reactions 0 assignees View on GitHub
internal-issue-created triage-done
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

When strict mode is enabled, the following code triggers an error for the second call to `func1`:
```
'use strict';
{
function func1(arg){ //func1 is local to the scope, not global
alert(arg) ;
}
func1('Hi') ;
}

func1('Hi') ; // <-- Error: func1 does not exist globally
```

When we compile that code:
```
java -jar closure-compiler-v20190121.jar --language_out NO_TRANSPILE -O SIMPLE --js_output_file Output.js Input.js
```

We get:
```
'use strict';
var func1 = function(a) {
alert(a);
};
func1("Hi");
func1("Hi");
```
i.e. `func1` was converted to a global function, which makes the code behave different than the original.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.