Skip to content

Add logging #51

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

Open
wants to merge 4 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
11 changes: 9 additions & 2 deletions cspell.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{
"version": "0.2",
"words": ["ahmadnassri", "codecov", "Eriksson", "paleite"],
"ignoreWords": ["pinst", "Wercker"]
"words": ["Unstaged"],
"ignoreWords": [
"ahmadnassri",
"codecov",
"Eriksson",
"paleite",
"pinst",
"Wercker"
]
}
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,23 @@
"test": "jest --coverage",
"typecheck": "tsc --project tsconfig.json --noEmit"
},
"resolutions": {
"cosmiconfig": "^8.1.3"
},
"devDependencies": {
"@paleite/eslint-config": "^1.0.9",
"@paleite/eslint-config-base": "^1.0.9",
"@paleite/eslint-config-typescript": "^1.0.9",
"@paleite/jest-config": "^1.0.9",
"@paleite/prettier-config": "^1.0.9",
"@paleite/tsconfig-node16": "^1.0.9",
"@types/debug": "^4.1.7",
"@types/eslint": "^8.4.10",
"@types/jest": "^29.2.6",
"@types/node": "^18.11.18",
"@typescript-eslint/eslint-plugin": "^5.48.2",
"@typescript-eslint/parser": "^5.48.2",
"debug": "^4.3.2",
"eslint": "^8.32.0",
"eslint-config-prettier": "^8.6.0",
"eslint-import-resolver-typescript": "^3.5.3",
Expand All @@ -73,8 +78,5 @@
},
"engines": {
"node": ">=14.0.0"
},
"resolutions": {
"cosmiconfig": "^8.1.3"
}
}
10 changes: 9 additions & 1 deletion src/Range.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { log } from "./logging";

class Range {
private readonly inclusiveLowerBound: Readonly<number>;
private readonly exclusiveUpperBound: Readonly<number>;
Expand All @@ -17,7 +19,13 @@ class Range {
}

isWithinRange(n: Readonly<number>): boolean {
return this.inclusiveLowerBound <= n && n < this.exclusiveUpperBound;
log(
`Checking if ${n} is within range ${this.inclusiveLowerBound} - ${this.exclusiveUpperBound}`
);
const result =
this.inclusiveLowerBound <= n && n < this.exclusiveUpperBound;

return result;
}
}

Expand Down
34 changes: 20 additions & 14 deletions src/ci.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { log } from "./logging";

type CiProviderCommon<T extends CiProviderName> = { name: T };

