Skip to content

Commit

Permalink
feat: allow free function exports in typewriter (#1379)
Browse files Browse the repository at this point in the history
Typewriter does not allow free functions to be exported today. This PR
enables exporting free functions.

Required for aws/aws-cdk#31850.

---------

Signed-off-by: github-actions <[email protected]>
Co-authored-by: github-actions <[email protected]>
Co-authored-by: Momo Kornher <[email protected]>
  • Loading branch information
3 people authored Oct 25, 2024
1 parent 12bebf9 commit 83ec1c3
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/@cdklabs/typewriter/src/callable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Statement } from './statements';
import { Block } from './statements/block';
import { asStmt } from './statements/private';
import { Type } from './type';
import { DeclarationKind, TypeDeclaration, TypeSpec } from './type-declaration';
import { DeclarationKind, Exported, TypeDeclaration, TypeSpec } from './type-declaration';

export interface CallableSpec extends TypeSpec {
name: string;
Expand All @@ -29,7 +29,7 @@ export interface CallableExpr {
/**
* Can't be called "Function"
*/
export class FreeFunction extends TypeDeclaration implements CallableDeclaration {
export class FreeFunction extends TypeDeclaration implements CallableDeclaration, Exported {
public readonly returnType: Type;
public readonly kind = DeclarationKind.Function;
public readonly parameters = new Array<Parameter>();
Expand Down
7 changes: 3 additions & 4 deletions packages/@cdklabs/typewriter/src/renderer/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import {
import { StructType } from '../struct';
import { ThingSymbol } from '../symbol';
import { PrimitiveType, Type } from '../type';
import { TypeParameterSpec } from '../type-declaration';
import { Exported, TypeParameterSpec } from '../type-declaration';
import { Initializer, MemberVisibility, Method } from '../type-member';

/**
Expand Down Expand Up @@ -431,11 +431,10 @@ export class TypeScriptRenderer extends Renderer {
throw new Error(`Symbol ${sym} (in ${sym.scope}) not visible from ${this.scopes[0]} (missing import?)`);
}

protected renderFunction(func: CallableDeclaration) {
protected renderFunction(func: CallableDeclaration & Exported) {
this.renderDocs(func);
this.emit(`// @ts-ignore TS6133\n`);

this.emit(`function ${func.name}`);
this.emit(`${func.exported ? 'export ' : ''}function ${func.name}`);
this.renderParameters(func.parameters);
if (func.returnType) {
this.emit(': ');
Expand Down
6 changes: 5 additions & 1 deletion packages/@cdklabs/typewriter/src/type-declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export interface Exportable {
export?: boolean;
}

export interface Exported {
exported?: boolean;
}

export interface TypeParameterSpec {
readonly name: string;
readonly extendsType?: Type;
Expand Down Expand Up @@ -52,7 +56,7 @@ export interface TypeSpec extends Documented, Exportable {
/**
* An abstract jsii type
*/
export abstract class TypeDeclaration implements Documented {
export abstract class TypeDeclaration implements Documented, Exported {
/**
* The simple name of the type (MyClass).
*/
Expand Down
48 changes: 48 additions & 0 deletions packages/@cdklabs/typewriter/test/exports.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { FreeFunction, Module, TypeScriptRenderer } from '../src';

const renderer = new TypeScriptRenderer();
let scope: Module;

beforeEach(() => {
scope = new Module('typewriter.test');
});

describe('functions', () => {
test('functions are implicitly not exported', () => {
new FreeFunction(scope, {
name: 'freeFunction',
});

expect(renderer.render(scope)).toMatchInlineSnapshot(`
"/* eslint-disable prettier/prettier, max-len */
// @ts-ignore TS6133
function freeFunction(): void;"
`);
});

test('functions can be explicitly exported', () => {
new FreeFunction(scope, {
name: 'freeFunction',
export: true,
});

expect(renderer.render(scope)).toMatchInlineSnapshot(`
"/* eslint-disable prettier/prettier, max-len */
// @ts-ignore TS6133
export function freeFunction(): void;"
`);
});

test('functions can be explicitly not exported', () => {
new FreeFunction(scope, {
name: 'freeFunction',
export: false,
});

expect(renderer.render(scope)).toMatchInlineSnapshot(`
"/* eslint-disable prettier/prettier, max-len */
// @ts-ignore TS6133
function freeFunction(): void;"
`);
});
});

0 comments on commit 83ec1c3

Please sign in to comment.