Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jsii): prohibit changing visibility when overriding #1876

Merged
merged 2 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/jsii/lib/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,14 @@ function _defaultValidations(): ValidationFunction[] {
label: string,
action: string,
) {
if (!!expected.protected !== !!actual.protected) {
const expVisibility = expected.protected ? 'protected' : 'public';
const actVisibility = actual.protected ? 'protected' : 'public';
diagnostic(
ts.DiagnosticCategory.Error,
`${label} changes visibility when ${action} (expected ${expVisibility}, found ${actVisibility})`,
);
}
if (!deepEqual(actual.returns, expected.returns)) {
const expType = spec.describeTypeReference(expected.returns?.type);
const actType = spec.describeTypeReference(actual.returns?.type);
Expand Down Expand Up @@ -446,6 +454,14 @@ function _defaultValidations(): ValidationFunction[] {
label: string,
action: string,
) {
if (!!expected.protected !== !!actual.protected) {
const expVisibility = expected.protected ? 'protected' : 'public';
const actVisibility = actual.protected ? 'protected' : 'public';
diagnostic(
ts.DiagnosticCategory.Error,
`${label} changes visibility when ${action} (expected ${expVisibility}, found ${actVisibility})`,
);
}
if (!deepEqual(expected.type, actual.type)) {
const expType = spec.describeTypeReference(expected.type);
const actType = spec.describeTypeReference(actual.type);
Expand Down
6 changes: 6 additions & 0 deletions packages/jsii/test/__snapshots__/negatives.test.js.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions packages/jsii/test/negatives/neg.override-changes-visibility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Note: not testing "public -> protected" because this is invalid TypeScript,
// so the type checker will already have caught it for us.

export class BaseClass {
protected readonly property?: string;

protected method() {
return;
}
}

export class ChildClass extends BaseClass {
// This property cannot be public as it overrides the one on BaseClass which is protected
public readonly property?: string;

// This method cannot be public as it overrides the one on BaseClass which is protected
public method() {
throw new Error('Not Implemented');
}
}