google / google/closure-compiler

Cannot use destructuring in `goog.scope`

Open
#2,196 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
7.7k
Forks
1.2k
Avg merge
2d 12h
Merged PRs (30d)
6

Description

Calling `goog.module.get` in a destructuring assignment statement causes a compiler error. For example:

```js
goog.require('goog.testing.jsunit');

goog.scope(function() {
const {BASE_PATH} = goog.module.get('goog.testing.jsunit');
});
```

This code causes a `JSC_GOOG_SCOPE_NON_ALIAS_LOCAL` error (as can be seen [in the online compiler][example]):

```
JSC_GOOG_SCOPE_NON_ALIAS_LOCAL: The local variable BASE_PATH is in a goog.scope and is not an alias. at line 4 character 9
const {BASE_PATH} = goog.module.get('goog.testing.jsunit');
```

Replacing the problematic line with the following more traditional assignment works fine:

```js
const BASE_PATH = goog.module.get('goog.testing.jsunit').BASE_PATH;
```

[example]: https://closure-compiler.appspot.com/home#code%3D%252F%252F%2520%253D%253DClosureCompiler%253D%253D%250A%252F%252F%2520%2540compilation_level%2520ADVANCED_OPTIMIZATIONS%250A%252F%252F%2520%2540output_file_name%2520default.js%250A%252F%252F%2520%2540use_closure_library%2520true%250A%252F%252F%2520%253D%253D%252FClosureCompiler%253D%253D%250A%250Agoog.require('goog.testing.jsunit')%253B%250A%250Agoog.scope(function()%2520%257B%250A%2520%2520const%2520%257BBASE_PATH%257D%2520%253D%2520goog.module.get('goog.testing.jsunit')%253B%250A%2520%2520%252F%252Fconst%2520BASE_PATH%2520%253D%2520goog.module.get('goog.testing.jsunit').BASE_PATH%253B%250A%257D)%253B

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.