-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(jsii): better errors around use of hidden types (#1861)
When a hidden (that is, `@internal` or not exported) type is used in a visible (`public` or `protected` on an exported type) API, the error produced would refer to the unusable type, but would not give any indication of where it was being used from. This makes several enhancements to this process: - Qualify the kind of use for the type (return, parameter, ...) - Attach the error to the resolving node (usage location) - Provide a related message with the unusable type's declaration - Specifically message around "this" (used or inferred as a return type) This is going to particularly enhance the experience of folks extending internal base types, where those internal base types declare members that return hidden types (or "this"). Fixes #1860
- Loading branch information
1 parent
c236307
commit 31ec095
Showing
72 changed files
with
927 additions
and
355 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
581 changes: 581 additions & 0 deletions
581
packages/jsii/test/__snapshots__/negatives.test.js.snap
Large diffs are not rendered by default.
Oops, something went wrong.
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
2 changes: 0 additions & 2 deletions
2
packages/jsii/test/negatives/neg.behavior-requires-iprefix.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
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 |
---|---|---|
@@ -1,5 +1 @@ | ||
///!MATCH_ERROR: Type names must use PascalCase: My_class | ||
|
||
export class My_class { | ||
|
||
} | ||
export class My_class {} |
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 |
---|---|---|
@@ -1,5 +1 @@ | ||
///!MATCH_ERROR: Type names must use PascalCase: myclass | ||
|
||
export class myclass { | ||
|
||
} | ||
export class myclass {} |
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 |
---|---|---|
@@ -1,4 +1 @@ | ||
///!MATCH_ERROR: Cannot find name 'boom'. | ||
///!MATCH_ERROR: Cannot find name 'CompilerErrorIsHere'. | ||
|
||
boom! >CompilerErrorIsHere | ||
boom! > CompilerErrorIsHere; |
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 |
---|---|---|
@@ -1,8 +1,6 @@ | ||
///!MATCH_ERROR: Exported enum cannot be declared 'const' | ||
|
||
export const enum NotAllowed { | ||
ThisEnum, | ||
GetsInlined, | ||
AndSoItGetsLost, | ||
ForJsii | ||
} | ||
ForJsii, | ||
} |
6 changes: 1 addition & 5 deletions
6
packages/jsii/test/negatives/neg.double-interface-members-deeper.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 |
---|---|---|
@@ -1,15 +1,11 @@ | ||
///!MATCH_ERROR: Interface declares same member as inherited interface: foo | ||
|
||
export interface IA { | ||
foo(): void; | ||
} | ||
|
||
export interface IB extends IA { | ||
bar(): void; | ||
bar(): void; | ||
} | ||
|
||
export interface IC extends IB { | ||
foo(): void; | ||
} | ||
|
||
|
3 changes: 0 additions & 3 deletions
3
packages/jsii/test/negatives/neg.double-interface-members-method.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 |
---|---|---|
@@ -1,9 +1,6 @@ | ||
///!MATCH_ERROR: Interface declares same member as inherited interface: foo | ||
|
||
export interface IA { | ||
foo(): void; | ||
} | ||
export interface IB extends IA { | ||
foo(): void; | ||
} | ||
|
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
///!MATCH_ERROR: Interface declares same member as inherited interface: foo | ||
|
||
export interface A { | ||
foo: number; | ||
} | ||
|
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 |
---|---|---|
@@ -1,6 +1,4 @@ | ||
///!MATCH_ERROR: Enum members must use ALL_CAPS: Goo | ||
|
||
export enum MyEnum { | ||
FOO, | ||
Goo | ||
FOO, | ||
Goo, | ||
} |
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 |
---|---|---|
@@ -1,6 +1,4 @@ | ||
///!MATCH_ERROR: Type names must use PascalCase: myEnum | ||
|
||
export enum myEnum { | ||
FOO, | ||
GOO | ||
FOO, | ||
GOO, | ||
} |
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 |
---|---|---|
@@ -1,6 +1,4 @@ | ||
///!MATCH_ERROR: Type names must use PascalCase: My_Enum | ||
|
||
export enum My_Enum { | ||
FOO, | ||
GOO | ||
FOO, | ||
GOO, | ||
} |
4 changes: 1 addition & 3 deletions
4
packages/jsii/test/negatives/neg.expose-unexported-type-external.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 |
---|---|---|
@@ -1,10 +1,8 @@ | ||
///!MATCH_ERROR: Exported APIs cannot use un-exported type jsii.UnexportedType | ||
|
||
// Attempt to expose an unexported type defined in another file should fial | ||
// because that type will not be available in the module spec. | ||
|
||
import { UnexportedType } from './mylib'; | ||
|
||
export class ExportedType { | ||
public p?: UnexportedType; | ||
public p?: UnexportedType; | ||
} |
7 changes: 2 additions & 5 deletions
7
packages/jsii/test/negatives/neg.expose-unexported-type-internal-in-namespace.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 |
---|---|---|
@@ -1,13 +1,10 @@ | ||
///!MATCH_ERROR: Cannot use private type MyNamespace.UnexportedType in exported declarations | ||
|
||
// Attempt to expose an unexported type defined in this file should fail | ||
// because that type will not be available in the module spec. | ||
|
||
namespace MyNamespace { | ||
export class UnexportedType { | ||
} | ||
export class UnexportedType {} | ||
} | ||
|
||
export class ExportedType { | ||
public p?: MyNamespace.UnexportedType; | ||
public p?: MyNamespace.UnexportedType; | ||
} |
8 changes: 2 additions & 6 deletions
8
packages/jsii/test/negatives/neg.expose-unexported-type-internal.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 |
---|---|---|
@@ -1,12 +1,8 @@ | ||
///!MATCH_ERROR: Cannot use private type UnexportedType in exported declarations | ||
|
||
// Attempt to expose an unexported type defined in this file should fail | ||
// because that type will not be available in the module spec. | ||
|
||
class UnexportedType { | ||
|
||
} | ||
class UnexportedType {} | ||
|
||
export class ExportedType { | ||
public p?: UnexportedType; | ||
public p?: UnexportedType; | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/jsii/test/negatives/neg.expose-unexported-type-this.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,13 @@ | ||
// Attempt to return "this" from a hidden base class | ||
|
||
abstract class HiddenBaseClass { | ||
public returnsThis() { | ||
return this; | ||
} | ||
} | ||
|
||
export class PublicClass extends HiddenBaseClass { | ||
public constructor(public readonly boolean = true) { | ||
super(); | ||
} | ||
} |
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
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
10 changes: 4 additions & 6 deletions
10
packages/jsii/test/negatives/neg.implementation-changes-types.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 |
---|---|---|
@@ -1,14 +1,12 @@ | ||
///!MATCH_ERROR: jsii.Something#returnSomething changes the return type when implementing jsii.ISomething (expected jsii.Superclass, found jsii.Subclass) | ||
|
||
export class Superclass {} | ||
export class Subclass extends Superclass {} | ||
|
||
export interface ISomething { | ||
returnSomething(): Superclass; | ||
returnSomething(): Superclass; | ||
} | ||
|
||
export class Something implements ISomething { | ||
public returnSomething(): Subclass { | ||
return 5; | ||
} | ||
public returnSomething(): Subclass { | ||
return 5; | ||
} | ||
} |
10 changes: 4 additions & 6 deletions
10
packages/jsii/test/negatives/neg.implementation-changes-types.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 |
---|---|---|
@@ -1,14 +1,12 @@ | ||
///!MATCH_ERROR: jsii.ISomethingElse#returnSomething changes the return type when implementing jsii.ISomething (expected jsii.Superclass, found jsii.Subclass) | ||
|
||
export class Superclass {} | ||
export class Subclass extends Superclass {} | ||
|
||
export interface ISomething { | ||
returnSomething(): Superclass; | ||
returnSomething(): Superclass; | ||
} | ||
|
||
export class ISomethingElse implements ISomething { | ||
public returnSomething(): Subclass { | ||
return new Subclass(); | ||
} | ||
public returnSomething(): Subclass { | ||
return new Subclass(); | ||
} | ||
} |
10 changes: 4 additions & 6 deletions
10
packages/jsii/test/negatives/neg.implementation-changes-types.3.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 |
---|---|---|
@@ -1,14 +1,12 @@ | ||
///!MATCH_ERROR: jsii.Something#takeSomething changes type of argument _argument when implementing jsii.ISomething (expected jsii.Superclass, found jsii.Subclass | ||
|
||
export class Superclass {} | ||
export class Subclass extends Superclass {} | ||
|
||
export interface ISomething { | ||
takeSomething(argument: Superclass): void; | ||
takeSomething(argument: Superclass): void; | ||
} | ||
|
||
export class Something implements ISomething { | ||
public takeSomething(_argument: Subclass): void { | ||
// Nothing | ||
} | ||
public takeSomething(_argument: Subclass): void { | ||
// Nothing | ||
} | ||
} |
6 changes: 2 additions & 4 deletions
6
packages/jsii/test/negatives/neg.implementation-changes-types.4.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 |
---|---|---|
@@ -1,12 +1,10 @@ | ||
///!MATCH_ERROR: jsii.Something#something changes the type of property when implementing jsii.ISomething (expected jsii.Superclass, found jsii.Subclass) | ||
|
||
export class Superclass {} | ||
export class Subclass extends Superclass {} | ||
|
||
export interface ISomething { | ||
something: Superclass; | ||
something: Superclass; | ||
} | ||
|
||
export class Something implements ISomething { | ||
public something: Subclass = new Subclass(); | ||
public something: Subclass = new Subclass(); | ||
} |
10 changes: 4 additions & 6 deletions
10
packages/jsii/test/negatives/neg.implementation-changes-types.5.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 |
---|---|---|
@@ -1,18 +1,16 @@ | ||
///!MATCH_ERROR: jsii.ISomethingElse#something changes the type of property when implementing jsii.ISomething (expected jsii.Superclass, found jsii.Subclass) | ||
|
||
export class Superclass {} | ||
export class Subclass extends Superclass {} | ||
|
||
export interface ISomething { | ||
something: Superclass; | ||
something: Superclass; | ||
} | ||
|
||
export interface ISomethingElse extends ISomething { | ||
addUnrelatedMember: number; | ||
addUnrelatedMember: number; | ||
} | ||
|
||
// Should still fail even though 2-level inheritance | ||
export class Something implements ISomethingElse { | ||
public something: Subclass = new Subclass(); | ||
public addUnrelatedMember: number = 1; | ||
public something: Subclass = new Subclass(); | ||
public addUnrelatedMember: number = 1; | ||
} |
2 changes: 0 additions & 2 deletions
2
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
2 changes: 0 additions & 2 deletions
2
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
2 changes: 0 additions & 2 deletions
2
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
2 changes: 0 additions & 2 deletions
2
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
2 changes: 0 additions & 2 deletions
2
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
2 changes: 0 additions & 2 deletions
2
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
Oops, something went wrong.