Skip to content

Commit 50c0b92

Browse files
Closure Teamcopybara-github
Closure Team
authored andcommitted
Added public field this support for computed field to ClosureCheckModule for false positives.
This fixes `JSC_GOOG_MODULE_REFERENCES_THIS` error thrown when a public field was assigned using the `this[property]`. ClosureCheckModule now checks if the assignment is allowed given that the `this` is not inside a goog.module by updating the hoist scope. PiperOrigin-RevId: 620887878
1 parent ebda2f1 commit 50c0b92

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,9 @@ private void handleClassMembers(Node n, Node parent) {
10851085
break;
10861086
case COMPUTED_FIELD_DEF:
10871087
currentNode = n;
1088+
1089+
Node previousHoistScopeRoot = currentHoistScopeRoot;
1090+
currentHoistScopeRoot = n;
10881091
pushScope(child);
10891092

10901093
if (callback.shouldTraverse(this, child, n)) {
@@ -1096,6 +1099,7 @@ private void handleClassMembers(Node n, Node parent) {
10961099
}
10971100

10981101
popScope();
1102+
currentHoistScopeRoot = previousHoistScopeRoot;
10991103
break;
11001104
case MEMBER_FIELD_DEF:
11011105
handleMemberFieldDef(n, child);

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

+45
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,26 @@ public void testGoogModuleThisInSubclass() {
7777
"class Bar extends Foo {", //
7878
" z = this.x;",
7979
"}"));
80+
81+
testSame(
82+
lines(
83+
"goog.module('xyz');",
84+
"class Foo {", //
85+
" [x] = 5;",
86+
"}",
87+
"class Bar extends Foo {", //
88+
" z = this[x];",
89+
"}"));
90+
91+
testSame(
92+
lines(
93+
"goog.module('xyz');",
94+
"class Foo {", //
95+
" [x] = 5;",
96+
"}",
97+
"class Bar extends Foo {", //
98+
" [z] = this[x];",
99+
"}"));
80100
}
81101

82102
@Test
@@ -89,6 +109,23 @@ public void testGoogModuleThisOnFields() {
89109
" x = 5;",
90110
" y = this.x",
91111
"}"));
112+
113+
testSame(
114+
lines(
115+
"goog.module('xyz');",
116+
"class Foo {", //
117+
" [x] = 5;",
118+
" [y] = this[x]",
119+
"}"));
120+
121+
testError(
122+
lines(
123+
"goog.module('xyz');",
124+
"class Foo {", //
125+
" [x] = 5;",
126+
" [this.x] = 6",
127+
"}"),
128+
GOOG_MODULE_REFERENCES_THIS);
92129
}
93130

94131
@Test
@@ -101,6 +138,14 @@ public void testStaticGoogModuleThisOnStaticFields() {
101138
" static x = 5;",
102139
" static y = this.x",
103140
"}"));
141+
142+
testSame(
143+
lines(
144+
"goog.module('math')", //
145+
"class Foo {",
146+
" static [x] = 5;",
147+
" static [y] = this[x]",
148+
"}"));
104149
}
105150

106151
@Test

0 commit comments

Comments
 (0)