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

feat: allow free function exports in typewriter #1379

Merged
merged 22 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 15 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
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, Exportable, 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, Exportable {
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 { Exportable, 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 & Exportable) {
this.renderDocs(func);
this.emit(`// @ts-ignore TS6133\n`);

this.emit(`function ${func.name}`);
this.emit(`${func.export ? 'export ' : ''}function ${func.name}`);
this.renderParameters(func.parameters);
if (func.returnType) {
this.emit(': ');
Expand Down
9 changes: 8 additions & 1 deletion packages/@cdklabs/typewriter/src/type-declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export interface TypeSpec extends Documented, Exportable {
/**
* An abstract jsii type
*/
export abstract class TypeDeclaration implements Documented {
export abstract class TypeDeclaration implements Documented, Exportable {
/**
* The simple name of the type (MyClass).
*/
Expand Down Expand Up @@ -83,6 +83,13 @@ export abstract class TypeDeclaration implements Documented {
return !!this.spec.export;
}

/**
* Whether this type is being exported from its scope
*/
public get export() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now we have two things that do the same. I rather have 2 different interface.

return !!this.spec.export;
}

mrgrain marked this conversation as resolved.
Show resolved Hide resolved
/**
* The generic type parameters of the type
*/
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;"
`);
});
});
Loading