-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(jsii): Validate overriding does not affect optionality (#549)
ptionality must remain unchancged when overriding methods and properties, such that types are pure with respects to Liskov's substitution.
- Loading branch information
1 parent
719be24
commit 8c826c1
Showing
7 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/jsii/test/negatives/neg.implementing-method-changes-optionality.1.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
///!MATCH_ERROR: jsii.Implementor#method changes the optionality of paramerter _optional when overriding jsii.AbstractClass (expected true, found false) | ||
|
||
// Attempt to change optionality of method parameter | ||
export abstract class AbstractClass { | ||
public abstract method(required: string, optional?: number): void; | ||
} | ||
|
||
export class Implementor extends AbstractClass { | ||
public method(_required: string, _optional: number): void { | ||
// ... | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/jsii/test/negatives/neg.implementing-method-changes-optionality.2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
///!MATCH_ERROR: jsii.Implementor#method changes the optionality of paramerter _optional when overriding jsii.ParentClass (expected true, found false) | ||
|
||
// Attempt to change optionality of method parameter | ||
export class ParentClass { | ||
public method(_required: string, _optional?: number): void { | ||
// ... | ||
} | ||
} | ||
|
||
export class Implementor extends ParentClass { | ||
public method(_required: string, _optional: number): void { | ||
// ... | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/jsii/test/negatives/neg.implementing-method-changes-optionality.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
///!MATCH_ERROR: jsii.Implementor#method changes the optionality of paramerter _optional when implementing jsii.IInterface (expected true, found false) | ||
|
||
// Attempt to change optionality of method parameter | ||
export interface IInterface { | ||
method(required: string, optional?: number): void; | ||
} | ||
|
||
export class Implementor implements IInterface { | ||
public method(_required: string, _optional: number): void { | ||
// ... | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/jsii/test/negatives/neg.implementing-property-changes-optionality.1.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
///!MATCH_ERROR: jsii.Implementor#property changes optionality of property when overriding jsii.AbstractClass | ||
|
||
// Attempt to change optionality of method parameter | ||
export abstract class AbstractClass { | ||
public abstract property?: string; | ||
} | ||
|
||
export class Implementor extends AbstractClass { | ||
public property: string; | ||
|
||
constructor() { | ||
super(); | ||
this.property = 'Bazinga!'; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/jsii/test/negatives/neg.implementing-property-changes-optionality.2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
///!MATCH_ERROR: jsii.Implementor#property changes optionality of property when overriding jsii.ParentClass | ||
|
||
// Attempt to change optionality of method parameter | ||
export class ParentClass { | ||
public property?: string = undefined; | ||
} | ||
|
||
export class Implementor extends ParentClass { | ||
public property: string; | ||
|
||
constructor() { | ||
super(); | ||
this.property = 'Bazinga!'; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/jsii/test/negatives/neg.implementing-property-changes-optionality.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
///!MATCH_ERROR: jsii.Implementor#property changes optionality of property when implementing jsii.IInterface | ||
|
||
// Attempt to change optionality of method parameter | ||
export interface IInterface { | ||
property?: string; | ||
} | ||
|
||
export class Implementor implements IInterface { | ||
public property: string; | ||
|
||
constructor() { | ||
this.property = 'Bazinga!'; | ||
} | ||
} |