google / google/closure-compiler

goog.module & declareLegacyNamespace issue on nested namespace

Open
#2,665 3 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

When we use `goog.module.declareLegacyNamespace` in a module which identifier is a subnamespace of a second module identifier that depends on it, it creates the subnamespace object on upper module before it is created.

File 1 in src/app/user.js
```js
goog.module('app.user');
goog.module.declareLegacyNamespace();

goog.require('app.user.a');

var _app_user_ = {...};
...

exports = _app_user_;
```

File 2 in a subfolder (src/app/user/a.js) , where its module identifier is a sub namespace of file 1
```js
goog.module('app.user.a');
goog.module.declareLegacyNamespace();

var _app_user_a_ = {...};
exports = _app_user_a_;
```

After compilation, file 2 is dependency of file 1 so added first in the bundle, but when processing the legacy namespace, it adds it to a non yet created object:

Here the generated code:
```js
var module$contents$app$user$a__app_user_a_ = {...};
module$contents$app$user__app_user_.a = module$contents$app$user$a__app_user_a_;
var module$contents$app$user__app_user_ = {};
....
app.user = module$contents$app$user__app_user_;
```
`module$contents$app$user__app_user_` object on line 2 is not yet declared.

I would expect line 2 comes after line 3 or is replaced with
```js
app.user.a = module$contents$app$user$a$a__app_user_a_;

```

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.