Skip to content

Commit 8d6a4db

Browse files
Fix typdef generation of default param func (#1551)
1 parent 1db1aba commit 8d6a4db

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/files/BrsFile.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,6 +2205,19 @@ describe('BrsFile', () => {
22052205
});
22062206

22072207
describe('transpile', () => {
2208+
it('namespaced functions default param values in d.bs files are transpiled correctly', () => {
2209+
testGetTypedef(`
2210+
namespace alpha
2211+
function beta()
2212+
end function
2213+
function charlie(fn = alpha.beta, fn2 = beta)
2214+
end function
2215+
end namespace
2216+
function delta(fn = alpha.beta)
2217+
end function
2218+
`);
2219+
});
2220+
22082221
describe('null tokens', () => {
22092222
it('succeeds when token locations are omitted', () => {
22102223
doTest(`

src/parser/AstNode.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ export abstract class AstNode {
2020

2121
public abstract transpile(state: BrsTranspileState): TranspileResult;
2222

23+
/**
24+
* Get the typedef for this node. (defaults to transpiling the node, should be overridden by subclasses if there's a more specific typedef requirement)
25+
*/
26+
public getTypedef(state: BrsTranspileState) {
27+
return this.transpile(state);
28+
}
29+
2330
/**
2431
* When being considered by the walk visitor, this describes what type of element the current class is.
2532
*/

src/parser/Expression.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,14 +460,12 @@ export class FunctionParameterExpression extends Expression {
460460
const results = [this.name.text] as TranspileResult;
461461

462462
if (this.defaultValue) {
463-
results.push(' = ', ...this.defaultValue.transpile(state));
463+
results.push(' = ', ...(this.defaultValue.getTypedef(state) ?? this.defaultValue.transpile(state)));
464464
}
465465

466466
if (this.asToken) {
467467
results.push(' as ');
468468

469-
// TODO: Is this conditional needed? Will typeToken always exist
470-
// so long as `asToken` exists?
471469
if (this.typeToken) {
472470
results.push(this.typeToken.text);
473471
}
@@ -581,6 +579,15 @@ export class DottedGetExpression extends Expression {
581579
}
582580
}
583581

582+
getTypedef(state: BrsTranspileState) {
583+
//always transpile the dots for typedefs
584+
return [
585+
...this.obj.transpile(state),
586+
state.transpileToken(this.dot),
587+
state.transpileToken(this.name)
588+
];
589+
}
590+
584591
walk(visitor: WalkVisitor, options: WalkOptions) {
585592
if (options.walkMode & InternalWalkMode.walkExpressions) {
586593
walk(this, 'obj', visitor, options);
@@ -1132,6 +1139,12 @@ export class VariableExpression extends Expression {
11321139
return result;
11331140
}
11341141

1142+
getTypedef(state: BrsTranspileState) {
1143+
return [
1144+
state.transpileToken(this.name)
1145+
];
1146+
}
1147+
11351148
walk(visitor: WalkVisitor, options: WalkOptions) {
11361149
//nothing to walk
11371150
}

0 commit comments

Comments
 (0)