Skip to content

Commit

Permalink
Merge branch 'master' into transpile-event-tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron committed Feb 2, 2022
2 parents d49aa2e + 76f528c commit 97f3bae
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/files/BrsFile.spec.ts
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
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
@@ -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
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
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

0 comments on commit 97f3bae

Please sign in to comment.