Skip to content

Commit

Permalink
Preserve types in LateEs6ToEs3Converter and enable typechecking in it…
Browse files Browse the repository at this point in the history
…s unit tests

LateEs6ToEs3Converter handles template literals, computed properties, and member functions in object literals.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=197437059
  • Loading branch information
lauraharker authored and brad4d committed May 22, 2018
1 parent a8223ef commit 99970d0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/com/google/javascript/jscomp/LateEs6ToEs3Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public final class LateEs6ToEs3Converter implements NodeTraversal.Callback, HotS
private final boolean addTypes;
private final JSTypeRegistry registry;
private final JSType unknownType;
private final JSType stringType;

private static final String FRESH_COMP_PROP_VAR = "$jscomp$compprop";

Expand All @@ -66,6 +67,7 @@ public LateEs6ToEs3Converter(AbstractCompiler compiler) {
this.addTypes = compiler.hasTypeCheckingRun();
this.registry = compiler.getTypeRegistry();
this.unknownType = createType(addTypes, registry, JSTypeNative.UNKNOWN_TYPE);
this.stringType = createType(addTypes, registry, JSTypeNative.STRING_TYPE);
}

@Override
Expand Down Expand Up @@ -211,7 +213,7 @@ private void visitObjectWithComputedProperty(Node obj) {
Node val = propdef.removeFirstChild();
JSType valueType = val.getJSType();
propdef.setToken(Token.STRING);
propdef.setJSType(null);
propdef.setJSType(stringType);
Token token = propdef.isQuotedString() ? Token.GETELEM : Token.GETPROP;
Node access =
withType(new Node(token, withType(IR.name(objName), objectType), propdef), valueType);
Expand Down
22 changes: 13 additions & 9 deletions test/com/google/javascript/jscomp/LateEs6ToEs3ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ protected void setUp() throws Exception {
super.setUp();
setAcceptedLanguage(LanguageMode.ECMASCRIPT_2015);
setLanguageOut(LanguageMode.ECMASCRIPT3);
// TODO(b/78891530): enable type check and type info validation
enableTypeCheck();
enableTypeInfoValidation();
}

@Override
Expand All @@ -55,11 +56,12 @@ public void testObjectLiteralMemberFunctionDef() {

public void testInitSymbolIterator() {
test(
"var x = {[Symbol.iterator]: function() { return this; }};",
lines(
externs("/** @type {symbol} */ Symbol.iterator;"),
srcs("var x = {[Symbol.iterator]: function() { return this; }};"),
expected(lines(
"var $jscomp$compprop0 = {};",
"var x = ($jscomp$compprop0[Symbol.iterator] = function() {return this;},",
" $jscomp$compprop0)"));
" $jscomp$compprop0)")));
}

public void testMethodInObject() {
Expand Down Expand Up @@ -267,11 +269,13 @@ public void testTaggedTemplateLiteral() {
"tag()($jscomp$templatelit$0, hello);"));

test(
"a.b`${hello} world`",
lines(
"var $jscomp$templatelit$0 = /** @type {!ITemplateArray} */ (['', ' world']);",
"$jscomp$templatelit$0.raw = ['', ' world'];",
"a.b($jscomp$templatelit$0, hello);"));
externs("var a = {}; a.b;"),
srcs("a.b`${hello} world`"),
expected(
lines(
"var $jscomp$templatelit$0 = /** @type {!ITemplateArray} */ (['', ' world']);",
"$jscomp$templatelit$0.raw = ['', ' world'];",
"a.b($jscomp$templatelit$0, hello);")));

// https://github.com/google/closure-compiler/issues/1299
test(
Expand Down

0 comments on commit 99970d0

Please sign in to comment.