Skip to content

Commit

Permalink
Added var tracking for #if statements
Browse files Browse the repository at this point in the history
  • Loading branch information
markwpearce committed Sep 13, 2024
1 parent b3181a8 commit 5f98714
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/plugins/trackCodeFlow/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,28 @@ describe('trackCodeFlow', () => {
expect(actual).deep.equal(expected);
});

it('implements assign-all-paths with conditional compilation', async () => {
const diagnostics = await linter.run({
...project1,
files: ['source/assign-all-paths-conditional-compilation.brs'],
rules: {
'assign-all-paths': 'error',
'consistent-return': 'off',
'unused-variable': 'off'
},
diagnosticFilters: [1001, 1090]
} as any);
const actual = fmtDiagnostics(diagnostics);
const expected = [
`15:LINT1003:Not all the code paths assign 'a'`,
`23:LINT1003:Not all the code paths assign 'a'`,
`42:LINT1003:Not all the code paths assign 'a'`,
`65:LINT1003:Not all the code paths assign 'a'`,
`76:LINT1003:Not all the code paths assign 'a'`
];
expect(actual).deep.equal(expected);
});

it('report errors for classes', async () => {
const diagnostics = await linter.run({
...project1,
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/trackCodeFlow/varTracking.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BscFile, FunctionExpression, BsDiagnostic, Range, isForStatement, isForEachStatement, isIfStatement, isAssignmentStatement, isNamespaceStatement, NamespaceStatement, Expression, isVariableExpression, isBinaryExpression, TokenKind, Scope, CallableContainerMap, DiagnosticSeverity, isLiteralInvalid, isWhileStatement, isCatchStatement, isLabelStatement, isGotoStatement, ParseMode, util, isMethodStatement, isTryCatchStatement } from 'brighterscript';
import { BscFile, FunctionExpression, BsDiagnostic, Range, isForStatement, isForEachStatement, isIfStatement, isAssignmentStatement, isNamespaceStatement, NamespaceStatement, Expression, isVariableExpression, isBinaryExpression, TokenKind, Scope, CallableContainerMap, DiagnosticSeverity, isLiteralInvalid, isWhileStatement, isCatchStatement, isLabelStatement, isGotoStatement, ParseMode, util, isMethodStatement, isTryCatchStatement, isConditionalCompileStatement } from 'brighterscript';
import { LintState, StatementInfo, NarrowingInfo, VarInfo, VarRestriction } from '.';
import { PluginContext } from '../../util';
import { Location } from 'vscode-languageserver-types';
Expand Down Expand Up @@ -280,7 +280,7 @@ export function createVarLinter(
if (!parent.locals) {
parent.locals = locals;
} else {
const isParentBranched = isIfStatement(parent.stat) || isTryCatchStatement(parent.stat);
const isParentBranched = isIfStatement(parent.stat) || isTryCatchStatement(parent.stat) || isConditionalCompileStatement(parent.stat);
const isLoop = isForStatement(closed.stat) || isForEachStatement(closed.stat) || isWhileStatement(closed.stat);
locals.forEach((local, name) => {
const parentLocal = parent.locals.get(name);
Expand Down
77 changes: 77 additions & 0 deletions test/project1/source/assign-all-paths-conditional-compilation.brs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
function assignPathsCC() as dynamic
#if true
a = 1
#else
a = 2
#end if
return a ' no error
end function


function assignPathsCC1() as dynamic
#if true
a = 1
#end if
return a ' not really an error because #else block will never be run, but displays one anyway. Maybe fix this
end function


function assignPathsCC2() as dynamic
#if SOME_VAR
a = 1
#end if
return a ' error
end function


function assignPathsCC3() as dynamic
#if SOME_VAR
a = 1
#else
a = 2
#end if
return a ' no error
end function

function assignPathsCC4() as dynamic
#if SOME_VAR
b = 1
#else
a = 2
#end if
return a ' error
end function


function assignPathsCC5() as dynamic
#if SOME_VAR
a = 1
#else if SOME_VAR2
a = 2
#else
a = 3
#end if
return a ' No error
end function

function assignPathsCC6() as dynamic
#if SOME_VAR
a = 1
#else if SOME_VAR2
a = 2
#else
' missing assignment
#end if
return a ' error
end function

function assignPathsCC7() as dynamic
#if SOME_VAR
a = 1
#else if SOME_VAR2
' missing assignment
#else
a = 2
#end if
return a ' error
end function

0 comments on commit 5f98714

Please sign in to comment.