From 299cde530f2006246c5f43c1095927605992a3fc Mon Sep 17 00:00:00 2001 From: Antti Ajanki Date: Fri, 20 Sep 2024 20:11:15 +0300 Subject: [PATCH] Accept but ignore "declare function" --- src/transpiler/index.ts | 6 ++++-- src/transpiler/statements.ts | 4 +++- test/transpiler.spec.ts | 31 +++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/transpiler/index.ts b/src/transpiler/index.ts index 1ff6679..a5990aa 100644 --- a/src/transpiler/index.ts +++ b/src/transpiler/index.ts @@ -19,6 +19,7 @@ const { ImportNamespaceSpecifier, Literal, Program, + TSDeclareFunction, TSTypeAliasDeclaration, TSInterfaceDeclaration, } = AST_NODE_TYPES @@ -69,12 +70,13 @@ function parseTopLevelStatement(node: any): SubworkflowAST[] { case TSInterfaceDeclaration: case TSTypeAliasDeclaration: - // Ignore "type" and "interface" declarations at the top-level + case TSDeclareFunction: + // Ignore "type", "interface" and "declare function" at the top-level return [] default: throw new WorkflowSyntaxError( - `Only function declarations, imports and type aliases allowed at the top level, encountered ${node?.type}`, + `Only function definitions, imports and type aliases allowed at the top level, encountered ${node?.type}`, node?.loc, ) } diff --git a/src/transpiler/statements.ts b/src/transpiler/statements.ts index f5cff7d..f88cfa5 100644 --- a/src/transpiler/statements.ts +++ b/src/transpiler/statements.ts @@ -67,6 +67,7 @@ const { SwitchStatement, ThrowStatement, TryStatement, + TSDeclareFunction, TSTypeAliasDeclaration, TSInterfaceDeclaration, VariableDeclaration, @@ -155,7 +156,8 @@ function parseStep(node: any, ctx: ParsingContext): WorkflowStepAST[] { case TSInterfaceDeclaration: case TSTypeAliasDeclaration: - // Ignore "type" and "interface" declarations + case TSDeclareFunction: + // Ignore "type", "interface" and "declare function" return [] default: diff --git a/test/transpiler.spec.ts b/test/transpiler.spec.ts index 3492056..269dc05 100644 --- a/test/transpiler.spec.ts +++ b/test/transpiler.spec.ts @@ -69,6 +69,26 @@ describe('Type annotations', () => { expect(observed).to.deep.equal(expected) }) + + it('ignores interface declaration', () => { + const code = ` + interface Person { + name: string + }` + const observed = YAML.parse(transpile(code)) as unknown + + expect(observed).to.deep.equal({}) + }) + + it('ignores type declaration', () => { + const code = ` + type Person = { + name: string + }` + const observed = YAML.parse(transpile(code)) as unknown + + expect(observed).to.deep.equal({}) + }) }) describe('Function definition', () => { @@ -116,6 +136,17 @@ describe('Function definition', () => { expect(observed).to.deep.equal(expected) }) + it('ignores function declaration', () => { + const code = ` + declare function computeIt() + export declare function exportedComputation() + ` + + const observed = YAML.parse(transpile(code)) as unknown + + expect(observed).to.deep.equal({}) + }) + it('throws if function is defined in a nested scope', () => { const code = ` function main() {