Skip to content

Commit

Permalink
Changed adding invalid as arg to empty callfunc invocations by default (
Browse files Browse the repository at this point in the history
#1043)

* Changed adding invalid as arg to empty callfunc invocations by default

* Update src/BsConfig.ts

Co-authored-by: Bronley Plumb <bronley@gmail.com>

* Update bsconfig.schema.json

Co-authored-by: Bronley Plumb <bronley@gmail.com>

---------

Co-authored-by: Bronley Plumb <bronley@gmail.com>
  • Loading branch information
markwpearce and TwitchBronBron authored Jan 31, 2024
1 parent f2fefd3 commit 5e2c6ce
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
5 changes: 5 additions & 0 deletions bsconfig.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@
"description": "Allow brighterscript features (classes, interfaces, etc...) to be included in BrightScript (`.brs`) files, and force those files to be transpiled.",
"type": "boolean",
"default": false
},
"legacyCallfuncHandling": {
"description": "Legacy RokuOS versions required at least one argument in callfunc() invocations. Previous brighterscript versions handled this by inserting invalid as an argument when no other args are present. This is not necessary in modern RokuOS versions.",
"type": "boolean",
"default": false
}
}
}
6 changes: 6 additions & 0 deletions src/BsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ export interface BsConfig {
* scripts inside `source` that depend on bslib.brs. Defaults to `source`.
*/
bslibDestinationDir?: string;

/* Legacy RokuOS versions required at least one argument in callfunc() invocations.
* Previous brighterscript versions handled this by inserting invalid as an argument when no other args are present.
* This is not necessary in modern RokuOS versions.
*/
legacyCallfuncHandling?: boolean;
}

type OptionalBsConfigFields =
Expand Down
21 changes: 20 additions & 1 deletion src/files/BrsFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2613,7 +2613,8 @@ describe('BrsFile', () => {
expectZeroDiagnostics(program);
});

it('sets invalid on empty callfunc', async () => {
it('sets invalid on empty callfunc with legacyCallfuncHandling=true', async () => {
program.options.legacyCallfuncHandling = true;
await testTranspile(`
sub main()
node = invalid
Expand All @@ -2631,6 +2632,24 @@ describe('BrsFile', () => {
`);
});

it('empty callfunc allowed by default', async () => {
await testTranspile(`
sub main()
node = invalid
node@.doSomething()
m.top.node@.doSomething()
m.top.node@.doSomething(1)
end sub
`, `
sub main()
node = invalid
node.callfunc("doSomething")
m.top.node.callfunc("doSomething")
m.top.node.callfunc("doSomething", 1)
end sub
`);
});

it('includes original arguments', async () => {
await testTranspile(`
sub main()
Expand Down
14 changes: 7 additions & 7 deletions src/parser/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1140,14 +1140,11 @@ export class CallfuncExpression extends Expression {
state.sourceNode(this.operator, '.callfunc'),
state.transpileToken(this.openingParen),
//the name of the function
state.sourceNode(this.methodName, ['"', this.methodName.text, '"']),
', '
state.sourceNode(this.methodName, ['"', this.methodName.text, '"'])
);
//transpile args
//callfunc with zero args never gets called, so pass invalid as the first parameter if there are no args
if (this.args.length === 0) {
result.push('invalid');
} else {
if (this.args?.length > 0) {
result.push(', ');
//transpile args
for (let i = 0; i < this.args.length; i++) {
//add comma between args
if (i > 0) {
Expand All @@ -1156,7 +1153,10 @@ export class CallfuncExpression extends Expression {
let arg = this.args[i];
result.push(...arg.transpile(state));
}
} else if (state.options.legacyCallfuncHandling) {
result.push(', ', 'invalid');
}

result.push(
state.transpileToken(this.closingParen)
);
Expand Down
3 changes: 2 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ export class Util {
emitDefinitions: config.emitDefinitions === true ? true : false,
removeParameterTypes: config.removeParameterTypes === true ? true : false,
logLevel: logLevel,
bslibDestinationDir: bslibDestinationDir
bslibDestinationDir: bslibDestinationDir,
legacyCallfuncHandling: config.legacyCallfuncHandling === true ? true : false
};

//mutate `config` in case anyone is holding a reference to the incomplete one
Expand Down

0 comments on commit 5e2c6ce

Please sign in to comment.