Skip to content

Commit 5faa93b

Browse files
committed
Cleanup
1 parent 53af44d commit 5faa93b

File tree

5 files changed

+67
-121
lines changed

5 files changed

+67
-121
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# eslint-plugin-diff
22

3-
Run ESLint on your changes only
3+
Run ESLint on your changed lines only. Now with CI support!
44

55
## What problem does it solve?
66

src/Range.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Range } from "./Range";
2+
23
describe("range", () => {
34
it("should instantiate with correct parameters", () => {
45
const range: Range = new Range(0, 1);

src/__fixtures__/diff.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
export const diff = `diff --git a/fixme.js b/fixme.js
1+
const diff = `diff --git a/fixme.js b/fixme.js
22
index 4886604..83c3014 100644
33
--- a/fixme.js
44
+++ b/fixme.js
55
@@ -1,0 +2,2 @@ if (new Date().getTime()) console.log("curly");
66
+if (new Date().getTime()) console.log("curly");
77
+if (new Date().getTime()) console.log("curly");`;
88

9-
export const staged = `diff --git a/fixme.js b/fixme.js
9+
const staged = `diff --git a/fixme.js b/fixme.js
1010
index 4886604..3238811 100644
1111
--- a/fixme.js
1212
+++ b/fixme.js
1313
@@ -1,0 +2 @@ if (new Date().getTime()) console.log("curly");
1414
+if (new Date().getTime()) console.log("curly");`;
1515

16-
export const hunks = `diff --git a/dirty.js b/dirty.js
16+
const hunks = `diff --git a/dirty.js b/dirty.js
1717
index 4d2637c..99dc494 100644
1818
--- a/dirty.js
1919
+++ b/dirty.js
@@ -35,7 +35,7 @@ index 4d2637c..99dc494 100644
3535
+if (!myFunc(true == myVar)) (myRes = false), (someotherThing = null);
3636
+unrelated = true;`;
3737

38-
export const includingOnlyRemovals = `diff --git a/dirty.js b/dirty.js
38+
const includingOnlyRemovals = `diff --git a/dirty.js b/dirty.js
3939
index cb3c131..874b8f9 100644
4040
--- a/dirty.js
4141
+++ b/dirty.js
@@ -46,11 +46,21 @@ index cb3c131..874b8f9 100644
4646
-import { b } from "../context/b";
4747
-import { c } from "../context/c";`;
4848

49-
export const filenamesAB = `a/dirty.js
49+
const filenamesAB = `a/dirty.js
5050
b/dirty.js
5151
`;
5252

53-
export const filenamesA = `a/dirty.js
54-
`
53+
const filenamesA = `a/dirty.js
54+
`;
55+
56+
const diffFileList = "file1\nfile2\nfile3\n";
5557

56-
export const diffFileList = "file1\nfile2\nfile3\n"
58+
export {
59+
diff,
60+
staged,
61+
hunks,
62+
includingOnlyRemovals,
63+
filenamesAB,
64+
filenamesA,
65+
diffFileList,
66+
};

src/git.ts

Lines changed: 17 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ const getCachedDiff = (filePath: string, staged: boolean) =>
1616

1717
const diffCache = new Map<string, string>();
1818
const getDiffForFile = (filePath: string, staged = false): string => {
19-
// console.log("getDiffForFile")
2019
let diff = getCachedDiff(filePath, staged);
2120
if (diff === undefined) {
2221
const command = [
@@ -25,7 +24,7 @@ const getDiffForFile = (filePath: string, staged = false): string => {
2524
"--diff-filter=ACM",
2625
staged && "--staged",
2726
"--unified=0",
28-
JSON.stringify(process.env.ESLINT_PLUGIN_DIFF_COMMIT) ?? "HEAD",
27+
JSON.stringify(process.env.ESLINT_PLUGIN_DIFF_COMMIT ?? "HEAD"),
2928
"--",
3029
sanitizeFilePath(filePath),
3130
]
@@ -40,17 +39,16 @@ const getDiffForFile = (filePath: string, staged = false): string => {
4039
return diff;
4140
};
4241

43-
let diffFileListCache: string[];
42+
let diffFileListCache: string[] | undefined;
4443
const getDiffFileList = (staged = false): string[] => {
45-
// console.log("getDiffFileList")
4644
if (diffFileListCache === undefined) {
4745
const command = [
4846
"git",
4947
"diff",
5048
"--diff-filter=ACM",
5149
"--name-only",
5250
staged && "--staged",
53-
JSON.stringify(process.env.ESLINT_PLUGIN_DIFF_COMMIT) ?? "HEAD",
51+
JSON.stringify(process.env.ESLINT_PLUGIN_DIFF_COMMIT ?? "HEAD"),
5452
]
5553
.filter(Boolean)
5654
.join(" ");
@@ -65,66 +63,29 @@ const getDiffFileList = (staged = false): string[] => {
6563
return diffFileListCache;
6664
};
6765

68-
let gitFileListCache: string[];
66+
let gitFileListCache: string[] | undefined;
6967
const getGitFileList = (): string[] => {
70-
// console.log("getGitFileList")
7168
if (gitFileListCache === undefined) {
7269
const command = ["git", "ls-files"].filter(Boolean).join(" ");
7370

7471
gitFileListCache = child_process
75-
.execSync(command)
76-
.toString()
77-
.trim()
78-
.split("\n")
79-
.map((filePath) => path.resolve(filePath));
72+
.execSync(command)
73+
.toString()
74+
.trim()
75+
.split("\n")
76+
.map((filePath) => path.resolve(filePath));
8077
}
81-
// console.log({gitFileListCache},gitFileListCache.join("\n"))
8278
return gitFileListCache;
8379
};
8480

8581
const getIgnorePatterns = (staged = false): string[] => {
86-
const diffFileList = getDiffFileList(staged);
87-
const gitFileList = getGitFileList();
88-
// console.log({diffFileList, gitFileList})
89-
90-
const newList = gitFileList.filter((x) => !diffFileList.includes(x));
91-
const willBeChecked = gitFileList.filter((x) => diffFileList.includes(x));
82+
const changedFiles = getDiffFileList(staged);
9283

93-
// throw new Error("Function not implemented.");
94-
const result = newList.map((x) =>
95-
path.join("/", path.relative(process.cwd(), x))
96-
);
84+
const unchangedFiles = getGitFileList()
85+
.filter((x) => !changedFiles.includes(x))
86+
.map((x) => path.join("/", path.relative(process.cwd(), x)));
9787

98-
console.log("😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀");
99-
console.log("😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀");
100-
console.log("😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀");
101-
console.log("😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀");
102-
console.log("😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀");
103-
console.log("😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀");
104-
console.log("😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀");
105-
console.log("😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀");
106-
console.log("😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀");
107-
console.log("😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀");
108-
console.log("😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀");
109-
console.log("😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀");
110-
console.log("😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀");
111-
console.log("😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀");
112-
console.log("😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀");
113-
console.log("😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀😀");
114-
console.log("will be checked" + willBeChecked.join("\n"));
115-
console.log("------------------");
116-
console.log("------------------");
117-
console.log("------------------");
118-
console.log("------------------");
119-
console.log("------------------");
120-
console.log("------------------");
121-
console.log("------------------");
122-
console.log("------------------");
123-
console.log("------------------");
124-
console.log("------------------");
125-
console.log("------------------");
126-
console.log("will be ignored" + newList.join("\n"));
127-
return result;
88+
return unchangedFiles;
12889
};
12990

13091
const isHunkHeader = (input: string) => {
@@ -162,18 +123,12 @@ const getRangeForChangedLines = (line: string) => {
162123

163124
const removeNullRanges = (r: Range | null): r is Range => r !== null;
164125

165-
const getRangesForDiff = (diff: string): Range[] => {
166-
return diff
126+
const getRangesForDiff = (diff: string): Range[] =>
127+
diff
167128
.split("\n")
168129
.filter(isHunkHeader)
169130
.map(getRangeForChangedLines)
170131
.filter(removeNullRanges);
171-
};
172132

173-
export {
174-
getDiffForFile,
175-
getIgnorePatterns,
176-
getRangesForDiff,
177-
getDiffFileList,
178-
};
133+
export { getDiffForFile, getIgnorePatterns, getRangesForDiff, getDiffFileList };
179134
export type { Range };

src/processors.ts

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { Linter } from "eslint";
2+
import type { Range } from "./git";
23
import {
34
getDiffFileList,
45
getDiffForFile,
56
getIgnorePatterns,
67
getRangesForDiff,
7-
Range,
88
} from "./git";
99

1010
const STAGED = true;
@@ -17,38 +17,21 @@ const diff = {
1717
messages: Linter.LintMessage[][],
1818
filename: string
1919
): Linter.LintMessage[] => {
20-
if (!getDiffFileList().includes(filename)) {
21-
// console.log( "🧠 skipping " + JSON.stringify(filename) + " because it's not in the diff list" );
22-
return [];
23-
}
24-
const result = messages
25-
.map((message) => {
26-
// console.log("diff/diff", message, JSON.stringify(filename));
27-
return message.filter(({ fatal, line }) => {
28-
if (fatal) {
29-
// console.log("❌ fatal error in " + JSON.stringify(filename));
30-
return fatal;
31-
}
20+
const shouldKeepFile = getDiffFileList().includes(filename);
3221

33-
if (
34-
!getRangesForDiff(getDiffForFile(filename)).some(
35-
isLineWithinRange(line)
36-
)
37-
) {
38-
// console.log( "🔵 skipping " + JSON.stringify(filename) + " because it's not in the diff list" );
39-
}
22+
return shouldKeepFile
23+
? messages
24+
.map((message) =>
25+
message.filter(({ fatal, line }) => {
26+
const shouldKeepLine = getRangesForDiff(
27+
getDiffForFile(filename)
28+
).some(isLineWithinRange(line));
4029

41-
return (
42-
fatal ||
43-
getRangesForDiff(getDiffForFile(filename)).some(
44-
isLineWithinRange(line)
45-
)
46-
);
47-
});
48-
})
49-
.reduce((a, b) => a.concat(b), []);
50-
// console.log("diff kjrngkjsngksnj", { result, filename, messages });
51-
return result;
30+
return fatal ?? shouldKeepLine;
31+
})
32+
)
33+
.reduce((a, b) => a.concat(b), [])
34+
: [];
5235
},
5336

5437
supportsAutofix: true,
@@ -66,29 +49,26 @@ const diffConfig = {
6649
};
6750

6851
const staged = {
69-
preprocess: (
70-
text: string,
71-
filename: string
72-
): ({ text: string; filename: string } & Record<any, any>)[] => {
73-
// console.log({ text: text, filename: filename, lol: "zooooooooooomg" });
74-
return getDiffFileList(STAGED).includes(filename)
75-
? [{ text, filename }]
76-
: [];
77-
},
78-
7952
postprocess: (
8053
messages: Linter.LintMessage[][],
8154
filename: string
82-
): Linter.LintMessage[] =>
83-
messages
84-
.map((message) =>
85-
message.filter(({ line }) =>
86-
getRangesForDiff(getDiffForFile(filename, STAGED)).some(
87-
isLineWithinRange(line)
55+
): Linter.LintMessage[] => {
56+
const shouldKeepFile = getDiffFileList().includes(filename);
57+
58+
return shouldKeepFile
59+
? messages
60+
.map((message) =>
61+
message.filter(({ fatal, line }) => {
62+
const shouldKeepLine = getRangesForDiff(
63+
getDiffForFile(filename, STAGED)
64+
).some(isLineWithinRange(line));
65+
66+
return fatal ?? shouldKeepLine;
67+
})
8868
)
89-
)
90-
)
91-
.reduce((a, b) => a.concat(b), []),
69+
.reduce((a, b) => a.concat(b), [])
70+
: [];
71+
},
9272

9373
supportsAutofix: true,
9474
};

0 commit comments

Comments
 (0)