Skip to content

Commit

Permalink
Renames Es7ToEs6Converter to Es7RewriteExponentialOperator and si…
Browse files Browse the repository at this point in the history
…mplifies its implementation to support adding type to the synthesized AST.

This rename was done because the exponential operator is the only thing this class ever handled, and we don't actually want it to do anything else.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=198788507
  • Loading branch information
nreid260 authored and lauraharker committed Jun 1, 2018
1 parent 4846c92 commit fa80945
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 120 deletions.
127 changes: 127 additions & 0 deletions src/com/google/javascript/jscomp/Es7RewriteExponentialOperator.java
@@ -0,0 +1,127 @@
/*
* 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.parsing.parser.FeatureSet;
import com.google.javascript.jscomp.parsing.parser.FeatureSet.Feature;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.jstype.JSType;
import com.google.javascript.rhino.jstype.JSTypeNative;
import com.google.javascript.rhino.jstype.JSTypeRegistry;

/** Replaces the ES7 `**` and `**=` operators to calls to `Math.pow`. */
public final class Es7RewriteExponentialOperator
implements NodeTraversal.Callback, HotSwapCompilerPass {

private static final FeatureSet transpiledFeatures =
FeatureSet.BARE_MINIMUM.with(Feature.EXPONENT_OP);

private final AbstractCompiler compiler;
private final Node mathPowCall; // This node should only ever be cloned, not directly inserted.

private final JSType numberType;
private final JSType stringType;
private final JSType mathType;
private final JSType mathPowType;

public Es7RewriteExponentialOperator(AbstractCompiler compiler) {
this.compiler = compiler;

if (compiler.hasTypeCheckingRun()) {
JSTypeRegistry registry = compiler.getTypeRegistry();
this.numberType = registry.getNativeType(JSTypeNative.NUMBER_TYPE);
this.stringType = registry.getNativeType(JSTypeNative.STRING_TYPE);
// TODO(nickreid): Get the actual type of the `Math` object here in case optimizations care.
this.mathType = registry.getNativeType(JSTypeNative.UNKNOWN_TYPE);
this.mathPowType = registry.createFunctionType(numberType, numberType, numberType);
} else {
this.numberType = null;
this.stringType = null;
this.mathType = null;
this.mathPowType = null;
}

this.mathPowCall = createMathPowCall();
}

@Override
public void process(Node externs, Node root) {
TranspilationPasses.processTranspile(compiler, externs, transpiledFeatures, this);
TranspilationPasses.processTranspile(compiler, root, transpiledFeatures, this);
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

@Override
public void hotSwapScript(Node scriptRoot, Node originalRoot) {
TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, transpiledFeatures, this);
TranspilationPasses.markFeaturesAsTranspiledAway(compiler, transpiledFeatures);
}

@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:
visitExponentiationOperator(n);
break;
case ASSIGN_EXPONENT:
visitExponentiationAssignmentOperator(n);
break;
default:
break;
}
}

private void visitExponentiationOperator(Node operator) {
Node callClone = mathPowCall.cloneTree();
callClone.addChildToBack(operator.removeFirstChild()); // Base argument.
callClone.addChildToBack(operator.removeFirstChild()); // Exponent argument.

callClone.useSourceInfoIfMissingFromForTree(operator);
operator.replaceWith(callClone);

compiler.reportChangeToEnclosingScope(callClone);
}

private void visitExponentiationAssignmentOperator(Node operator) {
Node lValue = operator.removeFirstChild();

Node callClone = mathPowCall.cloneTree();
callClone.addChildToBack(lValue.cloneTree()); // Base argument.
callClone.addChildToBack(operator.removeFirstChild()); // Exponent argument.

Node assignment = IR.assign(lValue, callClone).setJSType(numberType);

assignment.useSourceInfoIfMissingFromForTree(operator);
operator.replaceWith(assignment);

compiler.reportChangeToEnclosingScope(assignment);
}

private Node createMathPowCall() {
return IR.call(
IR.getprop(
IR.name("Math").setJSType(mathType), // Force wrapping.
IR.string("pow").setJSType(stringType))
.setJSType(mathPowType))
.setJSType(numberType);
}
}
116 changes: 0 additions & 116 deletions src/com/google/javascript/jscomp/Es7ToEs6Converter.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/com/google/javascript/jscomp/TranspilationPasses.java
Expand Up @@ -156,7 +156,7 @@ protected FeatureSet featureSet() {
new HotSwapPassFactory("convertEs7ToEs6") { new HotSwapPassFactory("convertEs7ToEs6") {
@Override @Override
protected HotSwapCompilerPass create(final AbstractCompiler compiler) { protected HotSwapCompilerPass create(final AbstractCompiler compiler) {
return new Es7ToEs6Converter(compiler); return new Es7RewriteExponentialOperator(compiler);
} }


@Override @Override
Expand Down
Expand Up @@ -21,9 +21,9 @@
* Test cases for ES6 transpilation. Despite the name, this isn't just testing {@link * Test cases for ES6 transpilation. Despite the name, this isn't just testing {@link
* Es6ToEs3Converter}, but also some other ES6 transpilation passes. See #getProcessor. * Es6ToEs3Converter}, but also some other ES6 transpilation passes. See #getProcessor.
*/ */
public final class Es7ToEs6ConverterTest extends CompilerTestCase { public final class Es7RewriteExponentialOperatorTest extends CompilerTestCase {


public Es7ToEs6ConverterTest() { public Es7RewriteExponentialOperatorTest() {
super(MINIMAL_EXTERNS); super(MINIMAL_EXTERNS);
} }


Expand All @@ -37,7 +37,7 @@ protected void setUp() throws Exception {


@Override @Override
protected CompilerPass getProcessor(Compiler compiler) { protected CompilerPass getProcessor(Compiler compiler) {
return new Es7ToEs6Converter(compiler); return new Es7RewriteExponentialOperator(compiler);
} }


@Override @Override
Expand Down

0 comments on commit fa80945

Please sign in to comment.