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

Fix bug with optional parameters #501

Merged
merged 1 commit into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/files/BrsFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1584,7 +1584,7 @@ describe('BrsFile', () => {
(await program.getHover(file.pathAbsolute, Position.create(2, 30))).contents
).to.equal([
'```brightscript',
'function UCase(s? as string) as string',
'function UCase(s as string) as string',
'```'
].join('\n'));
});
Expand All @@ -1601,7 +1601,7 @@ describe('BrsFile', () => {
).to.equal([
'```brightscript',
//TODO this really shouldn't be returning the global function, but it does...so make sure it doesn't crash right now.
'function Instr(start? as integer, text? as string, substring? as string) as integer',
'function Instr(start as integer, text as string, substring as string) as integer',
'```'
].join('\n'));
});
Expand Down
8 changes: 4 additions & 4 deletions src/files/BrsFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,9 @@ export class BrsFile {

functionType.setName(assignment.name.text);
for (let param of assignment.value.parameters) {
let isRequired = !param.defaultValue;
let isOptional = !!param.defaultValue;
//TODO compute optional parameters
functionType.addParameter(param.name.text, param.type, isRequired);
functionType.addParameter(param.name.text, param.type, isOptional);
}
return functionType;

Expand Down Expand Up @@ -573,8 +573,8 @@ export class BrsFile {
isRestArgument: false
};
params.push(callableParam);
let isRequired = !param.defaultValue;
functionType.addParameter(callableParam.name, callableParam.type, isRequired);
let isOptional = !!param.defaultValue;
functionType.addParameter(callableParam.name, callableParam.type, isOptional);
}

this.callables.push({
Expand Down
30 changes: 29 additions & 1 deletion src/globalCallables.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { standardizePath as s } from './util';
import util, { standardizePath as s } from './util';
import { Program } from './Program';
import { expectDiagnostics, expectZeroDiagnostics } from './testHelpers.spec';
import { DiagnosticMessages } from './DiagnosticMessages';
import { expect } from 'chai';

let tmpPath = s`${process.cwd()}/.tmp`;
let rootDir = s`${tmpPath}/rootDir`;
Expand Down Expand Up @@ -43,6 +44,33 @@ describe('globalCallables', () => {
]);
});

it('handles optional params properly', () => {
program.addOrReplaceFile('source/main.brs', `
sub main()
print Mid("value1", 1) 'third param is optional
end sub
`);
program.validate();
expectZeroDiagnostics(program);
});

it('hover shows correct for optional params', async () => {
const file = program.addOrReplaceFile('source/main.brs', `
sub main()
print Mid("value1", 1)
end sub
`);
program.validate();
const hover = await program.getHover(file.pathAbsolute, util.createPosition(2, 25));
expect(
hover.contents.toString().replace('\r\n', '\n')
).to.eql([
'```brightscript',
'function Mid(s as string, p as integer, n? as integer) as string',
'```'
].join('\n'));
});

describe('bslCore', () => {
it('exists', () => {
program.addOrReplaceFile('source/main.brs', `
Expand Down
6 changes: 3 additions & 3 deletions src/types/FunctionType.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ describe('FunctionType', () => {

//different parameter count
expect(
new FunctionType(new VoidType()).addParameter('a', new IntegerType(), true).isAssignableTo(
new FunctionType(new VoidType()).addParameter('a', new IntegerType(), false).isAssignableTo(
new FunctionType(new VoidType())
)
).to.be.false;

//different parameter types
expect(
new FunctionType(new VoidType()).addParameter('a', new IntegerType(), true).isAssignableTo(
new FunctionType(new VoidType()).addParameter('a', new StringType(), true)
new FunctionType(new VoidType()).addParameter('a', new IntegerType(), false).isAssignableTo(
new FunctionType(new VoidType()).addParameter('a', new StringType(), false)
)
).to.be.false;

Expand Down
8 changes: 4 additions & 4 deletions src/types/FunctionType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ export class FunctionType implements BscType {
*/
public isSub = false;

public params = [] as Array<{ name: string; type: BscType; isRequired: boolean }>;
public params = [] as Array<{ name: string; type: BscType; isOptional: boolean }>;

public setName(name: string) {
this.name = name;
return this;
}

public addParameter(name: string, type: BscType, isRequired: boolean) {
public addParameter(name: string, type: BscType, isOptional: boolean) {
this.params.push({
name: name,
type: type,
isRequired: isRequired === false ? false : true
isOptional: isOptional === true ? true : false
});
return this;
}
Expand Down Expand Up @@ -67,7 +67,7 @@ export class FunctionType implements BscType {
public toString() {
let paramTexts = [];
for (let param of this.params) {
paramTexts.push(`${param.name}${param.isRequired ? '' : '?'} as ${param.type.toString()}`);
paramTexts.push(`${param.name}${param.isOptional ? '?' : ''} as ${param.type.toString()}`);
}
return `${this.isSub ? 'sub' : 'function'} ${this.name}(${paramTexts.join(', ')}) as ${this.returnType.toString()}`;

Expand Down