Skip to content

Commit

Permalink
Added function call validation (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
markwpearce authored Jul 14, 2023
1 parent 08a0b94 commit 09975f2
Show file tree
Hide file tree
Showing 8 changed files with 597 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/DiagnosticMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,11 @@ export let DiagnosticMessages = {
message: `'${typeText}' cannot be used as a type`,
code: 1140,
severity: DiagnosticSeverity.Error
}),
argumentTypeMismatch: (actualTypeString: string, expectedTypeString: string) => ({
message: `Argument of type '${actualTypeString}' is not compatible with parameter of type '${expectedTypeString}'`,
code: 1141,
severity: DiagnosticSeverity.Error
})
};

Expand Down
5 changes: 4 additions & 1 deletion src/Scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,10 @@ export class Scope {

//do many per-file checks
this.enumerateBrsFiles((file) => {
this.diagnosticDetectFunctionCallsWithWrongParamCount(file, callableContainerMap);
if (!this.program.options.enableTypeValidation) {
//TODO: this is replaced by ScopeValidator.validateFunctionCalls() when enableTypeValidation is true
this.diagnosticDetectFunctionCallsWithWrongParamCount(file, callableContainerMap);
}
this.diagnosticDetectShadowedLocalVars(file, callableContainerMap);
this.diagnosticDetectFunctionCollisions(file);
this.detectVariableNamespaceCollisions(file);
Expand Down
15 changes: 11 additions & 4 deletions src/bscPlugin/validation/BrsFileValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,15 @@ export class BrsFileValidator {
const visitor = createVisitor({
MethodStatement: (node) => {
//add the `super` symbol to class methods
//Todo: get the actual type of the parent class
node.func.body.symbolTable.addSymbol('super', undefined, DynamicType.instance, SymbolTypeFlag.runtime);
let superType: BscType = DynamicType.instance;
if (isClassStatement(node.parent) && node.parent.hasParentClass()) {
if (this.event.program.options.enableTypeValidation) {
const parentClassType = node.parent.parentClassName.getType({ flags: SymbolTypeFlag.typetime });
const methodName = node.getName(ParseMode.BrighterScript);
superType = parentClassType.getMemberType(methodName, { flags: SymbolTypeFlag.runtime });
}
node.func.body.symbolTable.addSymbol('super', undefined, superType, SymbolTypeFlag.runtime);
}
},
CallfuncExpression: (node) => {
if (node.args.length > 5) {
Expand Down Expand Up @@ -98,7 +105,7 @@ export class BrsFileValidator {
},
FunctionStatement: (node) => {
this.validateDeclarationLocations(node, 'function', () => util.createBoundingRange(node.func.functionType, node.name));
const funcType = node.getType({ flags: SymbolTypeFlag.typetime });
const funcType = this.getTypeFromNode(node, { flags: SymbolTypeFlag.typetime });

if (node.name?.text) {
node.parent.getSymbolTable().addSymbol(
Expand Down Expand Up @@ -137,7 +144,7 @@ export class BrsFileValidator {
FunctionParameterExpression: (node) => {
const paramName = node.name?.text;
const symbolTable = node.getSymbolTable();
const nodeType = node.getType({ flags: SymbolTypeFlag.typetime });
const nodeType = this.getTypeFromNode(node, { flags: SymbolTypeFlag.typetime });
symbolTable?.addSymbol(paramName, node.name.range, nodeType, SymbolTypeFlag.runtime);
},
InterfaceStatement: (node) => {
Expand Down
Loading

0 comments on commit 09975f2

Please sign in to comment.