google / google/closure-compiler

"google" extern symbol is magic

Open
#2,449 5 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

[I'm using the closure compiler with the ivory tower bazel build rules. I'm not sure if this a compiler or build rule issue, but filing here at a guess.]

The string "google" seems to be magic when trying to define an extern. I assume this is because it's declared elsewhere, but I can't come up with a workaround. I'm trying to throw together usable exports for google charts, which have symbols in this space.

**externs.js**:
```js
/**
* @externs
*/

/**
* @type {Object}
*/
var foo = {};

/**
* @type {number}
*/
foo.bar;

/**
* @type {Object}
*/
var google = {};

/**
* @type {number}
*/
google.bar;
```

**test.js**:
```js
/**
* @public
*/
function test() {
console.log(foo.bar);
console.log(google.bar);
}
```

**Compiler output**:
```
test.js:6: ERROR - could not determine the type of this expression
console.log(google.bar);
^
ProTip: "JSC_UNKNOWN_EXPR_TYPE" or "reportUnknownTypes" can be added to the `suppress` attribute of:
//experimental/users/flamingcow/externtest:test
Alternatively /** @suppress {reportUnknownTypes} */ can be added to the source file.
```
(no mention of foo.bar)

I would expect errors for either both foo.bar and google.bar, or neither.

**Compiler flag file created by build rules**:
```
JsCompiler
--js_output_file
bazel-out/local_linux-opt/bin/experimental/users/flamingcow/externtest/test_bin.js
--create_source_map
bazel-out/local_linux-opt/bin/experimental/users/flamingcow/externtest/test_bin.js.map
--output_errors
bazel-out/local_linux-opt/bin/experimental/users/flamingcow/externtest/test_bin-stderr.txt
--language_in
ECMASCRIPT6_STRICT
--language_out
ECMASCRIPT5
--compilation_level
ADVANCED
--dependency_mode
LOOSE
--warning_level
VERBOSE
--generate_exports
--process_closure_primitives
--define=goog.json.USE_NATIVE_JSON
--define=goog.soy.REQUIRE_STRICT_AUTOESCAPE
--hide_warnings_for=closure/goog/base.js
--define=goog.DEBUG=false
--info
bazel-out/local_linux-opt/bin/experimental/users/flamingcow/externtest/test.pbtxt
--js_module_root
bazel-out/local_linux-opt/bin
--source_map_location_mapping
bazel-out/local_linux-opt/bin/experimental/users/flamingcow/externtest/test_bin.js|test_bin.js
--source_map_include_sources_content
--source_map_location_mapping
[synthetic:| [synthetic:
--source_map_location_mapping
bazel-out/local_linux-opt/bin/|/
--source_map_location_mapping
|/
--use_types_for_optimization
external/com_google_javascript_closure_library/closure/goog/base.js
external/com_google_javascript_closure_library/closure/goog/deps.js
experimental/users/flamingcow/externtest/externs.js
experimental/users/flamingcow/externtest/test.js
```

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.