1 | const fmt = require("simple-fmt");
|
2 | const is = require("simple-is");
|
3 | const assert = require("assert");
|
4 |
|
5 | function Stats() {
|
6 | this.lets = 0;
|
7 | this.consts = 0;
|
8 | this.renames = [];
|
9 | }
|
10 |
|
11 | Stats.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 |
|
20 | Stats.prototype.rename = function(oldName, newName, line) {
|
21 | this.renames.push({
|
22 | oldName: oldName,
|
23 | newName: newName,
|
24 | line: line,
|
25 | });
|
26 | };
|
27 |
|
28 | Stats.prototype.toString = function() {
|
29 |
|
30 |
|
31 | const renames = this.renames.map(function(r) {
|
32 | return r;
|
33 | }).sort(function(a, b) {
|
34 | return a.line - b.line;
|
35 | });
|
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 |
|
50 | module.exports = Stats;
|