Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull out Es6NormalizeShorthandProperties as the first ES6 pass in Tra…
…nspilationPasses. Also adds regression tests for various failures that happen when shorthand properties aren't handled correctly. #2685 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=174746346
- Loading branch information
Showing
8 changed files
with
150 additions
and
26 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/com/google/javascript/jscomp/Es6NormalizeShorthandProperties.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,55 @@ | ||
/* | ||
* Copyright 2017 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.NodeTraversal.AbstractPostOrderCallback; | ||
import com.google.javascript.rhino.IR; | ||
import com.google.javascript.rhino.Node; | ||
|
||
/** | ||
* Normalizes shorthand object properties. This should be one of the first things done when | ||
* transpiling from ES6 down to ES5, as it allows all the following checks and transpilations to not | ||
* care about shorthand and destructured assignments. | ||
*/ | ||
public final class Es6NormalizeShorthandProperties extends AbstractPostOrderCallback | ||
implements HotSwapCompilerPass { | ||
private final AbstractCompiler compiler; | ||
|
||
public Es6NormalizeShorthandProperties(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 void visit(NodeTraversal t, Node n, Node parent) { | ||
// Transform keys that look like {foo} into {foo: foo} by adding a NAME node | ||
// with the same string as the only child of any child-less STRING_KEY node. | ||
if (n.isStringKey() && !n.hasChildren()) { | ||
n.addChildToFront(IR.name(n.getString()).useSourceInfoFrom(n)); | ||
compiler.reportChangeToEnclosingScope(n); | ||
} | ||
} | ||
} |
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
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
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