From 4e5029f7ec82aaa99e16bd84a0ec2ad19a15c6b0 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Fri, 10 Apr 2020 11:06:55 -0700 Subject: Update license author field to broader to explicitly reflect that Yomichan is an ongoing community project. It really does take a village, and everyone who contributes code owns a part of it :) --- test/lint/global-declarations.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test/lint') diff --git a/test/lint/global-declarations.js b/test/lint/global-declarations.js index 2629cc5e..07ba5570 100644 --- a/test/lint/global-declarations.js +++ b/test/lint/global-declarations.js @@ -1,6 +1,6 @@ /* - * Copyright (C) 2020 Alex Yatskov - * Author: Alex Yatskov + * Copyright (C) 2020 Yomichan Authors + * Author: Yomichan Authors * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by -- cgit v1.2.3 From e7abeb02a64c599474ce47c5a8cde46097882634 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 10 Apr 2020 18:45:16 -0400 Subject: Add simple check to see if globals are unused --- test/lint/global-declarations.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'test/lint') 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; } -- cgit v1.2.3