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

fix(jsii): Prohibit illegal uses of structs (aka data types) #418

Merged
merged 4 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 24 additions & 0 deletions packages/jsii/lib/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,17 @@ export class Assembler implements Emitter {
}

jsiiType.interfaces = await this._processBaseInterfaces(fqn, clause.types.map(t => this._typeChecker.getTypeFromTypeNode(t)));
if (jsiiType.interfaces) {
this._deferUntilTypesAvailable(jsiiType.fqn, jsiiType.interfaces, type.symbol.valueDeclaration, (...ifaces) => {
for (const iface of ifaces) {
if (spec.isInterfaceType(iface) && iface.datatype) {
this._diagnostic(type.symbol.valueDeclaration,
ts.DiagnosticCategory.Error,
`Attempted to implement struct ${iface.fqn} from class ${jsiiType.fqn}`);
}
}
});
}
}

if (!type.isClass()) {
Expand Down Expand Up @@ -723,6 +734,19 @@ export class Assembler implements Emitter {
prop.immutable = true;
}
}
} else {
// This is *NOT* a data type, so it may not extend something that is one.
for (const base of bases) {
if (!spec.isInterfaceType(base)) {
// Invalid type we already warned about earlier, just ignoring it here...
continue;
}
if (base.datatype) {
this._diagnostic(type.symbol.valueDeclaration,
ts.DiagnosticCategory.Error,
`Attempted to extend struct ${base.fqn} from regular interface ${jsiiType.fqn}`);
}
}
}
});

Expand Down
10 changes: 10 additions & 0 deletions packages/jsii/test/negatives/neg.extend-struct.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
///!MATCH_ERROR: Attempted to extend struct jsii.Struct from regular interface jsii.IIllegal

// Attempt to extend a Struct (aka data type) from a regular interface will fail.
export interface Struct {
readonly field: string;
}

export interface IIllegal extends Struct {
method(): void;
}
14 changes: 14 additions & 0 deletions packages/jsii/test/negatives/neg.implement-struct.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
///!MATCH_ERROR: Attempted to implement struct jsii.Struct from class jsii.Illegal

// Attempt to implement a Struct (aka data type) will fail.
export interface Struct {
readonly field: string;
}

export class Illegal implements Struct {
public readonly field: string = 'foo';

public method(): void {
return;
}
}
10 changes: 10 additions & 0 deletions packages/jsii/test/negatives/neg.struct-extends-interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
///!MATCH_ERROR: Interface contains behavior: name should be "IStruct"

// Attempt to extend an interface from a struct (aka data type)
export interface IInterface {
readonly field: string;
}

export interface Struct extends IInterface {
readonly another: number;
}