Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CSV last column empty bug #20

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/fixtures
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nocuous",
"version": "0.4.0",
"version": "0.4.1",
"description": "A static code analysis tool for JavaScript and TypeScript.",
"main": "index.js",
"bin": {
Expand Down Expand Up @@ -34,6 +34,7 @@
"devDependencies": {
"@types/node": "14.0.9",
"@types/table": "5.0.0",
"@types/tmp": "^0.2.3",
"@types/yargs": "15.0.5",
"@typescript-eslint/eslint-plugin": "3.1.0",
"@typescript-eslint/parser": "3.1.0",
Expand All @@ -47,6 +48,7 @@
"prettier": "2.0.5",
"pretty-quick": "2.0.1",
"rimraf": "3.0.2",
"tmp": "^0.2.1",
"ts-node": "8.10.2",
"typescript": "3.9.3"
},
Expand Down
2 changes: 1 addition & 1 deletion src/reports/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function report(
}
}
row.unshift(`"${path}"`);
row.length = headers.length;
row.length = headers.length + 1; // headers do not have "Path"
rows.push(row.join(","));
}
const titles = headers.map((h) => `"${labels[h]}"`);
Expand Down
21 changes: 21 additions & 0 deletions tests/fixtures/stats/cyclomaticComplexity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,24 @@ export function qux(a: string, b: string): string | number {
}
return 0;
}

export function foo(a: number) {
for (let i = 0; false; i++) { } // FoStatement without added complexity
for (let i = 0; i < 10; i++) { }// FoStatement with added complexity
while (false) { }; // WhileStatement with no added complexity
while (a < 5) { // WhileStatement with added complexity
do { a++ } while (a < 5); // DoStatement
};
try {} catch {}; // CatchClause
a = a < 0 ? 0 : a; // ConditionalExpression
for (let x in []) {}; // ForInStatement
for (let x of []) {}; // ForOfStatement
function bar() {}; // FunctionDeclaration
const baz = function() {}; // FunctionExpression
const obj = {
foo() {}, // MethodDeclaration
get bar() { return null }, // GetAccessor
set bar(val) {}, // SetAccessor
};
const Quux = class {}; // ClassExpression
};
31 changes: 31 additions & 0 deletions tests/unit/reports/csv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import test from "ava";
import { fileSync } from "tmp";
import { readFileSync } from "fs";
import { fixtureAsSourceFile } from "../../util";
import { stat } from "../../../src/stats/anonInnerLength";
import { report } from "../../../src/reports/csv";
import { StatResults } from "../../../src/interfaces";

test("reports/csv - works", async (t) => {
const sourceFile = fixtureAsSourceFile("stats/cyclomaticComplexity.ts");
const results: StatResults = {};
results["path1/file1"] = [];
results["path2/file2"] = [];
const stat1 = await stat(sourceFile, { threshold: 1 });
const stat2 = await stat(sourceFile, { threshold: 100 });
if (stat1) {
results["path1/file1"].push(stat1);
}
if (stat2) {
results["path2/file2"].push(stat2);
}
report(results);
const tempFile = fileSync();
report(results, { output: tempFile.name });
const expected = `
"Path","Anonymous inner length"
"path1/file1",5
"path2/file2",
`.trim();
t.is(readFileSync(tempFile.name, "utf-8"), expected);
});
22 changes: 22 additions & 0 deletions tests/unit/reports/table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import test from "ava";
import { fixtureAsSourceFile } from "../../util";
import { stat } from "../../../src/stats/anonInnerLength";
import { report } from "../../../src/reports/table";
import { StatResults } from "../../../src/interfaces";

test("reports/table - works", async (t) => {
const sourceFile = fixtureAsSourceFile("stats/cyclomaticComplexity.ts");
const results: StatResults = {};
results["path1/file1"] = [];
results["path2/file2"] = [];
const stat1 = await stat(sourceFile, { threshold: 1 });
const stat2 = await stat(sourceFile, { threshold: 100 });
if (stat1) {
results["path1/file1"].push(stat1);
}
if (stat2) {
results["path2/file2"].push(stat2);
}
report(results);
t.assert(true);
});
2 changes: 1 addition & 1 deletion tests/unit/stats/cyclomaticComplexity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test("stats/cyclomaticComplexity - counts functions and methods", async (t) => {
const sourceFile = fixtureAsSourceFile("stats/cyclomaticComplexity.ts");
const actual = await stat(sourceFile, { threshold: 10 });
t.assert(actual);
t.is(actual?.count, 7);
t.is(actual?.count, 11);
});

test("stats/cyclomaticComplexity - scores based on threshold", async (t) => {
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import test from "ava";

import { commonStartsWith, sortPaths } from "../../src/util";

test("util/commonStartsWith - no items", async (t) => {
const actual = commonStartsWith(["a/a", "a/b"]);
t.is(actual, "a/");
});

test("util/sortPaths - no items", async (t) => {
const actual = sortPaths(["b/b", "a/b", "a/a"]);
t.deepEqual(actual, ["a/a", "a/b", "b/b"]);
});
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@
resolved "https://registry.yarnpkg.com/@types/table/-/table-5.0.0.tgz#67c3821138eb41d538c3d9286771c6cdeeac7172"
integrity sha512-fQLtGLZXor264zUPWI95WNDsZ3QV43/c0lJpR/h1hhLJumXRmHNsrvBfEzW2YMhb0EWCsn4U6h82IgwsajAuTA==

"@types/tmp@^0.2.3":
version "0.2.3"
resolved "https://registry.yarnpkg.com/@types/tmp/-/tmp-0.2.3.tgz#908bfb113419fd6a42273674c00994d40902c165"
integrity sha512-dDZH/tXzwjutnuk4UacGgFRwV+JSLaXL1ikvidfJprkb7L9Nx1njcRHHmi3Dsvt7pgqqTEeucQuOrWHPFgzVHA==

"@types/yargs-parser@*":
version "15.0.0"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-15.0.0.tgz#cb3f9f741869e20cce330ffbeb9271590483882d"
Expand Down Expand Up @@ -3616,6 +3621,13 @@ tmp@^0.0.33:
dependencies:
os-tmpdir "~1.0.2"

tmp@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/tmp/-/tmp-0.2.1.tgz#8457fc3037dcf4719c251367a1af6500ee1ccf14"
integrity sha512-76SUhtfqR2Ijn+xllcI5P1oyannHNHByD80W1q447gU3mp9G9PSpGdWmjUOHRDPiHYacIk66W7ubDTuPF3BEtQ==
dependencies:
rimraf "^3.0.0"

to-fast-properties@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/to-fast-properties/-/to-fast-properties-2.0.0.tgz#dc5e698cbd079265bc73e0377681a4e4e83f616e"
Expand Down