Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed adding invalid as arg to empty callfunc invocations by default #1043

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 a single 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.",
markwpearce marked this conversation as resolved.
Show resolved Hide resolved
"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 a single argument in callfunc() invocations.
markwpearce marked this conversation as resolved.
Show resolved Hide resolved
* 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