Skip to content

Commit

Permalink
Ensures diagnostic on using parent function var in inner func (#1369)
Browse files Browse the repository at this point in the history
  • Loading branch information
markwpearce authored Dec 13, 2024
1 parent ae32da2 commit f0304f7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/bscPlugin/validation/BrsFileValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ export class BrsFileValidator {
FunctionExpression: (node) => {
const funcSymbolTable = node.getSymbolTable();
const isInlineFunc = !(isFunctionStatement(node.parent) || isMethodStatement(node.parent));
if (isInlineFunc) {
// symbol table should not include any symbols from parent func
funcSymbolTable.pushParentProvider(() => node.findAncestor<Body>(isBody).getSymbolTable());
}
if (!funcSymbolTable?.hasSymbol('m', SymbolTypeFlag.runtime) || isInlineFunc) {
if (!isTypecastStatement(node.body?.statements?.[0])) {
funcSymbolTable?.addSymbol('m', { isInstance: true }, new AssociativeArrayType(), SymbolTypeFlag.runtime);
Expand Down
44 changes: 44 additions & 0 deletions src/bscPlugin/validation/ScopeValidator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1960,6 +1960,50 @@ describe('ScopeValidator', () => {
program.validate();
expectZeroDiagnostics(program);
});

it('has an diagnostic when using a variable defined in parent function', () => {
program.setFile('source/main.bs', `
function parentFunction()
parentVar = "test"
innerFunction = sub()
' Attempting to use parentVar from the parent function scope
print parentVar ' This should trigger a diagnostic
otherFunc() ' this is fine
end sub
innerFunction()
end function
sub otherFunc()
print "hello"
end sub
`);

program.validate();
expectDiagnostics(program, [
DiagnosticMessages.cannotFindName('parentVar')
]);
});

it('has an diagnostic when using a param from parent function', () => {
program.setFile('source/main.bs', `
function parentFunction(outerVal)
parentVar = "test"
innerFunction = sub(inner)
print inner + outer
end sub
innerFunction(2)
end function
`);

program.validate();
expectDiagnostics(program, [
DiagnosticMessages.cannotFindName('outer')
]);
});
});

describe('itemCannotBeUsedAsVariable', () => {
Expand Down

0 comments on commit f0304f7

Please sign in to comment.