Skip to content

Commit 8cab38f

Browse files
authored
Merge pull request #8 from paleite/bugfix/fix-typescript-eslint
Bugfix/fix typescript eslint
2 parents ec3cec3 + 5faa93b commit 8cab38f

File tree

5 files changed

+91
-47
lines changed

5 files changed

+91
-47
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: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const getDiffForFile = (filePath: string, staged = false): string => {
2424
"--diff-filter=ACM",
2525
staged && "--staged",
2626
"--unified=0",
27-
JSON.stringify(process.env.ESLINT_PLUGIN_DIFF_COMMIT) ?? "HEAD",
27+
JSON.stringify(process.env.ESLINT_PLUGIN_DIFF_COMMIT ?? "HEAD"),
2828
"--",
2929
sanitizeFilePath(filePath),
3030
]
@@ -39,7 +39,7 @@ const getDiffForFile = (filePath: string, staged = false): string => {
3939
return diff;
4040
};
4141

42-
let diffFileListCache: string[];
42+
let diffFileListCache: string[] | undefined;
4343
const getDiffFileList = (staged = false): string[] => {
4444
if (diffFileListCache === undefined) {
4545
const command = [
@@ -48,7 +48,7 @@ const getDiffFileList = (staged = false): string[] => {
4848
"--diff-filter=ACM",
4949
"--name-only",
5050
staged && "--staged",
51-
JSON.stringify(process.env.ESLINT_PLUGIN_DIFF_COMMIT) ?? "HEAD",
51+
JSON.stringify(process.env.ESLINT_PLUGIN_DIFF_COMMIT ?? "HEAD"),
5252
]
5353
.filter(Boolean)
5454
.join(" ");
@@ -63,6 +63,31 @@ const getDiffFileList = (staged = false): string[] => {
6363
return diffFileListCache;
6464
};
6565

66+
let gitFileListCache: string[] | undefined;
67+
const getGitFileList = (): string[] => {
68+
if (gitFileListCache === undefined) {
69+
const command = ["git", "ls-files"].filter(Boolean).join(" ");
70+
71+
gitFileListCache = child_process
72+
.execSync(command)
73+
.toString()
74+
.trim()
75+
.split("\n")
76+
.map((filePath) => path.resolve(filePath));
77+
}
78+
return gitFileListCache;
79+
};
80+
81+
const getIgnorePatterns = (staged = false): string[] => {
82+
const changedFiles = getDiffFileList(staged);
83+
84+
const unchangedFiles = getGitFileList()
85+
.filter((x) => !changedFiles.includes(x))
86+
.map((x) => path.join("/", path.relative(process.cwd(), x)));
87+
88+
return unchangedFiles;
89+
};
90+
6691
const isHunkHeader = (input: string) => {
6792
const hunkHeaderRE = new RegExp(/^@@ .* @@/g);
6893
return input.match(hunkHeaderRE);
@@ -98,13 +123,12 @@ const getRangeForChangedLines = (line: string) => {
98123

99124
const removeNullRanges = (r: Range | null): r is Range => r !== null;
100125

101-
const getRangesForDiff = (diff: string): Range[] => {
102-
return diff
126+
const getRangesForDiff = (diff: string): Range[] =>
127+
diff
103128
.split("\n")
104129
.filter(isHunkHeader)
105130
.map(getRangeForChangedLines)
106131
.filter(removeNullRanges);
107-
};
108132

109-
export { getDiffForFile, getRangesForDiff, getDiffFileList };
133+
export { getDiffForFile, getIgnorePatterns, getRangesForDiff, getDiffFileList };
110134
export type { Range };

src/processors.ts

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
import type { Linter } from "eslint";
22
import type { Range } from "./git";
3-
import { getDiffFileList, getDiffForFile, getRangesForDiff } from "./git";
3+
import {
4+
getDiffFileList,
5+
getDiffForFile,
6+
getIgnorePatterns,
7+
getRangesForDiff,
8+
} from "./git";
49

510
const STAGED = true;
611

712
const isLineWithinRange = (line: number) => (range: Range) =>
813
range.isWithinRange(line);
914

1015
const diff = {
11-
preprocess: (
12-
text: string,
13-
filename: string
14-
): { text: string; filename: string }[] =>
15-
getDiffFileList().includes(filename) ? [{ text, filename }] : [],
16-
1716
postprocess: (
1817
messages: Linter.LintMessage[][],
1918
filename: string
20-
): Linter.LintMessage[] =>
21-
messages
22-
.map((message) =>
23-
message.filter(({ line }) =>
24-
getRangesForDiff(getDiffForFile(filename)).some(
25-
isLineWithinRange(line)
19+
): Linter.LintMessage[] => {
20+
const shouldKeepFile = getDiffFileList().includes(filename);
21+
22+
return shouldKeepFile
23+
? messages
24+
.map((message) =>
25+
message.filter(({ fatal, line }) => {
26+
const shouldKeepLine = getRangesForDiff(
27+
getDiffForFile(filename)
28+
).some(isLineWithinRange(line));
29+
30+
return fatal ?? shouldKeepLine;
31+
})
2632
)
27-
)
28-
)
29-
.reduce((a, b) => a.concat(b), []),
33+
.reduce((a, b) => a.concat(b), [])
34+
: [];
35+
},
3036

3137
supportsAutofix: true,
3238
};
@@ -39,28 +45,30 @@ const diffConfig = {
3945
processor: "diff/diff",
4046
},
4147
],
48+
ignorePatterns: getIgnorePatterns(),
4249
};
4350

4451
const staged = {
45-
preprocess: (
46-
text: string,
47-
filename: string
48-
): { text: string; filename: string }[] =>
49-
getDiffFileList(STAGED).includes(filename) ? [{ text, filename }] : [],
50-
5152
postprocess: (
5253
messages: Linter.LintMessage[][],
5354
filename: string
54-
): Linter.LintMessage[] =>
55-
messages
56-
.map((message) =>
57-
message.filter(({ line }) =>
58-
getRangesForDiff(getDiffForFile(filename, STAGED)).some(
59-
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+
})
6068
)
61-
)
62-
)
63-
.reduce((a, b) => a.concat(b), []),
69+
.reduce((a, b) => a.concat(b), [])
70+
: [];
71+
},
6472

6573
supportsAutofix: true,
6674
};
@@ -73,6 +81,7 @@ const stagedConfig = {
7381
processor: "diff/staged",
7482
},
7583
],
84+
ignorePatterns: getIgnorePatterns(STAGED),
7685
};
7786

7887
export { diff, diffConfig, staged, stagedConfig };

0 commit comments

Comments
 (0)