Skip to content

Commit d502eb5

Browse files
committed
Lint
1 parent 845deaa commit d502eb5

File tree

3 files changed

+40
-38
lines changed

3 files changed

+40
-38
lines changed

src/git.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe("getDiffForFile", () => {
6060

6161
const lastCall = mockedChildProcess.execFileSync.mock.calls.at(-1);
6262
const [command, argsIncludingFile = []] = lastCall ?? [""];
63-
const args = argsIncludingFile.slice(0, argsIncludingFile.length - 2);
63+
const args = argsIncludingFile.slice(0, -2);
6464

6565
expect(command).toBe(expectedCommand);
6666
expect(args.join(" ")).toEqual(expectedArgs);

src/git.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ const getRangeForChangedLines = (line: string) => {
128128
return hasAddedLines ? new Range(start, end) : null;
129129
};
130130

131-
const getRangesForDiff = (diff: string): Range[] => {
132-
return diff.split("\n").reduce<Range[]>((ranges, line) => {
131+
const getRangesForDiff = (diff: string): Range[] =>
132+
diff.split("\n").reduce<Range[]>((ranges, line) => {
133133
if (!isHunkHeader(line)) {
134134
return ranges;
135135
}
@@ -141,7 +141,6 @@ const getRangesForDiff = (diff: string): Range[] => {
141141

142142
return [...ranges, range];
143143
}, []);
144-
};
145144

146145
export {
147146
getDiffFileList,

src/processors.ts

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const STAGED = true;
2020
const getPreProcessor = (staged = false) => {
2121
const untrackedFileList = getUntrackedFileList(staged);
2222
const diffFileList = getDiffFileList(staged);
23+
2324
return (text: string, filename: string) => {
2425
const shouldBeProcessed =
2526
process.env.VSCODE_CLI !== undefined ||
@@ -30,9 +31,29 @@ const getPreProcessor = (staged = false) => {
3031
};
3132
};
3233

33-
const isLineWithinRange = (line: number) => (range: Range) => {
34-
return range.isWithinRange(line);
35-
};
34+
const isLineWithinRange = (line: number) => (range: Range) =>
35+
range.isWithinRange(line);
36+
37+
function getUnstagedChangesError(filename: string) {
38+
// When we only want to diff staged files, but the file is partially
39+
// staged, the ranges of the staged diff might not match the ranges of the
40+
// unstaged diff and could cause a conflict, so we return a fatal
41+
// error-message instead.
42+
43+
const fatal = true;
44+
const message = `${filename} has unstaged changes. Please stage or remove the changes.`;
45+
const severity: Linter.Severity = 2;
46+
const fatalError: Linter.LintMessage = {
47+
fatal,
48+
message,
49+
severity,
50+
column: 0,
51+
line: 0,
52+
ruleId: null,
53+
};
54+
55+
return [fatalError];
56+
}
3657

3758
const getPostProcessor = (staged = false) => {
3859
const untrackedFileList = getUntrackedFileList(staged);
@@ -53,44 +74,26 @@ const getPostProcessor = (staged = false) => {
5374
}
5475

5576
if (staged && !hasCleanIndex(filename)) {
56-
// When we only want to diff staged files, but the file is partially
57-
// staged, the ranges of the staged diff might not match the ranges of the
58-
// unstaged diff and could cause a conflict, so we return a fatal
59-
// error-message instead.
60-
const fatal = true;
61-
const message = `${filename} has unstaged changes. Please stage or remove the changes.`;
62-
const severity: Linter.Severity = 2;
63-
const fatalError: Linter.LintMessage = {
64-
fatal,
65-
message,
66-
severity,
67-
column: 0,
68-
line: 0,
69-
ruleId: null,
70-
};
71-
72-
return [fatalError];
77+
return getUnstagedChangesError(filename);
7378
}
7479

7580
const rangesForDiff = getRangesForDiff(getDiffForFile(filename, staged));
7681

77-
return messages
78-
.map((message) => {
79-
const filteredMessage = message.filter(({ fatal, line }) => {
80-
if (fatal === true) {
81-
return true;
82-
}
82+
return messages.flatMap((message) => {
83+
const filteredMessage = message.filter(({ fatal, line }) => {
84+
if (fatal === true) {
85+
return true;
86+
}
8387

84-
const isLineWithinSomeRange = rangesForDiff.some(
85-
isLineWithinRange(line)
86-
);
88+
const isLineWithinSomeRange = rangesForDiff.some(
89+
isLineWithinRange(line)
90+
);
8791

88-
return isLineWithinSomeRange;
89-
});
92+
return isLineWithinSomeRange;
93+
});
9094

91-
return filteredMessage;
92-
})
93-
.reduce((a, b) => a.concat(b), []);
95+
return filteredMessage;
96+
});
9497
};
9598
};
9699

0 commit comments

Comments
 (0)