UNPKG

1.32 kBJavaScriptView Raw
1const fmt = require("simple-fmt");
2const is = require("simple-is");
3const assert = require("assert");
4
5function Stats() {
6 this.lets = 0;
7 this.consts = 0;
8 this.renames = [];
9}
10
11Stats.prototype.declarator = function(kind) {
12 assert(is.someof(kind, ["const", "let"]));
13 if (kind === "const") {
14 this.consts++;
15 } else {
16 this.lets++;
17 }
18};
19
20Stats.prototype.rename = function(oldName, newName, line) {
21 this.renames.push({
22 oldName: oldName,
23 newName: newName,
24 line: line,
25 });
26};
27
28Stats.prototype.toString = function() {
29// console.log("defs.js stats for file {0}:", filename)
30
31 const renames = this.renames.map(function(r) {
32 return r;
33 }).sort(function(a, b) {
34 return a.line - b.line;
35 }); // sort a copy of renames
36
37 const renameStr = renames.map(function(rename) {
38 return fmt("\nline {0}: {1} => {2}", rename.line, rename.oldName, rename.newName);
39 }).join("");
40
41 const sum = this.consts + this.lets;
42 const constlets = (sum === 0 ?
43 "can't calculate const coverage (0 consts, 0 lets)" :
44 fmt("{0}% const coverage ({1} consts, {2} lets)",
45 Math.floor(100 * this.consts / sum), this.consts, this.lets));
46
47 return constlets + renameStr + "\n";
48};
49
50module.exports = Stats;