-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When the extends clause of a class is not a qualified name, alias the…
… expression so that it can be transpiled. Allows GETELEM and Mixin functions extends to be correctly transpiled.
- Loading branch information
1 parent
b8d5c19
commit 841d065
Showing
4 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
src/com/google/javascript/jscomp/Es6RewriteClassExtendsExpressions.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,95 @@ | ||
/* | ||
* Copyright 2018 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 static com.google.common.base.Preconditions.checkArgument; | ||
|
||
import com.google.javascript.jscomp.deps.ModuleNames; | ||
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; | ||
|
||
/** | ||
* Extracts ES6 class extends expressions and creates an alias. | ||
* | ||
* <p>Example: Before: | ||
* | ||
* <p><code>class Foo extends Bar() {}</code> | ||
* | ||
* <p>After: | ||
* | ||
* <p><code> | ||
* const $jscomp$classextends$var0 = Bar(); | ||
* class Foo extends $jscomp$classextends$var0 {} | ||
* </code> | ||
* | ||
* <p>This must be done before {@link Es6ConvertSuper}, because that pass only handles extends | ||
* clauses which are simple NAME or GETPROP nodes. | ||
*/ | ||
public final class Es6RewriteClassExtendsExpressions extends NodeTraversal.AbstractPostOrderCallback | ||
implements HotSwapCompilerPass { | ||
|
||
static final String CLASS_EXTENDS_VAR = "$classextends$var"; | ||
|
||
private final AbstractCompiler compiler; | ||
private int classExtendsVarCounter = 0; | ||
private static final FeatureSet features = FeatureSet.BARE_MINIMUM.with(Feature.CLASSES); | ||
|
||
Es6RewriteClassExtendsExpressions(AbstractCompiler compiler) { | ||
this.compiler = compiler; | ||
} | ||
|
||
@Override | ||
public void process(Node externs, Node root) { | ||
TranspilationPasses.processTranspile(compiler, externs, features, this); | ||
TranspilationPasses.processTranspile(compiler, root, features, this); | ||
} | ||
|
||
@Override | ||
public void hotSwapScript(Node scriptRoot, Node originalRoot) { | ||
TranspilationPasses.hotSwapTranspile(compiler, scriptRoot, features, this); | ||
} | ||
|
||
@Override | ||
public void visit(NodeTraversal t, Node n, Node parent) { | ||
if (n.isClass() && needsExtendsDecomposing(n)) { | ||
extractExtends(n); | ||
} | ||
} | ||
|
||
private boolean needsExtendsDecomposing(Node classNode) { | ||
checkArgument(classNode.isClass()); | ||
return !(classNode.getSecondChild().isEmpty() || classNode.getSecondChild().isQualifiedName()); | ||
} | ||
|
||
private void extractExtends(Node classNode) { | ||
String name = | ||
ModuleNames.fileToJsIdentifier(classNode.getStaticSourceFile().getName()) | ||
+ CLASS_EXTENDS_VAR | ||
+ (classExtendsVarCounter++); | ||
|
||
Node statement = NodeUtil.getEnclosingStatement(classNode); | ||
Node originalExtends = classNode.getSecondChild(); | ||
originalExtends.replaceWith(IR.name(name).useSourceInfoFrom(originalExtends)); | ||
Node extendsAlias = | ||
IR.constNode(IR.name(name), originalExtends) | ||
.useSourceInfoIfMissingFromForTree(originalExtends); | ||
statement.getParent().addChildBefore(extendsAlias, statement); | ||
compiler.reportChangeToEnclosingScope(classNode); | ||
} | ||
} |
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
76 changes: 76 additions & 0 deletions
76
test/com/google/javascript/jscomp/Es6RewriteClassExtendsExpressionsTest.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,76 @@ | ||
/* | ||
* Copyright 2018 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; | ||
|
||
public final class Es6RewriteClassExtendsExpressionsTest extends CompilerTestCase { | ||
|
||
@Override | ||
protected CompilerPass getProcessor(Compiler compiler) { | ||
return new Es6RewriteClassExtendsExpressions(compiler); | ||
} | ||
|
||
@Override | ||
protected void setUp() throws Exception { | ||
super.setUp(); | ||
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2015); | ||
setLanguageOut(LanguageMode.ECMASCRIPT3); | ||
disableTypeCheck(); | ||
enableRunTypeCheckAfterProcessing(); | ||
} | ||
|
||
public void testBasic() { | ||
test( | ||
"const foo = {'bar': Object}; class Foo extends foo['bar'] {}", | ||
lines( | ||
"const foo = {'bar': Object};", | ||
"const testcode$classextends$var0 = foo['bar'];", | ||
"class Foo extends testcode$classextends$var0 {}")); | ||
} | ||
|
||
public void testName() { | ||
testSame("class Foo extends Object {}"); | ||
} | ||
|
||
public void testGetProp() { | ||
testSame("const foo = { bar: Object}; class Foo extends foo.bar {}"); | ||
} | ||
|
||
public void testMixinFunction() { | ||
test( | ||
lines( | ||
"/** @return {function(new:Object)} */", | ||
"function mixObject(Superclass) {", | ||
" return class extends Superclass {", | ||
" bar() { return 'bar'; }", | ||
" };", | ||
"}", | ||
"class Foo {}", | ||
"class Bar extends mixObject(Foo) {}"), | ||
lines( | ||
"/** @return {function(new:Object)} */", | ||
"function mixObject(Superclass) {", | ||
" return class extends Superclass {", | ||
" bar() { return 'bar'; }", | ||
" };", | ||
"}", | ||
"class Foo {}", | ||
"const testcode$classextends$var0 = mixObject(Foo);", | ||
"class Bar extends testcode$classextends$var0 {}")); | ||
} | ||
} |