public final class Es6ToEs3Converter extends java.lang.Object implements NodeTraversal.Callback, HotSwapCompilerPass
In most cases, the output is valid as ES3 (hence the class name) but in some cases, if the output language is set to ES5, we rely on ES5 features such as getters, setters, and Object.defineProperties.
Modifier and Type | Class and Description |
---|---|
private class |
Es6ToEs3Converter.CheckClassAssignments |
(package private) static class |
Es6ToEs3Converter.ClassDeclarationMetadata
Represents static metadata on a class declaration expression - i.e.
|
Modifier and Type | Field and Description |
---|---|
(package private) static DiagnosticType |
BAD_REST_PARAMETER_ANNOTATION |
(package private) static DiagnosticType |
CANNOT_CONVERT |
(package private) static DiagnosticType |
CANNOT_CONVERT_YET |
(package private) static DiagnosticType |
CLASS_REASSIGNMENT |
private AbstractCompiler |
compiler |
(package private) static DiagnosticType |
CONFLICTING_GETTER_SETTER_TYPE |
(package private) static DiagnosticType |
DYNAMIC_EXTENDS_TYPE |
private static java.lang.String |
FRESH_COMP_PROP_VAR |
private static java.lang.String |
FRESH_SPREAD_VAR |
(package private) static java.lang.String |
INHERITS |
private static java.lang.String |
ITER_BASE |
private static java.lang.String |
ITER_RESULT |
private static java.lang.String |
REST_INDEX |
private static java.lang.String |
REST_PARAMS |
Constructor and Description |
---|
Es6ToEs3Converter(AbstractCompiler compiler) |
Modifier and Type | Method and Description |
---|---|
private void |
addToDefinePropertiesObject(Es6ToEs3Converter.ClassDeclarationMetadata metadata,
Node member) |
private void |
addTypeDeclarations(Es6ToEs3Converter.ClassDeclarationMetadata metadata,
Node insertionPoint)
Add declarations for properties that were defined with a getter and/or setter,
so that the typechecker knows those properties exist on the class.
|
private static Node |
arrayFromIterable(AbstractCompiler compiler,
Node iterable)
Returns a call to $jscomp.arrayFromIterable with
iterable as its argument. |
private static Node |
callEs6RuntimeFunction(AbstractCompiler compiler,
Node iterable,
java.lang.String function) |
private void |
cannotConvert(Node n,
java.lang.String message) |
private void |
cannotConvertYet(Node n,
java.lang.String feature)
Warns the user that the given ES6 feature cannot be converted to ES3
because the transpilation is not yet implemented.
|
private void |
checkClassReassignment(Node clazz) |
private static Node |
getQualifiedMemberAccess(Node member,
Node staticAccess,
Node instanceAccess)
Constructs a Node that represents an access to the given class member, qualified by either the
static or the instance access context, depending on whether the member is static.
|
private JSTypeExpression |
getTypeFromGetterOrSetter(Node node) |
void |
hotSwapScript(Node scriptRoot,
Node originalRoot)
Process the JS with root node root.
|
private void |
initSymbolBefore(Node n)
Inserts a call to $jscomp.initSymbol() before
n . |
private boolean |
isGlobalSymbol(NodeTraversal t,
Node n) |
(package private) static Node |
makeIterator(AbstractCompiler compiler,
Node iterable)
Returns a call to
$jscomp.makeIterator with iterable as its argument. |
void |
process(Node externs,
Node root)
Process the JS with root node root.
|
boolean |
shouldTraverse(NodeTraversal t,
Node n,
Node parent)
Some nodes must be visited pre-order in order to rewrite the
references to
this correctly. |
void |
visit(NodeTraversal t,
Node n,
Node parent)
Visits a node in postorder (after its children have been visited).
|
private void |
visitArrayLitOrCallWithSpread(NodeTraversal t,
Node node,
Node parent)
Processes array literals or calls containing spreads.
|
private void |
visitClass(Node classNode,
Node parent)
Classes are processed in 3 phases:
The class name is extracted.
|
private void |
visitClassMember(Node member,
Es6ToEs3Converter.ClassDeclarationMetadata metadata)
Handles transpilation of a standard class member function.
|
private void |
visitComputedPropInClass(Node member,
Es6ToEs3Converter.ClassDeclarationMetadata metadata) |
private void |
visitForOf(NodeTraversal t,
Node node,
Node parent) |
private void |
visitGetprop(NodeTraversal t,
Node n) |
private void |
visitMemberFunctionDefInObjectLit(Node n,
Node parent)
Converts a member definition in an object literal to an ES3 key/value pair.
|
private void |
visitObject(Node obj) |
private void |
visitObjectWithComputedProperty(Node obj) |
private void |
visitRestParam(Node restParam,
Node paramList)
Processes a rest parameter
|
private void |
visitStringKey(Node n)
Converts extended object literal {a} to {a:a}.
|
private final AbstractCompiler compiler
static final DiagnosticType CANNOT_CONVERT
static final DiagnosticType CANNOT_CONVERT_YET
static final DiagnosticType DYNAMIC_EXTENDS_TYPE
static final DiagnosticType CLASS_REASSIGNMENT
static final DiagnosticType CONFLICTING_GETTER_SETTER_TYPE
static final DiagnosticType BAD_REST_PARAMETER_ANNOTATION
private static final java.lang.String REST_INDEX
private static final java.lang.String REST_PARAMS
private static final java.lang.String FRESH_SPREAD_VAR
private static final java.lang.String FRESH_COMP_PROP_VAR
private static final java.lang.String ITER_BASE
private static final java.lang.String ITER_RESULT
static final java.lang.String INHERITS
public Es6ToEs3Converter(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
.public boolean shouldTraverse(NodeTraversal t, Node n, Node parent)
this
correctly.
Everything else is translated post-order in visit(com.google.javascript.jscomp.NodeTraversal, com.google.javascript.rhino.Node, com.google.javascript.rhino.Node)
.shouldTraverse
in interface NodeTraversal.Callback
public void visit(NodeTraversal t, Node n, Node parent)
NodeTraversal.Callback
Visits a node in postorder (after its children have been visited).
A node is visited only if all its parents should be traversed
(NodeTraversal.Callback.shouldTraverse(NodeTraversal, Node, Node)
).
Implementations can have side effects (e.g. modifying the parse tree).
visit
in interface NodeTraversal.Callback
private boolean isGlobalSymbol(NodeTraversal t, Node n)
n
is a reference to the global "Symbol" function.private void initSymbolBefore(Node n)
n
.private void visitGetprop(NodeTraversal t, Node n)
private void visitMemberFunctionDefInObjectLit(Node n, Node parent)
visitClass(com.google.javascript.rhino.Node, com.google.javascript.rhino.Node)
.private void visitStringKey(Node n)
private void visitForOf(NodeTraversal t, Node node, Node parent)
private void checkClassReassignment(Node clazz)
private void visitRestParam(Node restParam, Node paramList)
private void visitArrayLitOrCallWithSpread(NodeTraversal t, Node node, Node parent)
private void visitObject(Node obj)
private void visitObjectWithComputedProperty(Node obj)
private void visitClass(Node classNode, Node parent)
private JSTypeExpression getTypeFromGetterOrSetter(Node node)
node
- A getter or setter node.private void addToDefinePropertiesObject(Es6ToEs3Converter.ClassDeclarationMetadata metadata, Node member)
member
- A getter or setter, or a computed property that is a getter/setter.private void visitComputedPropInClass(Node member, Es6ToEs3Converter.ClassDeclarationMetadata metadata)
private void visitClassMember(Node member, Es6ToEs3Converter.ClassDeclarationMetadata metadata)
private void addTypeDeclarations(Es6ToEs3Converter.ClassDeclarationMetadata metadata, Node insertionPoint)
private static Node getQualifiedMemberAccess(Node member, Node staticAccess, Node instanceAccess)
WARNING: member
may be modified/destroyed by this method, do not use it
afterwards.
private void cannotConvert(Node n, java.lang.String message)
private void cannotConvertYet(Node n, java.lang.String feature)
feature
" comment.static Node makeIterator(AbstractCompiler compiler, Node iterable)
$jscomp.makeIterator
with iterable
as its argument.private static Node arrayFromIterable(AbstractCompiler compiler, Node iterable)
iterable
as its argument.private static Node callEs6RuntimeFunction(AbstractCompiler compiler, Node iterable, java.lang.String function)