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

Commit 8a7c2fa

Browse files
committed
Alias colliding names in Closure Library like goog.ui.Component
1 parent 589c0b1 commit 8a7c2fa

File tree

4 files changed

+102
-7
lines changed

4 files changed

+102
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,37 @@ public static void main(String[] args) {
241241

242242
/**
243243
* If one file defines a name and another uses it as a namespace, we have the
244-
* Constants.COLLDING_PROVIDE_ALIAS_POSTFIX workaround. In partial mode, Clutz can't see all
244+
* Constants.COLLIDING_PROVIDE_ALIAS_POSTFIX workaround. In partial mode, Clutz can't see all
245245
* definitions of a name, so the list of names that require aliases must be passed as an input.
246+
* Also Closure Library uses some names as both classes and namespases. For example,
247+
* `goog.ui.Component` is not only a class but also a namespace in `goog.ui.Component.EventType`.
246248
*/
247-
private Set<String> collidingProvides = new LinkedHashSet<>();
249+
private Set<String> collidingProvides =
250+
Sets.newHashSet(
251+
"goog.ui.AdvancedTooltip",
252+
"goog.ui.AnimatedZippy",
253+
"goog.ui.Checkbox",
254+
"goog.ui.ColorPicker",
255+
"goog.ui.Component",
256+
"goog.ui.Container",
257+
"goog.ui.Control",
258+
"goog.ui.Dialog",
259+
"goog.ui.FilteredMenu",
260+
"goog.ui.HoverCard",
261+
"goog.ui.Menu",
262+
"goog.ui.MenuItem",
263+
"goog.ui.ModalPopup",
264+
"goog.ui.Ratings",
265+
"goog.ui.ScrollFloater",
266+
"goog.ui.SliderBase",
267+
"goog.ui.SplitPane",
268+
"goog.ui.TableSorter",
269+
"goog.ui.Textarea",
270+
"goog.ui.TriStateMenuItem",
271+
"goog.ui.Zippy",
272+
"goog.ui.editor.AbstractDialog",
273+
"goog.ui.editor.LinkDialog",
274+
"goog.ui.tree.BaseNode");
248275

249276
DeclarationGenerator(Options opts) {
250277
this.opts = opts;
@@ -397,7 +424,7 @@ String generateDeclarations(
397424
legacyNamespaceReexportMap =
398425
new LegacyNamespaceReexportMapBuilder()
399426
.build(compiler.getParsedInputs(), opts.depgraph.getGoogProvides());
400-
collidingProvides = opts.collidingProvides;
427+
collidingProvides.addAll(opts.collidingProvides);
401428
}
402429

403430
unknownType = compiler.getTypeRegistry().getNativeType(JSTypeNative.UNKNOWN_TYPE);
@@ -487,7 +514,7 @@ String produceDts(Depgraph depgraph) {
487514
rewrittenProvides.add(rewritenProvide);
488515
}
489516
if (needsAlias(shadowedProvides, provide, symbol)) {
490-
emitName += Constants.COLLDING_PROVIDE_ALIAS_POSTFIX;
517+
emitName += Constants.COLLIDING_PROVIDE_ALIAS_POSTFIX;
491518
}
492519
if (symbol == null) {
493520
// Sometimes goog.provide statements are used as pure markers for dependency management, or
@@ -833,7 +860,7 @@ private void processExternSymbols() {
833860
boolean isDefault = isDefaultExport(symbol);
834861
String emitName = symbol.getName();
835862
if (needsAlias(shadowedSymbols, symbol.getName(), symbol)) {
836-
emitName += Constants.COLLDING_PROVIDE_ALIAS_POSTFIX;
863+
emitName += Constants.COLLIDING_PROVIDE_ALIAS_POSTFIX;
837864
}
838865

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

14591486
private String getAbsoluteName(ObjectType objectType) {
14601487
String name = objectType.getDisplayName();
1488+
if (collidingProvides.contains(name)) {
1489+
name += Constants.COLLIDING_PROVIDE_ALIAS_POSTFIX;
1490+
}
14611491
// Names that do not have a namespace '.' are either platform names in the top level
14621492
// namespace like `Object` or `Element`, or they are unqualified `goog.provide`s, e.g.
14631493
// `goog.provide('Toplevel')`. In both cases they will be found with the naked name.
@@ -1486,7 +1516,7 @@ private void walk(TypedVar symbol, String emitName) {
14861516
// Since closure inlines all aliases before this step, check against
14871517
// the type name.
14881518
if (!isAliasedClassOrInterface(symbol, ftype)) {
1489-
visitClassOrInterface(getUnqualifiedName(symbol), ftype);
1519+
visitClassOrInterface(getUnqualifiedName(emitName), ftype);
14901520
} else {
14911521
if (KNOWN_CLASS_ALIASES.containsKey(symbol.getName())) {
14921522
visitKnownTypeValueAlias(
Lines changed: 36 additions & 0 deletions
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+
}
Lines changed: 29 additions & 0 deletions
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)