aboutsummaryrefslogtreecommitdiff
path: root/dev/lint/global-declarations.js
blob: b28f53cd2429d8862ea07b49425ab25b4b7eadc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * Copyright (C) 2020-2022  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
 * 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');
const {getAllFiles} = require('../util');


function escapeRegExp(string) {
    return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&');
}

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';
    }
}

function getSubstringCount(string, substring) {
    let count = 0;
    const pattern = new RegExp(`\\b${escapeRegExp(substring)}\\b`, 'g');
    while (true) {
        const match = pattern.exec(string);
        if (match === null) { break; }
        ++count;
    }
    return count;
}


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 = '';
    const allGlobals = [];
    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;

        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 errorCount === 0;
}


function main() {
    const fix = (process.argv.length >= 2 && process.argv[2] === '--fix');
    const directory = path.resolve(__dirname, '..', '..', 'ext');
    const pattern = /\.js$/;
    const ignorePattern = /^lib[\\/]/;
    const fileNames = getAllFiles(directory, (f) => pattern.test(f) && !ignorePattern.test(f));
    for (const fileName of fileNames) {
        if (!validateGlobals(path.join(directory, fileName), fix)) {
            process.exit(-1);
            return;
        }
    }
    process.exit(0);
}


if (require.main === module) { main(); }