Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit ba4f88e

Browse files
committed
Alias colliding names in Closure Library like goog.ui.Component
1 parent 4e16e11 commit ba4f88e

File tree

4 files changed

+103
-7
lines changed

4 files changed

+103
-7
lines changed

src/main/java/com/google/javascript/clutz/Constants.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ public interface Constants {
1414
* The alias does not affect the external module declaration, thus the user can still import the
1515
* symbol using unaliased module name (i.e. import bar from 'goog:foo.bar').
1616
*/
17-
static final String COLLDING_PROVIDE_ALIAS_POSTFIX = "__clutz_alias";
17+
static final String COLLIDING_PROVIDE_ALIAS_POSTFIX = "__clutz_alias";
1818
}

src/main/java/com/google/javascript/clutz/DeclarationGenerator.java

+37-6
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,38 @@ public static void main(String[] args) {
260260

261261
/**
262262
* If one file defines a name and another uses it as a namespace, we have the
263-
* Constants.COLLDING_PROVIDE_ALIAS_POSTFIX workaround. In partial mode, Clutz can't see all
263+
* Constants.COLLIDING_PROVIDE_ALIAS_POSTFIX workaround. In partial mode, Clutz can't see all
264264
* definitions of a name, so the list of names that require aliases must be passed as an input.
265+
* Also Closure Library uses some names as both classes and namespases. For example,
266+
* `goog.ui.Component` is not only a class but also a namespace in `goog.ui.Component.EventType`.
267+
* See https://github.com/google/closure-library/blob/master/closure/goog/ui/component.js
265268
*/
266-
private Set<String> collidingProvides = new LinkedHashSet<>();
269+
private Set<String> collidingProvides =
270+
Sets.newHashSet(
271+
"goog.ui.AdvancedTooltip",
272+
"goog.ui.AnimatedZippy",
273+
"goog.ui.Checkbox",
274+
"goog.ui.ColorPicker",
275+
"goog.ui.Component",
276+
"goog.ui.Container",
277+
"goog.ui.Control",
278+
"goog.ui.Dialog",
279+
"goog.ui.FilteredMenu",
280+
"goog.ui.HoverCard",
281+
"goog.ui.Menu",
282+
"goog.ui.MenuItem",
283+
"goog.ui.ModalPopup",
284+
"goog.ui.Ratings",
285+
"goog.ui.ScrollFloater",
286+
"goog.ui.SliderBase",
287+
"goog.ui.SplitPane",
288+
"goog.ui.TableSorter",
289+
"goog.ui.Textarea",
290+
"goog.ui.TriStateMenuItem",
291+
"goog.ui.Zippy",
292+
"goog.ui.editor.AbstractDialog",
293+
"goog.ui.editor.LinkDialog",
294+
"goog.ui.tree.BaseNode");
267295

268296
DeclarationGenerator(Options opts) {
269297
this.opts = opts;
@@ -416,7 +444,7 @@ String generateDeclarations(
416444
legacyNamespaceReexportMap =
417445
new LegacyNamespaceReexportMapBuilder()
418446
.build(compiler.getParsedInputs(), opts.depgraph.getGoogProvides());
419-
collidingProvides = opts.collidingProvides;
447+
collidingProvides.addAll(opts.collidingProvides);
420448
}
421449

422450
unknownType = compiler.getTypeRegistry().getNativeType(JSTypeNative.UNKNOWN_TYPE);
@@ -512,7 +540,7 @@ String produceDts(Depgraph depgraph) {
512540
rewrittenProvides.add(rewritenProvide);
513541
}
514542
if (needsAlias(shadowedProvides, provide, symbol)) {
515-
emitName += Constants.COLLDING_PROVIDE_ALIAS_POSTFIX;
543+
emitName += Constants.COLLIDING_PROVIDE_ALIAS_POSTFIX;
516544
}
517545
if (symbol == null) {
518546
// Sometimes goog.provide statements are used as pure markers for dependency management, or
@@ -858,7 +886,7 @@ private void processExternSymbols() {
858886
boolean isDefault = isDefaultExport(symbol);
859887
String emitName = symbol.getName();
860888
if (needsAlias(shadowedSymbols, symbol.getName(), symbol)) {
861-
emitName += Constants.COLLDING_PROVIDE_ALIAS_POSTFIX;
889+
emitName += Constants.COLLIDING_PROVIDE_ALIAS_POSTFIX;
862890
}
863891

864892
// There is nothing to emit for a namespace, because all its symbols will be visited later,
@@ -1485,6 +1513,9 @@ private TreeWalker(
14851513

14861514
private String getAbsoluteName(ObjectType objectType) {
14871515
String name = objectType.getDisplayName();
1516+
if (collidingProvides.contains(name)) {
1517+
name += Constants.COLLIDING_PROVIDE_ALIAS_POSTFIX;
1518+
}
14881519
// Names that do not have a namespace '.' are either platform names in the top level
14891520
// namespace like `Object` or `Element`, or they are unqualified `goog.provide`s, e.g.
14901521
// `goog.provide('Toplevel')`. In both cases they will be found with the naked name.
@@ -1513,7 +1544,7 @@ private void walk(TypedVar symbol, String emitName) {
15131544
// Since closure inlines all aliases before this step, check against
15141545
// the type name.
15151546
if (!isAliasedClassOrInterface(symbol, ftype)) {
1516-
visitClassOrInterface(getUnqualifiedName(symbol), ftype);
1547+
visitClassOrInterface(getUnqualifiedName(emitName), ftype);
15171548
} else {
15181549
if (KNOWN_CLASS_ALIASES.containsKey(symbol.getName())) {
15191550
visitKnownTypeValueAlias(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
declare namespace ಠ_ಠ.clutz.goog.ui {
2+
class Component__clutz_alias {
3+
private noStructuralTyping_goog_ui_Component : any;
4+
}
5+
}
6+
declare module 'goog:goog.ui.Component' {
7+
import Component = ಠ_ಠ.clutz.goog.ui.Component__clutz_alias;
8+
export default Component;
9+
}
10+
declare namespace ಠ_ಠ.clutz.goog.ui.Component {
11+
enum EventType {
12+
ACTION = 'action' ,
13+
}
14+
}
15+
declare module 'goog:goog.ui.Component.EventType' {
16+
import EventType = ಠ_ಠ.clutz.goog.ui.Component.EventType;
17+
export default EventType;
18+
}
19+
declare namespace ಠ_ಠ.clutz.goog.ui.tree {
20+
class BaseNode__clutz_alias extends ಠ_ಠ.clutz.goog.ui.Component__clutz_alias {
21+
private noStructuralTyping_goog_ui_tree_BaseNode : any;
22+
}
23+
}
24+
declare module 'goog:goog.ui.tree.BaseNode' {
25+
import BaseNode = ಠ_ಠ.clutz.goog.ui.tree.BaseNode__clutz_alias;
26+
export default BaseNode;
27+
}
28+
declare namespace ಠ_ಠ.clutz.goog.ui.tree.BaseNode {
29+
enum EventType {
30+
BEFORE_COLLAPSE = 'beforecollapse' ,
31+
}
32+
}
33+
declare module 'goog:goog.ui.tree.BaseNode.EventType' {
34+
import EventType = ಠ_ಠ.clutz.goog.ui.tree.BaseNode.EventType;
35+
export default EventType;
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
goog.provide('goog.ui.Component');
2+
goog.provide('goog.ui.Component.EventType');
3+
goog.provide('goog.ui.tree.BaseNode');
4+
goog.provide('goog.ui.tree.BaseNode.EventType');
5+
6+
/**
7+
* @constructor
8+
*/
9+
goog.ui.Component = function() {};
10+
11+
/**
12+
* @enum {string}
13+
*/
14+
goog.ui.Component.EventType = {
15+
ACTION: 'action'
16+
};
17+
18+
/**
19+
* @constructor
20+
* @extends {goog.ui.Component}
21+
*/
22+
goog.ui.tree.BaseNode = function() {};
23+
24+
/**
25+
* @enum {string}
26+
*/
27+
goog.ui.tree.BaseNode.EventType = {
28+
BEFORE_COLLAPSE: 'beforecollapse'
29+
};

0 commit comments

Comments
 (0)