From 23a08efd4c22e7e639f9b6d7982e2cc257b3f035 Mon Sep 17 00:00:00 2001 From: Arturo Cuya Date: Sat, 9 Apr 2022 12:18:29 -0500 Subject: [PATCH] fix(varTracking.ts): do not consider enums as unused variables. --- src/plugins/trackCodeFlow/index.spec.ts | 39 +++++++++++++++++++++++ src/plugins/trackCodeFlow/varTracking.ts | 11 ++++--- test/project1/source/enum-in-class.bs | 10 ++++++ test/project1/source/enum-in-namespace.bs | 10 ++++++ test/project1/source/enums.bs | 13 ++++++++ 5 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 test/project1/source/enum-in-class.bs create mode 100644 test/project1/source/enum-in-namespace.bs create mode 100644 test/project1/source/enums.bs 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