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 11 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
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 @@ -54,7 +54,7 @@ import {
import { StructType } from '../struct';
import { ThingSymbol } from '../symbol';
import { PrimitiveType, Type } from '../type';
import { TypeParameterSpec } from '../type-declaration';
import { TypeDeclaration, TypeParameterSpec } from '../type-declaration';
import { Initializer, MemberVisibility, Method } from '../type-member';

export class TypeScriptRenderer extends Renderer {
Expand Down Expand Up @@ -411,11 +411,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 & TypeDeclaration) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this is correct, we want to accept all CallableDeclarations not, just the ones that are also a TypeDeclaration.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we keep the signature as it is.

Then w can have a guard isExported(x): is { exported: true } { ... } to check if exported exists and is true.

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
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