type CiProvider<T extends CiProviderName = CiProviderName> =
Expand Down Expand Up @@ -74,24 +76,28 @@ const PROVIDERS = {

type CiProviderName = keyof typeof PROVIDERS;

const guessProviders = () =>
Object.values(PROVIDERS).reduce<{ name: CiProviderName; branch: string }[]>(
(acc, { name, ...cur }) => {
if (!cur.isSupported || cur.diffBranch === undefined) {
return acc;
}
const guessProviders = () => {
log("Guessing CI providers");

return Object.values(PROVIDERS).reduce<
{ name: CiProviderName; branch: string }[]
>((acc, { name, ...cur }) => {
if (!cur.isSupported || cur.diffBranch === undefined) {
return acc;
}

const branch = process.env[cur.diffBranch] ?? "";
if (branch === "") {
return acc;
}
const branch = process.env[cur.diffBranch] ?? "";
if (branch === "") {
return acc;
}

return [...acc, { name, branch }];
},
[]
);
return [...acc, { name, branch }];
}, []);
};

const guessBranch = (): string | undefined => {
log("Guessing branch");

if ((process.env.ESLINT_PLUGIN_COMMIT ?? "").length > 0) {
throw Error("ESLINT_PLUGIN_COMMIT already set");
}
Expand Down
8 changes: 6 additions & 2 deletions src/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ describe("getDiffForFile", () => {
mockedChildProcess.execFileSync.mockReturnValueOnce(Buffer.from(hunks));
process.env.ESLINT_PLUGIN_DIFF_COMMIT = "1234567";

const diffFromFile = getDiffForFile("./mockfile.js", true);
const diffFromFile = getDiffForFile(
process.env.ESLINT_PLUGIN_DIFF_COMMIT,
"./mockfile.js",
true
);

const expectedCommand = "git";
const expectedArgs =
Expand Down Expand Up @@ -93,7 +97,7 @@ describe("getDiffFileList", () => {
Buffer.from(diffFileList)
);
expect(mockedChildProcess.execFileSync).toHaveBeenCalledTimes(0);
const fileListA = getDiffFileList();
const fileListA = getDiffFileList("HEAD");

expect(mockedChildProcess.execFileSync).toHaveBeenCalledTimes(1);
expect(fileListA).toEqual(
Expand Down
35 changes: 28 additions & 7 deletions src/git.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import * as child_process from "child_process";
import { resolve } from "path";
import { log } from "./logging";
import { Range } from "./Range";

const COMMAND = "git";
const OPTIONS = { maxBuffer: 1024 * 1024 * 100 };

const getDiffForFile = (filePath: string, staged = false): string => {
const getDiffForFile = (
commit: string,
filePath: string,
staged = false
): string => {
log("Getting diff for file", filePath);
const args = [
"diff",
"--diff-algorithm=histogram",
Expand All @@ -15,7 +21,7 @@ const getDiffForFile = (filePath: string, staged = false): string => {
"--relative",
staged && "--staged",
"--unified=0",
process.env.ESLINT_PLUGIN_DIFF_COMMIT ?? "HEAD",
commit,
"--",
resolve(filePath),
].reduce<string[]>(
Expand All @@ -26,7 +32,9 @@ const getDiffForFile = (filePath: string, staged = false): string => {
return child_process.execFileSync(COMMAND, args, OPTIONS).toString();
};

const getDiffFileList = (staged = false): string[] => {
const getDiffFileList = (commit: string, staged = false): string[] => {
log(`Getting list of files for ${staged ? "staged files" : "changed files"}`);

const args = [
"diff",
"--diff-algorithm=histogram",
Expand All @@ -36,22 +44,28 @@ const getDiffFileList = (staged = false): string[] => {
"--no-ext-diff",
"--relative",
staged && "--staged",
process.env.ESLINT_PLUGIN_DIFF_COMMIT ?? "HEAD",
commit,
"--",
].reduce<string[]>(
(acc, cur) => (typeof cur === "string" ? [...acc, cur] : acc),
[]
);

return child_process
const diffFileList = child_process
.execFileSync(COMMAND, args, OPTIONS)
.toString()
.trim()
.split("\n")
.map((filePath) => resolve(filePath));

log(diffFileList);

return diffFileList;
};

const hasCleanIndex = (filePath: string): boolean => {
log("Checking if file has clean index");

const args = [
"diff",
"--no-ext-diff",
Expand All @@ -72,6 +86,7 @@ const hasCleanIndex = (filePath: string): boolean => {
};

const fetchFromOrigin = (branch: string) => {
log("Fetching from origin", branch);
const args = ["fetch", "--quiet", "origin", branch];

child_process.execFileSync(COMMAND, args, OPTIONS);
Expand All @@ -87,6 +102,7 @@ const getUntrackedFileList = (
}

if (untrackedFileListCache === undefined || shouldRefresh) {
log("Getting list of files for untracked files");
const args = ["ls-files", "--exclude-standard", "--others"];

untrackedFileListCache = child_process
Expand All @@ -95,6 +111,8 @@ const getUntrackedFileList = (
.trim()
.split("\n")
.map((filePath) => resolve(filePath));

log("Untracked files", untrackedFileListCache);
}

return untrackedFileListCache;
Expand Down Expand Up @@ -141,8 +159,10 @@ const getRangeForChangedLines = (line: string) => {
return hasAddedLines ? new Range(start, end) : null;
};

const getRangesForDiff = (diff: string): Range[] =>
diff.split("\n").reduce<Range[]>((ranges, line) => {
const getRangesForDiff = (diff: string): Range[] => {
log("Getting ranges for diff");

return diff.split("\n").reduce<Range[]>((ranges, line) => {
if (!isHunkHeader(line)) {
return ranges;
}
Expand All @@ -154,6 +174,7 @@ const getRangesForDiff = (diff: string): Range[] =>

return [...ranges, range];
}, []);
};

export {
fetchFromOrigin,
Expand Down
5 changes: 5 additions & 0 deletions src/logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import debug from "debug";

const log = debug("eslint-plugin-diff");

export { log };
Loading