2020-03-11 02:28:33 +00:00
|
|
|
/*
|
2021-01-01 19:50:41 +00:00
|
|
|
* Copyright (C) 2020-2021 Yomichan Authors
|
2020-03-11 02:28:33 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const assert = require('assert');
|
2020-09-19 23:04:28 +00:00
|
|
|
const {getAllFiles} = require('../util');
|
2020-03-11 02:28:33 +00:00
|
|
|
|
|
|
|
|
2020-12-12 19:47:17 +00:00
|
|
|
function escapeRegExp(string) {
|
|
|
|
return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&');
|
|
|
|
}
|
|
|
|
|
2020-03-11 02:28:33 +00:00
|
|
|
function countOccurences(string, pattern) {
|
|
|
|
return (string.match(pattern) || []).length;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNewline(string) {
|
|
|
|
const count1 = countOccurences(string, /(?:^|[^\r])\n/g);
|
|
|
|
const count2 = countOccurences(string, /\r\n/g);
|
|
|
|
const count3 = countOccurences(string, /\r(?:[^\n]|$)/g);
|
|
|
|
if (count2 > count1) {
|
|
|
|
return (count3 > count2) ? '\r' : '\r\n';
|
|
|
|
} else {
|
|
|
|
return (count3 > count1) ? '\r' : '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 22:45:16 +00:00
|
|
|
function getSubstringCount(string, substring) {
|
|
|
|
let count = 0;
|
2020-12-12 19:47:17 +00:00
|
|
|
const pattern = new RegExp(`\\b${escapeRegExp(substring)}\\b`, 'g');
|
2020-04-10 22:45:16 +00:00
|
|
|
while (true) {
|
2020-12-12 19:47:17 +00:00
|
|
|
const match = pattern.exec(string);
|
|
|
|
if (match === null) { break; }
|
2020-04-10 22:45:16 +00:00
|
|
|
++count;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2020-03-11 02:28:33 +00:00
|
|
|
|
|
|
|
function validateGlobals(fileName, fix) {
|
|
|
|
const pattern = /\/\*\s*global\s+([\w\W]*?)\*\//g;
|
|
|
|
const trimPattern = /^[\s,*]+|[\s,*]+$/g;
|
|
|
|
const splitPattern = /[\s,*]+/;
|
|
|
|
const source = fs.readFileSync(fileName, {encoding: 'utf8'});
|
|
|
|
let match;
|
|
|
|
let first = true;
|
|
|
|
let endIndex = 0;
|
|
|
|
let newSource = '';
|
2020-04-10 22:45:16 +00:00
|
|
|
const allGlobals = [];
|
2020-03-11 02:28:33 +00:00
|
|
|
const newline = getNewline(source);
|
|
|
|
while ((match = pattern.exec(source)) !== null) {
|
|
|
|
if (!first) {
|
|
|
|
console.error(`Encountered more than one global declaration in ${fileName}`);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
const parts = match[1].replace(trimPattern, '').split(splitPattern);
|
|
|
|
parts.sort();
|
|
|
|
|
|
|
|
const actual = match[0];
|
|
|
|
const expected = `/* global${parts.map((v) => `${newline} * ${v}`).join('')}${newline} */`;
|
|
|
|
|
|
|
|
try {
|
|
|
|
assert.strictEqual(actual, expected);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(`Global declaration error encountered in ${fileName}:`);
|
|
|
|
console.error(e.message);
|
|
|
|
if (!fix) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newSource += source.substring(0, match.index);
|
|
|
|
newSource += expected;
|
|
|
|
endIndex = match.index + match[0].length;
|
2020-04-10 22:45:16 +00:00
|
|
|
|
|
|
|
allGlobals.push(...parts);
|
2020-03-11 02:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newSource += source.substring(endIndex);
|
|
|
|
|
2020-04-10 22:45:16 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-11 02:28:33 +00:00
|
|
|
if (fix) {
|
|
|
|
fs.writeFileSync(fileName, newSource, {encoding: 'utf8'});
|
|
|
|
}
|
|
|
|
|
2020-04-10 22:45:16 +00:00
|
|
|
return errorCount === 0;
|
2020-03-11 02:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function main() {
|
|
|
|
const fix = (process.argv.length >= 2 && process.argv[2] === '--fix');
|
|
|
|
const directory = path.resolve(__dirname, '..', '..', 'ext');
|
|
|
|
const pattern = /\.js$/;
|
|
|
|
const ignorePattern = /[\\/]ext[\\/]mixed[\\/]lib[\\/]/;
|
2020-08-10 01:00:37 +00:00
|
|
|
const fileNames = getAllFiles(directory, null, (f) => pattern.test(f) && !ignorePattern.test(f));
|
2020-03-11 02:28:33 +00:00
|
|
|
for (const fileName of fileNames) {
|
|
|
|
if (!validateGlobals(fileName, fix)) {
|
|
|
|
process.exit(-1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (require.main === module) { main(); }
|