summaryrefslogtreecommitdiff
path: root/test/lint/global-declarations.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2020-04-16 17:30:09 -0700
committerAlex Yatskov <alex@foosoft.net>2020-04-16 17:30:09 -0700
commit93c4fb9eab2651da24fb816f20dcb967eae1437e (patch)
treedf8f6d5e04da7e6e78db4712d4d3d92c460454b9 /test/lint/global-declarations.js
parent3b9a87b2ebe843e30536924639d6c14afef936cd (diff)
parent8c16a6e580bfdd70e27df1816ca90807062cf9b5 (diff)
Merge branch 'master' of https://github.com/FooSoft/yomichan
Diffstat (limited to 'test/lint/global-declarations.js')
-rw-r--r--test/lint/global-declarations.js27
1 files changed, 26 insertions, 1 deletions
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;
}