Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
paleite committed Jul 26, 2022
1 parent 845deaa commit d502eb5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe("getDiffForFile", () => {

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

expect(command).toBe(expectedCommand);
expect(args.join(" ")).toEqual(expectedArgs);
Expand Down
5 changes: 2 additions & 3 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ const getRangeForChangedLines = (line: string) => {
return hasAddedLines ? new Range(start, end) : null;
};

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

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

export {
getDiffFileList,
Expand Down
71 changes: 37 additions & 34 deletions src/processors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const STAGED = true;
const getPreProcessor = (staged = false) => {
const untrackedFileList = getUntrackedFileList(staged);
const diffFileList = getDiffFileList(staged);

return (text: string, filename: string) => {
const shouldBeProcessed =
process.env.VSCODE_CLI !== undefined ||
Expand All @@ -30,9 +31,29 @@ const getPreProcessor = (staged = false) => {
};
};

const isLineWithinRange = (line: number) => (range: Range) => {
return range.isWithinRange(line);
};
const isLineWithinRange = (line: number) => (range: Range) =>
range.isWithinRange(line);

function getUnstagedChangesError(filename: string) {
// When we only want to diff staged files, but the file is partially
// staged, the ranges of the staged diff might not match the ranges of the
// unstaged diff and could cause a conflict, so we return a fatal
// error-message instead.

const fatal = true;
const message = `${filename} has unstaged changes. Please stage or remove the changes.`;
const severity: Linter.Severity = 2;
const fatalError: Linter.LintMessage = {
fatal,
message,
severity,
column: 0,
line: 0,
ruleId: null,
};

return [fatalError];
}

const getPostProcessor = (staged = false) => {
const untrackedFileList = getUntrackedFileList(staged);
Expand All @@ -53,44 +74,26 @@ const getPostProcessor = (staged = false) => {
}

if (staged && !hasCleanIndex(filename)) {
// When we only want to diff staged files, but the file is partially
// staged, the ranges of the staged diff might not match the ranges of the
// unstaged diff and could cause a conflict, so we return a fatal
// error-message instead.
const fatal = true;
const message = `${filename} has unstaged changes. Please stage or remove the changes.`;
const severity: Linter.Severity = 2;
const fatalError: Linter.LintMessage = {
fatal,
message,
severity,
column: 0,
line: 0,
ruleId: null,
};

return [fatalError];
return getUnstagedChangesError(filename);
}

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

return messages
.map((message) => {
const filteredMessage = message.filter(({ fatal, line }) => {
if (fatal === true) {
return true;
}
return messages.flatMap((message) => {
const filteredMessage = message.filter(({ fatal, line }) => {
if (fatal === true) {
return true;
}

const isLineWithinSomeRange = rangesForDiff.some(
isLineWithinRange(line)
);
const isLineWithinSomeRange = rangesForDiff.some(
isLineWithinRange(line)
);

return isLineWithinSomeRange;
});
return isLineWithinSomeRange;
});

return filteredMessage;
})
.reduce((a, b) => a.concat(b), []);
return filteredMessage;
});
};
};

Expand Down

0 comments on commit d502eb5

Please sign in to comment.