Skip to content

Commit 21a9389

Browse files
shickscopybara-github
authored andcommitted
Allow broadening visibility when overriding JS class methods.
PiperOrigin-RevId: 566739971
1 parent 6c74008 commit 21a9389

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -869,15 +869,14 @@ private void checkPropertyOverrideVisibility(
869869

870870
// Check that:
871871
// (a) the property *can* be overridden,
872-
// (b) the visibility of the override is the same as the
872+
// (b) the visibility of the override is the same as (or broader than) the
873873
// visibility of the original property,
874874
// (c) the visibility is explicitly redeclared if the override is in
875875
// a file with default visibility in the @fileoverview block.
876876
if (visibility == Visibility.PRIVATE && !sameInput) {
877877
compiler.report(
878878
JSError.make(propRef.getSourceNode(), PRIVATE_OVERRIDE, objectType.toString()));
879-
} else if (overridingVisibility != Visibility.INHERITED
880-
&& overridingVisibility != visibility
879+
} else if (!canOverrideVisibility(visibility, overridingVisibility)
881880
&& fileOverviewVisibility == null) {
882881
compiler.report(
883882
JSError.make(
@@ -889,6 +888,14 @@ private void checkPropertyOverrideVisibility(
889888
}
890889
}
891890

891+
private static boolean canOverrideVisibility(
892+
Visibility superclassVisibility, Visibility subclassVisibility) {
893+
// This allows INHERITED to override anything, PUBLIC to override anything (except INHERITED),
894+
// and PROTECTED to override anything (except PUBLIC or INHERITED). PRIVATE was already handled
895+
// in a previous check, leaving PACKAGE as the lowest visibility.
896+
return superclassVisibility.compareTo(subclassVisibility) <= 0;
897+
}
898+
892899
private void checkPropertyAccessVisibility(
893900
PropertyReference propRef,
894901
Visibility visibility,

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3005,6 +3005,22 @@ public void testGoodOverrideOfProtectedProperty() {
30053005
"}")));
30063006
}
30073007

3008+
@Test
3009+
public void testPublicOverrideOfProtectedProperty() {
3010+
test(
3011+
srcs(
3012+
lines(
3013+
"class Foo {", //
3014+
" /** @protected */",
3015+
" bar() {}",
3016+
"}"),
3017+
lines(
3018+
"class SubFoo extends Foo {", //
3019+
" /** @public */",
3020+
" bar() {}",
3021+
"}")));
3022+
}
3023+
30083024
@Test
30093025
public void testBadOverrideOfProtectedProperty() {
30103026
test(
@@ -3022,6 +3038,22 @@ public void testBadOverrideOfProtectedProperty() {
30223038
error(VISIBILITY_MISMATCH));
30233039
}
30243040

3041+
@Test
3042+
public void testProtectedOverrideOfPackageProperty() {
3043+
test(
3044+
srcs(
3045+
lines(
3046+
"class Foo {", //
3047+
" /** @package */",
3048+
" bar() {}",
3049+
"}"),
3050+
lines(
3051+
"class SubFoo extends Foo {", //
3052+
" /** @protected */",
3053+
" bar() {}",
3054+
"}")));
3055+
}
3056+
30253057
@Test
30263058
public void testBadOverrideOfPrivateProperty() {
30273059
test(

0 commit comments

Comments
 (0)