Skip to content

Commit

Permalink
Accept but ignore "declare function"
Browse files Browse the repository at this point in the history
  • Loading branch information
aajanki committed Sep 20, 2024
1 parent 6ceaec7 commit 299cde5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/transpiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const {
ImportNamespaceSpecifier,
Literal,
Program,
TSDeclareFunction,
TSTypeAliasDeclaration,
TSInterfaceDeclaration,
} = AST_NODE_TYPES
Expand Down Expand Up @@ -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,
)
}
Expand Down
4 changes: 3 additions & 1 deletion src/transpiler/statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const {
SwitchStatement,
ThrowStatement,
TryStatement,
TSDeclareFunction,
TSTypeAliasDeclaration,
TSInterfaceDeclaration,
VariableDeclaration,
Expand Down Expand Up @@ -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:
Expand Down
31 changes: 31 additions & 0 deletions test/transpiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 299cde5

Please sign in to comment.