Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate Es7 to Es6 transpilation into its own pass.
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=163361753
- Loading branch information
Showing
9 changed files
with
168 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 2014 The Closure Compiler Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.javascript.jscomp; | ||
|
||
import com.google.javascript.rhino.IR; | ||
import com.google.javascript.rhino.Node; | ||
|
||
/** | ||
* Converts ES7 code to valid ES6 code. | ||
* | ||
* Currently this class converts ** and **= operators to calling Math.pow | ||
*/ | ||
public final class Es7ToEs6Converter implements NodeTraversal.Callback, HotSwapCompilerPass { | ||
private final AbstractCompiler compiler; | ||
|
||
public Es7ToEs6Converter(AbstractCompiler compiler) { | ||
this.compiler = compiler; | ||
} | ||
|
||
@Override | ||
public void process(Node externs, Node root) { | ||
TranspilationPasses.processTranspile(compiler, externs, this); | ||
TranspilationPasses.processTranspile(compiler, root, this); | ||
} | ||
|
||
@Override | ||
public void hotSwapScript(Node scriptRoot, Node originalRoot) { | ||
TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, this); | ||
} | ||
|
||
@Override | ||
public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void visit(NodeTraversal t, Node n, Node parent) { | ||
switch (n.getToken()) { | ||
case EXPONENT: | ||
visitExponentiationExpression(n, parent); | ||
break; | ||
case ASSIGN_EXPONENT: | ||
visitExponentiationAssignmentExpression(n, parent); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
private void visitExponentiationExpression(Node n, Node parent) { | ||
Node left = n.removeFirstChild(); | ||
Node right = n.removeFirstChild(); | ||
Node mathDotPowCall = | ||
IR.call(NodeUtil.newQName(compiler, "Math.pow"), left, right) | ||
.useSourceInfoIfMissingFromForTree(n); | ||
parent.replaceChild(n, mathDotPowCall); | ||
compiler.reportChangeToEnclosingScope(mathDotPowCall); | ||
} | ||
|
||
private void visitExponentiationAssignmentExpression(Node n, Node parent) { | ||
Node left = n.removeFirstChild(); | ||
Node right = n.removeFirstChild(); | ||
Node mathDotPowCall = IR.call(NodeUtil.newQName(compiler, "Math.pow"), left.cloneTree(), right); | ||
Node assign = IR.assign(left, mathDotPowCall).useSourceInfoIfMissingFromForTree(n); | ||
parent.replaceChild(n, assign); | ||
compiler.reportChangeToEnclosingScope(assign); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
test/com/google/javascript/jscomp/Es7ToEs6ConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2014 The Closure Compiler Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.javascript.jscomp; | ||
|
||
import com.google.javascript.jscomp.CompilerOptions.LanguageMode; | ||
|
||
/** | ||
* Test cases for ES6 transpilation. Despite the name, this isn't just testing {@link | ||
* Es6ToEs3Converter}, but also some other ES6 transpilation passes. See #getProcessor. | ||
*/ | ||
public final class Es7ToEs6ConverterTest extends TypeICompilerTestCase { | ||
|
||
public Es7ToEs6ConverterTest() { | ||
super(MINIMAL_EXTERNS); | ||
} | ||
|
||
@Override | ||
protected void setUp() throws Exception { | ||
super.setUp(); | ||
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2016); | ||
setLanguageOut(LanguageMode.ECMASCRIPT5); | ||
enableRunTypeCheckAfterProcessing(); | ||
this.mode = TypeInferenceMode.NEITHER; | ||
} | ||
|
||
@Override | ||
protected CompilerPass getProcessor(Compiler compiler) { | ||
return new Es7ToEs6Converter(compiler); | ||
} | ||
|
||
@Override | ||
protected int getNumRepetitions() { | ||
return 1; | ||
} | ||
|
||
public void testExponentiationOperator() { | ||
test("2 ** 2;", "Math.pow(2,2)"); | ||
} | ||
|
||
public void testExponentiationAssignmentOperator() { | ||
test("x **= 2;", "x=Math.pow(x,2)"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters