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): unable to return Promise<void> #3752

Merged
merged 2 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 21 additions & 2 deletions packages/jsii/lib/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2222,7 +2222,11 @@ export class Assembler implements Emitter {
protected: _isProtected(symbol) || undefined,
returns: _isVoid(returnType)
? undefined
: this._optionalValue(returnType, declaration.name, 'return type'),
: this._optionalValue(
returnType,
declaration.type ?? declaration.name,
'return type',
),
async: _isPromise(returnType) || undefined,
static: _isStatic(symbol) || undefined,
locationInModule: this.declarationLocation(declaration),
Expand Down Expand Up @@ -2435,7 +2439,7 @@ export class Assembler implements Emitter {
{
...this._optionalValue(
this._typeChecker.getTypeAtLocation(paramDeclaration),
paramDeclaration.name,
paramDeclaration.type ?? paramDeclaration.name,
'parameter type',
),
name: paramSymbol.name,
Expand Down Expand Up @@ -2969,7 +2973,22 @@ function _isStatic(symbol: ts.Symbol): boolean {
);
}

/**
* Determines whether a given type is void or Promise<void>.
*
* @param type the tested type
*
* @returns `true` if the type is void or Promise<void>
*/
function _isVoid(type: ts.Type): boolean {
if (_isPromise(type)) {
const typeRef = type as ts.TypeReference;
return (
typeRef.typeArguments != null &&
typeRef.typeArguments.length === 1 &&
_isVoid(typeRef.typeArguments[0])
);
}
return (type.flags & ts.TypeFlags.Void) !== 0;
}

Expand Down
40 changes: 40 additions & 0 deletions packages/jsii/test/promise.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { sourceToAssemblyHelper } from '../lib';

// ----------------------------------------------------------------------
test('Promise<void> is a valid return type', () => {
const assembly = sourceToAssemblyHelper(`
export class PromiseMaker {
public static staticPromise(): Promise<void> {
return Promise.resolve();
}

public instancePromise(): Promise<void> {
return Promise.resolve();
}

private constructor() {}
}
`);

expect(assembly.types!['testpkg.PromiseMaker']).toEqual({
assembly: 'testpkg',
fqn: 'testpkg.PromiseMaker',
kind: 'class',
methods: [
{
async: true,
locationInModule: { filename: 'index.ts', line: 3 },
name: 'staticPromise',
static: true,
},
{
async: true,
locationInModule: { filename: 'index.ts', line: 7 },
name: 'instancePromise',
},
],
locationInModule: { filename: 'index.ts', line: 2 },
name: 'PromiseMaker',
symbolId: 'index:PromiseMaker',
});
});