Skip to content

Commit 4fc3d90

Browse files
Closure Teamcopybara-github
Closure Team
authored andcommitted
CheckAccessControls: Properly detect the best instance type for a class declaration when it is returned from a function.
PiperOrigin-RevId: 736151473
1 parent 59cc4df commit 4fc3d90

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ private void exitAccessControlScope(Node root) {
232232
// We need to handle declaration syntaxes separately in a way that we can't determine based on
233233
// the type of just one node.
234234
// TODO(nickreid): Determine if these can be replaced with FUNCTION and CLASS cases below.
235-
if (NodeUtil.isFunctionDeclaration(n) || NodeUtil.isClassDeclaration(n)) {
235+
if (NodeUtil.isFunctionDeclaration(n)
236+
// isClassDeclaration returns false for many instances that we need to handle here.
237+
|| n.isClass()) {
236238
return instanceTypeFor(n.getJSType());
237239
}
238240

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

+117
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,123 @@ public void testProtectedAccessForProperties16() {
14911491
"}")));
14921492
}
14931493

1494+
@Test
1495+
public void testProtectedAccessForProperties17() {
1496+
// Access in subclass field initializer where Subclass parent is SCRIPT.
1497+
test(
1498+
srcs(
1499+
"""
1500+
class SuperClass {
1501+
/** @protected @return {number} */
1502+
getNum() { return 1; }
1503+
}
1504+
""",
1505+
"""
1506+
class Subclass extends SuperClass {
1507+
numField = this.getNum();
1508+
}
1509+
"""));
1510+
}
1511+
1512+
@Test
1513+
public void testProtectedAccessForProperties18() {
1514+
// Access in subclass field initializer where Subclass parent is BLOCK.
1515+
test(
1516+
srcs(
1517+
"""
1518+
class SuperClass {
1519+
/** @protected @return {number} */
1520+
getNum() { return 1; }
1521+
}
1522+
""",
1523+
"""
1524+
{
1525+
class Subclass extends SuperClass {
1526+
numField = this.getNum();
1527+
}
1528+
}
1529+
"""));
1530+
}
1531+
1532+
@Test
1533+
public void testProtectedAccessForProperties19() {
1534+
// Access in subclass field initializer where class parent is ASSIGN.
1535+
test(
1536+
srcs(
1537+
"""
1538+
class SuperClass {
1539+
/** @protected @return {number} */
1540+
getNum() { return 1; }
1541+
}
1542+
""",
1543+
"""
1544+
const Subclass = class extends SuperClass {
1545+
numField = this.getNum();
1546+
}
1547+
"""));
1548+
}
1549+
1550+
@Test
1551+
public void testProtectedAccessForProperties20() {
1552+
// Access in subclass field initializer where class parent is FUNCTION.
1553+
test(
1554+
srcs(
1555+
"""
1556+
class SuperClass {
1557+
/** @protected @return {number} */
1558+
getNum() { return 1; }
1559+
}
1560+
""",
1561+
"""
1562+
const Subclass = (() => class extends SuperClass {
1563+
numField = this.getNum();
1564+
})();
1565+
"""));
1566+
}
1567+
1568+
@Test
1569+
public void testProtectedAccessForProperties21() {
1570+
// Access in subclass field initializer where class parent is RETURN.
1571+
test(
1572+
srcs(
1573+
"""
1574+
class SuperClass {
1575+
/** @protected @return {number} */
1576+
getNum() { return 1; }
1577+
}
1578+
""",
1579+
"""
1580+
function classGenerator() {
1581+
return class extends SuperClass {
1582+
numField = this.getNum();
1583+
};
1584+
}
1585+
const Subclass = classGenerator();
1586+
"""));
1587+
}
1588+
1589+
@Test
1590+
public void testProtectedAccessForProperties22() {
1591+
// Access in subclass field initializer where class parent is GETPROP.
1592+
test(
1593+
srcs(
1594+
"""
1595+
class SuperClass {
1596+
/** @protected @return {number} */
1597+
getNum() { return 1; }
1598+
}
1599+
""",
1600+
"""
1601+
function getValue() {
1602+
return class extends SuperClass {
1603+
numField = this.getNum();
1604+
static staticField = 1;
1605+
}.staticField;
1606+
}
1607+
const value = getValue();
1608+
"""));
1609+
}
1610+
14941611
@Test
14951612
public void testProtectedPropAccess_inDifferentFile_inSubclass_throughDestructuring() {
14961613
test(

0 commit comments

Comments
 (0)