Skip to content

Commit a541fb1

Browse files
committed
List untracked files
1 parent 181bdb8 commit a541fb1

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

src/git.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
getDiffForFile,
77
getGitFileList,
88
getRangesForDiff,
9+
getUntrackedFileList,
910
hasCleanIndex,
1011
} from "./git";
1112
import {
@@ -118,6 +119,28 @@ describe("getDiffFileList", () => {
118119
});
119120
});
120121

122+
describe("getUntrackedFileList", () => {
123+
it("should get the list of untracked files", () => {
124+
jest.mock("child_process").resetAllMocks();
125+
mockedChildProcess.execSync.mockReturnValueOnce(Buffer.from(diffFileList));
126+
expect(mockedChildProcess.execSync).toHaveBeenCalledTimes(0);
127+
const fileListA = getUntrackedFileList();
128+
const staged = false;
129+
const fileListB = getUntrackedFileList(staged);
130+
131+
expect(mockedChildProcess.execSync).toHaveBeenCalledTimes(1);
132+
expect(fileListA).toEqual(
133+
["file1", "file2", "file3"].map((p) => path.resolve(p))
134+
);
135+
expect(fileListA).toEqual(fileListB);
136+
});
137+
138+
it("should not get a list when looking when using staged", () => {
139+
const staged = true;
140+
expect(getUntrackedFileList(staged)).toEqual([]);
141+
});
142+
});
143+
121144
describe("getGitFileList", () => {
122145
it("should get the list of committed files", () => {
123146
mockedChildProcess.execSync.mockReturnValueOnce(Buffer.from(diffFileList));

src/git.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,19 @@ const hasCleanIndex = (filePath: string): boolean => {
104104

105105
let untrackedFileListCache: string[] | undefined;
106106
const getUntrackedFileList = (staged = false): string[] => {
107-
if (untrackedFileListCache === undefined) {
107+
if (staged) {
108+
untrackedFileListCache = [];
109+
} else if (untrackedFileListCache === undefined) {
108110
const command = ["git", "ls-files", "--exclude-standard", "--others"]
109111
.filter(Boolean)
110112
.join(" ");
111113

112-
if (staged === false) {
113-
untrackedFileListCache = child_process
114-
.execSync(command)
115-
.toString()
116-
.trim()
117-
.split("\n")
118-
.map((filePath) => path.resolve(filePath));
119-
} else {
120-
untrackedFileListCache = [];
121-
}
114+
untrackedFileListCache = child_process
115+
.execSync(command)
116+
.toString()
117+
.trim()
118+
.split("\n")
119+
.map((filePath) => path.resolve(filePath));
122120
}
123121
return untrackedFileListCache;
124122
};

src/processors.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Linter } from "eslint";
22
import type { Range } from "./git";
33
import {
4+
getUntrackedFileList,
45
getDiffFileList,
56
getDiffForFile,
67
getRangesForDiff,
@@ -19,7 +20,9 @@ const STAGED = true;
1920
const getPreProcessor =
2021
(staged = false) =>
2122
(text: string, filename: string) => {
22-
const shouldBeProcessed = getDiffFileList(staged).includes(filename);
23+
const shouldBeProcessed =
24+
getDiffFileList(staged).includes(filename) ||
25+
getUntrackedFileList(staged).includes(filename);
2326

2427
return shouldBeProcessed ? [text] : [];
2528
};

0 commit comments

Comments
 (0)