Skip to content

Commit fff9d47

Browse files
committed
Make the code path run once instead of relying on cache
1 parent a425902 commit fff9d47

File tree

6 files changed

+59
-125
lines changed

6 files changed

+59
-125
lines changed

.eslintrc.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1+
const typescriptProjects = ["./tsconfig.json", "./tsconfig.eslint.json"];
2+
13
/** @type import("eslint").Linter.Config */
24
module.exports = {
35
root: true,
46
extends: ["@paleite"],
5-
parserOptions: {
6-
project: [
7-
`${__dirname}/tsconfig.json`,
8-
`${__dirname}/tsconfig.eslint.json`,
9-
],
10-
},
7+
parserOptions: { project: typescriptProjects, tsconfigRootDir: __dirname },
118
overrides: [
129
{
13-
files: ["lint-staged.config.js", "jest.config.js"],
10+
files: [".*.js", "*.js"],
1411

1512
rules: {
1613
"@typescript-eslint/no-var-requires": "off",

src/git.test.ts

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import path from "path";
44
import {
55
getDiffFileList,
66
getDiffForFile,
7-
getGitFileList,
87
getRangesForDiff,
98
getUntrackedFileList,
109
hasCleanIndex,
@@ -68,21 +67,6 @@ describe("getDiffForFile", () => {
6867
expect(diffFromFile).toContain("diff --git");
6968
expect(diffFromFile).toContain("@@");
7069
});
71-
72-
it("should hit the cached diff of a file", () => {
73-
jest.mock("child_process").resetAllMocks();
74-
mockedChildProcess.execFileSync.mockReturnValueOnce(Buffer.from(hunks));
75-
76-
expect(mockedChildProcess.execFileSync).toHaveBeenCalledTimes(0);
77-
const diffFromFileA = getDiffForFile("./mockfileCache.js");
78-
const diffFromFileB = getDiffForFile("./mockfileCache.js");
79-
expect(mockedChildProcess.execFileSync).toHaveBeenCalledTimes(1);
80-
expect(diffFromFileA).toEqual(diffFromFileB);
81-
82-
mockedChildProcess.execFileSync.mockReturnValueOnce(Buffer.from(hunks));
83-
getDiffForFile("./mockfileMiss.js");
84-
expect(mockedChildProcess.execFileSync).toHaveBeenCalledTimes(2);
85-
});
8670
});
8771

8872
describe("hasCleanIndex", () => {
@@ -111,13 +95,11 @@ describe("getDiffFileList", () => {
11195
);
11296
expect(mockedChildProcess.execFileSync).toHaveBeenCalledTimes(0);
11397
const fileListA = getDiffFileList();
114-
const fileListB = getDiffFileList();
11598

11699
expect(mockedChildProcess.execFileSync).toHaveBeenCalledTimes(1);
117100
expect(fileListA).toEqual(
118101
["file1", "file2", "file3"].map((p) => path.resolve(p))
119102
);
120-
expect(fileListA).toEqual(fileListB);
121103
});
122104
});
123105

@@ -129,10 +111,15 @@ describe("getUntrackedFileList", () => {
129111
);
130112
expect(mockedChildProcess.execFileSync).toHaveBeenCalledTimes(0);
131113
const fileListA = getUntrackedFileList();
114+
expect(mockedChildProcess.execFileSync).toHaveBeenCalledTimes(1);
115+
116+
mockedChildProcess.execFileSync.mockReturnValueOnce(
117+
Buffer.from(diffFileList)
118+
);
132119
const staged = false;
133120
const fileListB = getUntrackedFileList(staged);
121+
expect(mockedChildProcess.execFileSync).toHaveBeenCalledTimes(2);
134122

135-
expect(mockedChildProcess.execFileSync).toHaveBeenCalledTimes(1);
136123
expect(fileListA).toEqual(
137124
["file1", "file2", "file3"].map((p) => path.resolve(p))
138125
);
@@ -144,14 +131,3 @@ describe("getUntrackedFileList", () => {
144131
expect(getUntrackedFileList(staged)).toEqual([]);
145132
});
146133
});
147-
148-
describe("getGitFileList", () => {
149-
it("should get the list of committed files", () => {
150-
mockedChildProcess.execFileSync.mockReturnValueOnce(
151-
Buffer.from(diffFileList)
152-
);
153-
expect(getGitFileList()).toEqual(
154-
["file1", "file2", "file3"].map((p) => path.resolve(p))
155-
);
156-
});
157-
});

src/git.ts

Lines changed: 42 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2,88 +2,49 @@ import * as child_process from "child_process";
22
import * as path from "path";
33
import { Range } from "./Range";
44

5-
const RUNNING_INSIDE_VSCODE = process.env.VSCODE_CLI !== undefined;
65
const COMMAND = "git";
76

87
const sanitizeFilePath = (filePath: string) =>
98
JSON.stringify(path.resolve(filePath));
109

11-
const diffCacheKey = (filePath: string, staged: boolean): string =>
12-
JSON.stringify([path.resolve(filePath), staged]);
13-
14-
const diffCache = new Map<string, string>();
15-
const setCachedDiff = (filePath: string, staged: boolean, diff: string): void =>
16-
void diffCache.set(diffCacheKey(filePath, staged), diff);
17-
18-
const getCachedDiff = (filePath: string, staged: boolean) =>
19-
diffCache.get(diffCacheKey(filePath, staged));
20-
2110
const getDiffForFile = (filePath: string, staged = false): string => {
22-
let diff = getCachedDiff(filePath, staged);
23-
if (RUNNING_INSIDE_VSCODE || diff === undefined) {
24-
const args = [
25-
"diff",
26-
"--diff-algorithm=histogram",
27-
"--diff-filter=ACM",
28-
"-M100%",
29-
"--relative",
30-
staged && "--staged",
31-
"--unified=0",
32-
JSON.stringify(process.env.ESLINT_PLUGIN_DIFF_COMMIT ?? "HEAD"),
33-
"--",
34-
sanitizeFilePath(filePath),
35-
].reduce<string[]>(
36-
(acc, cur) => (typeof cur === "string" ? [...acc, cur] : acc),
37-
[]
38-
);
39-
40-
const result = child_process.execFileSync(COMMAND, args).toString();
41-
setCachedDiff(filePath, staged, result);
42-
diff = result;
43-
}
11+
const args = [
12+
"diff",
13+
"--diff-algorithm=histogram",
14+
"--diff-filter=ACM",
15+
"-M100%",
16+
"--relative",
17+
staged && "--staged",
18+
"--unified=0",
19+
JSON.stringify(process.env.ESLINT_PLUGIN_DIFF_COMMIT ?? "HEAD"),
20+
"--",
21+
sanitizeFilePath(filePath),
22+
].reduce<string[]>(
23+
(acc, cur) => (typeof cur === "string" ? [...acc, cur] : acc),
24+
[]
25+
);
4426

45-
return diff;
27+
return child_process.execFileSync(COMMAND, args).toString();
4628
};
4729

48-
let diffFileListCache: string[] | undefined;
4930
const getDiffFileList = (): string[] => {
50-
if (RUNNING_INSIDE_VSCODE || diffFileListCache === undefined) {
51-
const args = [
52-
"diff",
53-
"--diff-algorithm=histogram",
54-
"--diff-filter=ACM",
55-
"-M100%",
56-
"--name-only",
57-
"--relative",
58-
"--staged",
59-
JSON.stringify(process.env.ESLINT_PLUGIN_DIFF_COMMIT ?? "HEAD"),
60-
];
61-
62-
diffFileListCache = child_process
63-
.execFileSync(COMMAND, args)
64-
.toString()
65-
.trim()
66-
.split("\n")
67-
.map((filePath) => path.resolve(filePath));
68-
}
69-
70-
return diffFileListCache;
71-
};
72-
73-
let gitFileListCache: string[] | undefined;
74-
const getGitFileList = (): string[] => {
75-
if (RUNNING_INSIDE_VSCODE || gitFileListCache === undefined) {
76-
const args = ["ls-files"];
77-
78-
gitFileListCache = child_process
79-
.execFileSync(COMMAND, args)
80-
.toString()
81-
.trim()
82-
.split("\n")
83-
.map((filePath) => path.resolve(filePath));
84-
}
31+
const args = [
32+
"diff",
33+
"--diff-algorithm=histogram",
34+
"--diff-filter=ACM",
35+
"-M100%",
36+
"--name-only",
37+
"--relative",
38+
"--staged",
39+
JSON.stringify(process.env.ESLINT_PLUGIN_DIFF_COMMIT ?? "HEAD"),
40+
];
8541

86-
return gitFileListCache;
42+
return child_process
43+
.execFileSync(COMMAND, args)
44+
.toString()
45+
.trim()
46+
.split("\n")
47+
.map((filePath) => path.resolve(filePath));
8748
};
8849

8950
const hasCleanIndex = (filePath: string): boolean => {
@@ -106,21 +67,20 @@ const hasCleanIndex = (filePath: string): boolean => {
10667
return result;
10768
};
10869

109-
let untrackedFileListCache: string[] | undefined;
11070
const getUntrackedFileList = (staged = false): string[] => {
11171
if (staged) {
112-
untrackedFileListCache = [];
113-
} else if (RUNNING_INSIDE_VSCODE || untrackedFileListCache === undefined) {
114-
const args = ["ls-files", "--exclude-standard", "--others"];
115-
116-
untrackedFileListCache = child_process
117-
.execFileSync(COMMAND, args)
118-
.toString()
119-
.trim()
120-
.split("\n")
121-
.map((filePath) => path.resolve(filePath));
72+
return [];
12273
}
12374

75+
const args = ["ls-files", "--exclude-standard", "--others"];
76+
77+
const untrackedFileListCache = child_process
78+
.execFileSync(COMMAND, args)
79+
.toString()
80+
.trim()
81+
.split("\n")
82+
.map((filePath) => path.resolve(filePath));
83+
12484
return untrackedFileListCache;
12585
};
12686

@@ -183,7 +143,6 @@ export {
183143
getDiffFileList,
184144
getDiffForFile,
185145
getRangesForDiff,
186-
getGitFileList,
187146
getUntrackedFileList,
188147
hasCleanIndex,
189148
};

src/processors.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const mockedChildProcess = mocked(child_process, true);
1313
mockedChildProcess.execFileSync.mockReturnValue(
1414
Buffer.from('/mock filename ", ; .js')
1515
);
16-
jest.mock("./git", () => ({
16+
jest.mock("./git", (): unknown => ({
1717
...jest.requireActual("./git"),
1818
getDiffFileList: jest
1919
.fn()

src/processors.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,18 @@ const getPostProcessor =
5858
return [fatalError];
5959
}
6060

61+
const diff = getDiffForFile(filename, staged);
62+
6163
return messages
6264
.map((message) => {
6365
const filteredMessage = message.filter(({ fatal, line }) => {
6466
if (fatal === true) {
6567
return true;
6668
}
6769

68-
const isLineWithinSomeRange = getRangesForDiff(
69-
getDiffForFile(filename, staged)
70-
).some(isLineWithinRange(line));
70+
const isLineWithinSomeRange = getRangesForDiff(diff).some(
71+
isLineWithinRange(line)
72+
);
7173

7274
return isLineWithinSomeRange;
7375
});

tsconfig.eslint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"extends": "./tsconfig.base.json",
3-
"include": [".eslintrc.js", "jest.config.js"]
3+
"include": [".*.js", "*.js"]
44
}

0 commit comments

Comments
 (0)