diff --git a/src/plugins/trackCodeFlow/index.spec.ts b/src/plugins/trackCodeFlow/index.spec.ts index 065bc14..a7d0a5a 100644 --- a/src/plugins/trackCodeFlow/index.spec.ts +++ b/src/plugins/trackCodeFlow/index.spec.ts @@ -54,6 +54,45 @@ describe('trackCodeFlow', () => { expect(actual).deep.equal(expected); }); + describe('does not mark enums as uninitialised vars', () => { + it('in a regular file', async () => { + const diagnostics = await linter.run({ + ...project1, + files: ['source/enums.bs'], + rules: { + 'unused-variable': 'error' + } + }); + const actual = fmtDiagnostics(diagnostics); + const expected = []; + expect(actual).deep.equal(expected); + }); + it('inside a class', async () => { + const diagnostics = await linter.run({ + ...project1, + files: ['source/enum-in-class.bs'], + rules: { + 'unused-variable': 'error' + } + }); + const actual = fmtDiagnostics(diagnostics); + const expected = []; + expect(actual).deep.equal(expected); + }); + it('inside a namespace', async () => { + const diagnostics = await linter.run({ + ...project1, + files: ['source/enum-in-namespace.bs'], + rules: { + 'unused-variable': 'error' + } + }); + const actual = fmtDiagnostics(diagnostics); + const expected = []; + expect(actual).deep.equal(expected); + }); + }); + it('implements assign-all-paths', async () => { const diagnostics = await linter.run({ ...project1, diff --git a/src/plugins/trackCodeFlow/varTracking.ts b/src/plugins/trackCodeFlow/varTracking.ts index 25e1c3f..1d1f4af 100644 --- a/src/plugins/trackCodeFlow/varTracking.ts +++ b/src/plugins/trackCodeFlow/varTracking.ts @@ -368,16 +368,17 @@ function deferredVarLinter( deferred: ValidationInfo[], diagnostics: BsDiagnostic[] ) { - // lookups for namespaces and classes + // lookups for namespaces, classes, and enums + // to add them to the topLevel so that they don't get marked as unused. const toplevel = new Set(globals); scope.getAllNamespaceStatements().forEach(ns => { toplevel.add(ns.name.toLowerCase().split('.')[0]); // keep root of namespace }); scope.getClassMap().forEach(cls => { - const name = cls.item.name.text.toLowerCase(); - if (!cls.item.namespaceName) { - toplevel.add(name); - } + toplevel.add(cls.item.name.text.toLowerCase()); + }); + scope.getEnumMap().forEach(enm => { + toplevel.add(enm.item.name.toLowerCase()); }); if (isBrsFile(file)) { file.parser.references.classStatements.forEach(cls => { diff --git a/test/project1/source/enum-in-class.bs b/test/project1/source/enum-in-class.bs new file mode 100644 index 0000000..5ad9149 --- /dev/null +++ b/test/project1/source/enum-in-class.bs @@ -0,0 +1,10 @@ +class MyClass + public value + public sub new() + enum MyEnum + val1 + val2 + end enum + m.value = MyEnum.val1 + end sub +end class diff --git a/test/project1/source/enum-in-namespace.bs b/test/project1/source/enum-in-namespace.bs new file mode 100644 index 0000000..84792e5 --- /dev/null +++ b/test/project1/source/enum-in-namespace.bs @@ -0,0 +1,10 @@ +namespace MyNamespace + enum MyEnum + val1 + val2 + end enum + + function foo() as integer + return MyEnum.val1 + end function +end namespace diff --git a/test/project1/source/enums.bs b/test/project1/source/enums.bs new file mode 100644 index 0000000..2749207 --- /dev/null +++ b/test/project1/source/enums.bs @@ -0,0 +1,13 @@ +enum Day + Monday + Tuesday + Wednesday + Thursday + Friday + Saturday + Sunday +end enum + +sub main() + ? Day.Monday +end sub