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

Ensures diagnostic on using Variable from parent function in an inner function #1369

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: 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
Loading