summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-04-13 18:55:19 -0400
committerGitHub <noreply@github.com>2020-04-13 18:55:19 -0400
commitbf971be15bf8787f992f03bb5943e3ee32c1583a (patch)
treedd034440354a85920822984848b4cc5e4fc0aac0
parent02d815f3f4ece48bab1ba0aa8c650459a4698930 (diff)
parentd164fc6f6255ecbfc9c120f52af7983e46ed4c51 (diff)
Merge pull request #444 from toasted-nutbread/unused-globals
Check for unused globals as part of CI
-rw-r--r--ext/bg/js/backend.js1
-rw-r--r--ext/bg/js/database.js3
-rw-r--r--test/lint/global-declarations.js27
3 files changed, 26 insertions, 5 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index be8ea322..eb1f3e7b 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -30,7 +30,6 @@
* Translator
* conditionsTestValue
* dictConfigured
- * dictEnabledSet
* dictTermsSort
* handlebarsRenderDynamic
* jp
diff --git a/ext/bg/js/database.js b/ext/bg/js/database.js
index ad4e3bad..260c815a 100644
--- a/ext/bg/js/database.js
+++ b/ext/bg/js/database.js
@@ -16,10 +16,7 @@
*/
/* global
- * JSZip
- * JsonSchema
* dictFieldSplit
- * requestJson
*/
class Database {
diff --git a/test/lint/global-declarations.js b/test/lint/global-declarations.js
index 07ba5570..2fc9a5e2 100644
--- a/test/lint/global-declarations.js
+++ b/test/lint/global-declarations.js
@@ -37,6 +37,18 @@ function getNewline(string) {
}
}
+function getSubstringCount(string, substring) {
+ let start = 0;
+ let count = 0;
+ while (true) {
+ const pos = string.indexOf(substring, start);
+ if (pos < 0) { break; }
+ ++count;
+ start = pos + substring.length;
+ }
+ return count;
+}
+
function validateGlobals(fileName, fix) {
const pattern = /\/\*\s*global\s+([\w\W]*?)\*\//g;
@@ -47,6 +59,7 @@ function validateGlobals(fileName, fix) {
let first = true;
let endIndex = 0;
let newSource = '';
+ const allGlobals = [];
const newline = getNewline(source);
while ((match = pattern.exec(source)) !== null) {
if (!first) {
@@ -74,15 +87,27 @@ function validateGlobals(fileName, fix) {
newSource += source.substring(0, match.index);
newSource += expected;
endIndex = match.index + match[0].length;
+
+ allGlobals.push(...parts);
}
newSource += source.substring(endIndex);
+ // This is an approximate check to see if a global variable is unused.
+ // If the global appears in a comment, string, or similar, the check will pass.
+ let errorCount = 0;
+ for (const global of allGlobals) {
+ if (getSubstringCount(newSource, global) <= 1) {
+ console.error(`Global variable ${global} appears to be unused in ${fileName}`);
+ ++errorCount;
+ }
+ }
+
if (fix) {
fs.writeFileSync(fileName, newSource, {encoding: 'utf8'});
}
- return true;
+ return errorCount === 0;
}