Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9c84acf

Browse files
lauraharkercopybara-github
authored andcommitted
Correctly track computed getter/setter features in deserialization
PiperOrigin-RevId: 819860645
1 parent 3c0005f commit 9c84acf

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

‎src/com/google/javascript/jscomp/AstValidator.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,14 +1879,17 @@ private void validateComputedPropClassMethod(Node n) {
18791879
validateFunctionExpression(n.getLastChild());
18801880
if (n.getBooleanProp(Node.COMPUTED_PROP_GETTER)) {
18811881
validateObjectLitComputedPropGetKey(n);
1882+
validateFeature(Feature.CLASS_GETTER_SETTER, n);
18821883
} else if (n.getBooleanProp(Node.COMPUTED_PROP_SETTER)) {
18831884
validateObjectLitComputedPropSetKey(n);
1885+
validateFeature(Feature.CLASS_GETTER_SETTER, n);
18841886
}
18851887
}
18861888
}
18871889

18881890
private void validateObjectLitComputedPropGetKey(Node n) {
18891891
validateFeature(Feature.COMPUTED_PROPERTIES, n);
1892+
validateFeature(Feature.GETTER, n);
18901893
validateNodeType(Token.COMPUTED_PROP, n);
18911894
validateProperties(n);
18921895
validateChildCount(n);
@@ -1904,6 +1907,7 @@ private void validateObjectLitComputedPropGetKey(Node n) {
19041907

19051908
private void validateObjectLitComputedPropSetKey(Node n) {
19061909
validateFeature(Feature.COMPUTED_PROPERTIES, n);
1910+
validateFeature(Feature.SETTER, n);
19071911
validateNodeType(Token.COMPUTED_PROP, n);
19081912
validateProperties(n);
19091913
validateChildCount(n);

‎src/com/google/javascript/jscomp/serialization/ScriptNodeDeserializer.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,17 @@ private void recordScriptFeatures(FeatureContext context, Node node) {
254254
return;
255255
case COMPUTED_PROP:
256256
this.addScriptFeature(Feature.COMPUTED_PROPERTIES);
257+
boolean isGetter = node.getBooleanProp(Node.COMPUTED_PROP_GETTER);
258+
boolean isSetter = node.getBooleanProp(Node.COMPUTED_PROP_SETTER);
259+
boolean isClassMember = context.equals(FeatureContext.CLASS_MEMBERS);
260+
if (isGetter) {
261+
this.addScriptFeature(Feature.GETTER);
262+
} else if (isSetter) {
263+
this.addScriptFeature(Feature.SETTER);
264+
}
265+
if ((isGetter || isSetter) && isClassMember) {
266+
this.addScriptFeature(Feature.CLASS_GETTER_SETTER);
267+
}
257268
return;
258269
case OPTCHAIN_GETPROP:
259270
case OPTCHAIN_CALL:

‎test/com/google/javascript/jscomp/serialization/SerializeAndDeserializeAstTest.java‎

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public final class SerializeAndDeserializeAstTest extends CompilerTestCase {
7878
private boolean includeTypes;
7979
private boolean resolveSourceMapAnnotations;
8080
private boolean parseInlineSourceMaps;
81+
private boolean checkJsDocEquality;
8182
private ImmutableList<String> runtimeLibraries = null;
8283
private Optional<PassFactory> preSerializePassFactory = Optional.empty();
8384

@@ -111,6 +112,7 @@ public void setUp() throws Exception {
111112
enableCreateModuleMap();
112113
enableSourceInformationAnnotator();
113114
this.includeTypes = true;
115+
this.checkJsDocEquality = true;
114116
this.resolveSourceMapAnnotations = true;
115117
this.parseInlineSourceMaps = true;
116118
this.runtimeLibraries = ImmutableList.of();
@@ -166,6 +168,11 @@ public void testObjectWithQuotedGetter() {
166168
testSame("let obj = {get 'x'() {}};");
167169
}
168170

171+
@Test
172+
public void testObjectWithComputedGetter() {
173+
testSame("let obj = {get [x]() {}};");
174+
}
175+
169176
@Test
170177
public void testObjectWithSetter() {
171178
testSame("let obj = {set x(value) {}};");
@@ -176,6 +183,11 @@ public void testObjectWithQuotedSetter() {
176183
testSame("let obj = {set 'x'(value) {}};");
177184
}
178185

186+
@Test
187+
public void testObjectWithComputedSetter() {
188+
testSame("let obj = {set [x](value) {}};");
189+
}
190+
179191
@Test
180192
public void testTemplateLiteral_simple() {
181193
testSame("let obj = `foobar`;");
@@ -259,11 +271,10 @@ set d(x) {}
259271
}
260272
""");
261273

262-
// Type checking will report computed property accesses as errors for a class,
263-
// so disable it for this case which contains several.
264-
disableTypeCheck();
274+
this.checkJsDocEquality = false; // @unrestricted
265275
testSame(
266276
"""
277+
/** @unrestricted */
267278
class Foo {
268279
a() {}
269280
'b'() {}
@@ -274,6 +285,30 @@ set d(x) {}
274285
""");
275286
}
276287

288+
@Test
289+
public void testClassDeclarationWithComputedGetter() {
290+
this.checkJsDocEquality = false; // @dict
291+
testSame(
292+
"""
293+
/** @dict */
294+
class Foo {
295+
get ['a']() {}
296+
}
297+
""");
298+
}
299+
300+
@Test
301+
public void testClassDeclarationWithComputedSetter() {
302+
this.checkJsDocEquality = false; // @dict
303+
testSame(
304+
"""
305+
/** @dict */
306+
class Foo {
307+
set ['a'](val) {}
308+
}
309+
""");
310+
}
311+
277312
@Test
278313
public void testClassDeclarationWithFields() {
279314
testSame(
@@ -931,7 +966,11 @@ private Result testAndReturnResult(Externs externs, Sources code, Expected expec
931966
}
932967

933968
Node expectedRoot = this.parseExpectedJs(expected);
934-
assertNode(newSourceRoot).isEqualIncludingJsDocTo(expectedRoot);
969+
if (checkJsDocEquality) {
970+
assertNode(newSourceRoot).isEqualIncludingJsDocTo(expectedRoot);
971+
} else {
972+
assertNode(newSourceRoot).isEqualTo(expectedRoot);
973+
}
935974
Node newRoot = IR.root(newExternsRoot, newSourceRoot);
936975

937976
new AstValidator(deserializingCompiler, /* validateScriptFeatures= */ true)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /