public final class Es6ToEs3ClassSideInheritance extends java.lang.Object implements HotSwapCompilerPass
For example, Es6ToEs3Converter
will convert
class Foo { static f() {} } class Bar extends Foo {}to
function Foo() {} Foo.f = function() {}; function Bar() {} $jscomp.inherits(Bar, Foo);and then this class will convert that to
function Foo() {} Foo.f = function() {}; function Bar() {} $jscomp.inherits(Bar, Foo); Bar.f = Foo.f;Additionally, there are getter and setter fields which are transpiled from:
class Foo { static get prop() { return 1; } } class Bar extends Foo {}to:
var Foo = function() {}; Foo.prop; // stub declaration so that the type checker knows about prop Object.defineProperties(Foo, {prop:{get:function() { return 1; }}}); var Bar = function() {}; $jscomp.inherits(Bar, Foo);The stub declaration of Foo.prop needs to be duplicated for Bar so that the type checker knows that Bar also has this property. (ES5 clases don't have class-side inheritance).
var Bar = function() {}; Bar.prop; $jscomp.inherits(Bar, Foo);
In order to gather the type checker declarations, this pass gathers all GETPROPs on a class. In order to determine which of these are the stub declarations it filters them based on names discovered in Object.defineProperties. Unfortunately, we cannot simply gather the defined properties because they don't have the type information (JSDoc). The type information is stored on the stub declarations so we must gather both to transpile correctly.
TODO(tdeegan): In the future the type information for getter/setter properties could be stored in the defineProperies functions. It would reduce the complexity of this pass significantly.
Modifier and Type | Class and Description |
---|---|
private class |
Es6ToEs3ClassSideInheritance.FindStaticMembers |
private static class |
Es6ToEs3ClassSideInheritance.JavascriptClass |
Modifier and Type | Field and Description |
---|---|
private java.util.LinkedHashMap<java.lang.String,Es6ToEs3ClassSideInheritance.JavascriptClass> |
classByAlias |
private AbstractCompiler |
compiler |
(package private) static DiagnosticType |
DUPLICATE_CLASS |
private java.util.Set<java.lang.String> |
duplicateClassNames |
Constructor and Description |
---|
Es6ToEs3ClassSideInheritance(AbstractCompiler compiler) |
Modifier and Type | Method and Description |
---|---|
private void |
copyDeclarations(Es6ToEs3ClassSideInheritance.JavascriptClass superClass,
Es6ToEs3ClassSideInheritance.JavascriptClass subClass,
Node inheritsCall)
When static get/set properties are transpiled, in addition to the Object.defineProperties, they
are declared with stub GETPROP declarations so that the type checker understands that these
properties exist on the class.
|
private void |
copyStaticMembers(Es6ToEs3ClassSideInheritance.JavascriptClass superClass,
Es6ToEs3ClassSideInheritance.JavascriptClass subClass,
Node inheritsCall) |
void |
hotSwapScript(Node scriptRoot,
Node originalRoot)
Process the JS with root node root.
|
private boolean |
isOverriden(Es6ToEs3ClassSideInheritance.JavascriptClass subClass,
java.lang.String memberName) |
private boolean |
isReferenceToClass(NodeTraversal t,
Node n) |
void |
process(Node externs,
Node root)
Process the JS with root node root.
|
private void |
processInherits(java.util.List<Node> inheritsCalls) |
static final DiagnosticType DUPLICATE_CLASS
private final java.util.Set<java.lang.String> duplicateClassNames
private final AbstractCompiler compiler
private final java.util.LinkedHashMap<java.lang.String,Es6ToEs3ClassSideInheritance.JavascriptClass> classByAlias
public Es6ToEs3ClassSideInheritance(AbstractCompiler compiler)
public void process(Node externs, Node root)
CompilerPass
process
in interface CompilerPass
externs
- Top of external JS treeroot
- Top of JS treepublic void hotSwapScript(Node scriptRoot, Node originalRoot)
HotSwapCompilerPass
hotSwapScript
in interface HotSwapCompilerPass
scriptRoot
- Root node corresponding to the file that is modified,
should be of type Token.SCRIPT
.originalRoot
- Root node corresponding to the original version of the
file that is modified. Should be of type token.SCRIPT
.private void processInherits(java.util.List<Node> inheritsCalls)
private void copyDeclarations(Es6ToEs3ClassSideInheritance.JavascriptClass superClass, Es6ToEs3ClassSideInheritance.JavascriptClass subClass, Node inheritsCall)
private void copyStaticMembers(Es6ToEs3ClassSideInheritance.JavascriptClass superClass, Es6ToEs3ClassSideInheritance.JavascriptClass subClass, Node inheritsCall)
private boolean isOverriden(Es6ToEs3ClassSideInheritance.JavascriptClass subClass, java.lang.String memberName)
private boolean isReferenceToClass(NodeTraversal t, Node n)