From 9071cbdc25e97153086ac48bd389b04c43e56458 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Fri, 19 Aug 2022 18:53:25 +0200 Subject: Updates scripts and tests from dev/plugins --- scripts/code_quality.js | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 scripts/code_quality.js (limited to 'scripts/code_quality.js') diff --git a/scripts/code_quality.js b/scripts/code_quality.js new file mode 100644 index 00000000..aca01d24 --- /dev/null +++ b/scripts/code_quality.js @@ -0,0 +1,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++; + } + } + } +} -- cgit 1.4.1