diff --git a/src/files/BrsFile.spec.ts b/src/files/BrsFile.spec.ts index dfac05a94..41e141867 100644 --- a/src/files/BrsFile.spec.ts +++ b/src/files/BrsFile.spec.ts @@ -2205,6 +2205,19 @@ describe('BrsFile', () => { }); describe('transpile', () => { + it('namespaced functions default param values in d.bs files are transpiled correctly', () => { + testGetTypedef(` + namespace alpha + function beta() + end function + function charlie(fn = alpha.beta, fn2 = beta) + end function + end namespace + function delta(fn = alpha.beta) + end function + `); + }); + describe('null tokens', () => { it('succeeds when token locations are omitted', () => { doTest(` diff --git a/src/parser/AstNode.ts b/src/parser/AstNode.ts index a4e00f0ba..17c63731e 100644 --- a/src/parser/AstNode.ts +++ b/src/parser/AstNode.ts @@ -20,6 +20,13 @@ export abstract class AstNode { public abstract transpile(state: BrsTranspileState): TranspileResult; + /** + * Get the typedef for this node. (defaults to transpiling the node, should be overridden by subclasses if there's a more specific typedef requirement) + */ + public getTypedef(state: BrsTranspileState) { + return this.transpile(state); + } + /** * When being considered by the walk visitor, this describes what type of element the current class is. */ diff --git a/src/parser/Expression.ts b/src/parser/Expression.ts index f38e0829a..4388989cf 100644 --- a/src/parser/Expression.ts +++ b/src/parser/Expression.ts @@ -460,14 +460,12 @@ export class FunctionParameterExpression extends Expression { const results = [this.name.text] as TranspileResult; if (this.defaultValue) { - results.push(' = ', ...this.defaultValue.transpile(state)); + results.push(' = ', ...(this.defaultValue.getTypedef(state) ?? this.defaultValue.transpile(state))); } if (this.asToken) { results.push(' as '); - // TODO: Is this conditional needed? Will typeToken always exist - // so long as `asToken` exists? if (this.typeToken) { results.push(this.typeToken.text); } @@ -581,6 +579,15 @@ export class DottedGetExpression extends Expression { } } + getTypedef(state: BrsTranspileState) { + //always transpile the dots for typedefs + return [ + ...this.obj.transpile(state), + state.transpileToken(this.dot), + state.transpileToken(this.name) + ]; + } + walk(visitor: WalkVisitor, options: WalkOptions) { if (options.walkMode & InternalWalkMode.walkExpressions) { walk(this, 'obj', visitor, options); @@ -1132,6 +1139,12 @@ export class VariableExpression extends Expression { return result; } + getTypedef(state: BrsTranspileState) { + return [ + state.transpileToken(this.name) + ]; + } + walk(visitor: WalkVisitor, options: WalkOptions) { //nothing to walk }