Skip to content

Commit 8b4b1dd

Browse files
concavelenzblickly
authored andcommitted
Support 'symbol' as a property name in 'in' expressions
Fixes #3060 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=212994115
1 parent e845ed0 commit 8b4b1dd

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

src/com/google/javascript/jscomp/TypeCheck.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ public void visit(NodeTraversal t, Node n, Node parent) {
771771
left = n.getFirstChild();
772772
right = n.getLastChild();
773773
rightType = getJSType(right);
774-
validator.expectString(t, left, getJSType(left), "left side of 'in'");
774+
validator.expectStringOrSymbol(t, left, getJSType(left), "left side of 'in'");
775775
validator.expectObject(t, n, rightType, "'in' requires an object");
776776
if (rightType.isStruct()) {
777777
report(t, right, IN_USED_WITH_STRUCT);

test/com/google/javascript/jscomp/TypeCheckTest.java

+35-17
Original file line numberDiff line numberDiff line change
@@ -3931,10 +3931,11 @@ public void testIn3() {
39313931

39323932
@Test
39333933
public void testIn4() {
3934-
testTypes("Date in Object",
3935-
"left side of 'in'\n" +
3936-
"found : function(new:Date, ?=, ?=, ?=, ?=, ?=, ?=, ?=): string\n" +
3937-
"required: string");
3934+
testTypes(
3935+
"Date in Object",
3936+
"left side of 'in'\n"
3937+
+ "found : function(new:Date, ?=, ?=, ?=, ?=, ?=, ?=, ?=): string\n"
3938+
+ "required: (string|symbol)");
39383939
}
39393940

39403941
@Test
@@ -3960,19 +3961,36 @@ public void testIn6() {
39603961
public void testIn7() {
39613962
// Make sure we do inference in the 'in' expression.
39623963
testTypes(
3963-
"/**\n" +
3964-
" * @param {number} x\n" +
3965-
" * @return {number}\n" +
3966-
" */\n" +
3967-
"function g(x) { return 5; }" +
3968-
"function f() {" +
3969-
" var x = {};" +
3970-
" x.foo = '3';" +
3971-
" return g(x.foo) in {};" +
3972-
"}",
3973-
"actual parameter 1 of g does not match formal parameter\n" +
3974-
"found : string\n" +
3975-
"required: number");
3964+
lines(
3965+
"/**",
3966+
" * @param {number} x",
3967+
" * @return {number}",
3968+
" */",
3969+
"function g(x) { return 5; }",
3970+
"function f() {",
3971+
" var x = {};",
3972+
" x.foo = '3';",
3973+
" return g(x.foo) in {};",
3974+
"}"),
3975+
lines(
3976+
"actual parameter 1 of g does not match formal parameter",
3977+
"found : string",
3978+
"required: number"));
3979+
}
3980+
3981+
@Test
3982+
public void testInWithWellKnownSymbol() {
3983+
testTypesWithCommonExterns("Symbol.iterator in Object");
3984+
}
3985+
3986+
@Test
3987+
public void testInWithUniqueSymbol() {
3988+
testTypes("Symbol('foo') in Object");
3989+
}
3990+
3991+
@Test
3992+
public void testInWithSymbol() {
3993+
testTypes("function f(/** symbol */ s) { return s in {}; }");
39763994
}
39773995

39783996
@Test

0 commit comments

Comments
 (0)