Skip to content

Commit

Permalink
Check if in TypeExpression before doing VariableExpression checks (#122)
Browse files Browse the repository at this point in the history
* Check for being in a type expression before doing VariableExpression checks

* Added tests for typecasts
  • Loading branch information
markwpearce authored Jul 26, 2024
1 parent 6380ed5 commit 4581ee9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/plugins/trackCodeFlow/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,34 @@ describe('trackCodeFlow', () => {
expect(actual).deep.equal(expected);
});

it('does not mark inline anonymous functions param types as uninitialised vars', async () => {
const diagnostics = await linter.run({
...project1,
files: ['source/inline-functions.bs'],
rules: {
'unused-variable': 'error'
},
diagnosticFilters: []
} as any);
const actual = fmtDiagnostics(diagnostics);
const expected = [];
expect(actual).deep.equal(expected);
});

it('does not mark typecasts as uninitialised vars', async () => {
const diagnostics = await linter.run({
...project1,
files: ['source/typecast-expressions.bs'],
rules: {
'unused-variable': 'error'
},
diagnosticFilters: []
} as any);
const actual = fmtDiagnostics(diagnostics);
const expected = [];
expect(actual).deep.equal(expected);
});

describe('does not mark enums as uninitialised vars', () => {
it('in a regular file', async () => {
const diagnostics = await linter.run({
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/trackCodeFlow/varTracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ export function createVarLinter(
}

function visitExpression(expr: Expression, parent: Expression, curr: StatementInfo) {
if (isVariableExpression(expr)) {
if (isVariableExpression(expr) && !util.isInTypeExpression(expr)) {
const name = expr.tokens.name.text;
if (name === 'm') {
return;
Expand Down
10 changes: 10 additions & 0 deletions test/project1/source/inline-functions.bs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
sub main()
takesFunction(function(input as object) as string
return "Hello, " + input.name + "!"
end function)
end sub


sub takesFunction(anonFunc as function)
print anonFunc({name: "Hello, World!"})
end sub
14 changes: 14 additions & 0 deletions test/project1/source/typecast-expression.bs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
typecast m as MyTypeCast



sub main(input)
print m.name
print (input as myTypeCast).name
print (input as dynamic).name
end sub


interface MyTypeCast
name as string
end interface

0 comments on commit 4581ee9

Please sign in to comment.