summary refs log tree commit diff
path: root/scripts/code_quality.js
blob: aca01d245375bc45d41bc13c745e63a18bb90aa3 (plain) (blame)
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
const path = require("path");
const fs = require("fs").promises;
const { execIn, getLines, walk, projectRoot } = require("./utils");

let printTodos = process.argv.includes("TODOS");

let root = path.resolve(path.join(__dirname, "..", "src"));
let files = walk(root);
let _files = [];
let errors = 0,
	warnings = 0,
	todos = 0;

Promise.all(files.map(getFile)).then((f) => {
	Promise.all(Object.keys(_files).map(checkFile));
	console.log(`\n${errors} errors, ${warnings} warnings, ${todos} TODOs.`);

	let loc = 0;
	Object.values(_files).forEach((x) => {
		loc += x.length;
	});
	console.log("\nStats:\n");
	console.log(`Lines of code: ${loc} lines in ${Object.values(_files).length} files.`);

	debugger;
});

async function getFile(name) {
	let contents = (await fs.readFile(name)).toString().split("\n");
	_files[name] = contents;
}

async function checkFile(x) {
	_files[x].forEach((line) => scanLine(x, line));
}

function log(file, line, msg) {
	let lineNum = _files[file].indexOf(line) + 1;
	console.log(msg, "File:", file.replace(root + "/", "") + ":" + lineNum);
}

function scanLine(x, line) {
	if (/import/.test(line)) {
		if (/import {?.*}? from '.*'/.test(line)) {
			log(x, line, `[WARN] Inconsistent import syntax, please use double quotes!`);
			warnings++;
		}
	} else {
		if (line.trim().endsWith("TODO:")) {
			log(x, line, `[ERRO] Empty TODO!`);
			errors++;
		} else if (/\/\/\s{0,3}TODO:/.test(line)) {
			if (printTodos) log(x, line, `[TODO] Found a TODO: ${line.split("TODO:")[1].trim()}.`);
			todos++;
		}
		if (/(:|=)/.test(line)) {
			if (/(:|=) {2,}/.test(line)) {
				log(x, line, `[WARN] Multiple spaces in assignment!`);
				warnings++;
			}
			if (/(:|=)\t'/.test(line)) {
				log(x, line, `[WARN] Tab in assignment!`);
				warnings++;
			}
			if (/(:|=)\w'/.test(line)) {
				log(x, line, `[WARN] Missing space in assignment!`);
				warnings++;
			}
			if (/(:|=) undefined/.test(line) && !/(:|=){2,} undefined/.test(line)) {
				log(x, line, `[WARN] Use of undefined!`);
				warnings++;
			}
		}
	}
}