Skip to content

Commit acfb083

Browse files
committed
Add support for env variable ESLINT_PLUGIN_DIFF_COMMIT
1 parent 469cc69 commit acfb083

File tree

4 files changed

+32
-86
lines changed

4 files changed

+32
-86
lines changed

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,8 @@
3535
"postinstall": "husky install",
3636
"prepublishOnly": "pinst --disable"
3737
},
38-
"dependencies": {
39-
"env-ci": "^5.0.2"
40-
},
4138
"devDependencies": {
4239
"@tsconfig/node10": "^1.0.7",
43-
"@types/env-ci": "^3.1.0",
4440
"@types/eslint": "^7.2.6",
4541
"@types/jest": "^26.0.20",
4642
"@types/node": "^14.14.31",

src/git.ts

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,24 @@ const diffCache = new Map<string, string>();
1818
const getDiffForFile = (
1919
filePath: string,
2020
staged = false,
21-
changesBetween = "HEAD"
21+
changesBetween = process.env.ESLINT_PLUGIN_DIFF_COMMIT ?? "HEAD"
2222
): string => {
2323
let diff = getCachedDiff(filePath, staged);
2424
if (diff === undefined) {
25-
const result = child_process
26-
.execSync(
27-
`git diff --diff-filter=ACM --unified=0 ${changesBetween} ${
28-
staged ? " --staged" : ""
29-
} -- ${sanitizeFilePath(filePath)}`
30-
)
31-
.toString();
25+
const command = [
26+
"git",
27+
"diff",
28+
"--diff-filter=ACM",
29+
staged && "--staged",
30+
"--unified=0",
31+
changesBetween,
32+
"--",
33+
sanitizeFilePath(filePath),
34+
]
35+
.filter(Boolean)
36+
.join(" ");
37+
38+
const result = child_process.execSync(command).toString();
3239
setCachedDiff(filePath, staged, result);
3340
diff = result;
3441
}
@@ -37,14 +44,24 @@ const getDiffForFile = (
3744
};
3845

3946
let diffFileListCache: string[];
40-
const getDiffFileList = (staged = false, changesBetween = "HEAD"): string[] => {
47+
const getDiffFileList = (
48+
staged = false,
49+
changesBetween = process.env.ESLINT_PLUGIN_DIFF_COMMIT ?? "HEAD"
50+
): string[] => {
4151
if (diffFileListCache === undefined) {
52+
const command = [
53+
"git",
54+
"diff",
55+
"--diff-filter=ACM",
56+
"--name-only",
57+
staged && "--staged",
58+
changesBetween,
59+
]
60+
.filter(Boolean)
61+
.join(" ");
62+
4263
diffFileListCache = child_process
43-
.execSync(
44-
`git diff --diff-filter=ACM ${changesBetween} --name-only ${
45-
staged ? "--staged" : ""
46-
}`
47-
)
64+
.execSync(command)
4865
.toString()
4966
.trim()
5067
.split("\n")

src/processors.ts

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
1-
import { default as envCi } from "env-ci";
21
import type { Linter } from "eslint";
32
import type { Range } from "./git";
43
import { getDiffFileList, getDiffForFile, getRangesForDiff } from "./git";
54

65
const STAGED = true;
76

8-
const changesBetween = (): string => {
9-
const { branch, commit } = envCi();
10-
11-
if (!branch) {
12-
throw Error("Couldn't find branch");
13-
} else if (!commit) {
14-
throw Error("Couldn't find commit");
15-
}
16-
17-
return `${branch}..${commit}`;
18-
};
19-
207
const isLineWithinRange = (line: number) => (range: Range) =>
218
range.isWithinRange(line);
229

@@ -88,40 +75,4 @@ const stagedConfig = {
8875
],
8976
};
9077

91-
const ci = {
92-
preprocess: (
93-
text: string,
94-
filename: string
95-
): { text: string; filename: string }[] =>
96-
getDiffFileList(false, changesBetween()).includes(filename)
97-
? [{ text, filename }]
98-
: [],
99-
100-
postprocess: (
101-
messages: Linter.LintMessage[][],
102-
filename: string
103-
): Linter.LintMessage[] =>
104-
messages
105-
.map((message) =>
106-
message.filter(({ line }) =>
107-
getRangesForDiff(
108-
getDiffForFile(filename, false, changesBetween())
109-
).some(isLineWithinRange(line))
110-
)
111-
)
112-
.reduce((a, b) => a.concat(b), []),
113-
114-
supportsAutofix: true,
115-
};
116-
117-
const ciConfig = {
118-
plugins: ["diff"],
119-
overrides: [
120-
{
121-
files: ["*"],
122-
processor: "diff/ci",
123-
},
124-
],
125-
};
126-
127-
export { diff, diffConfig, staged, stagedConfig, ci, ciConfig };
78+
export { diff, diffConfig, staged, stagedConfig };

yarn.lock

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -607,11 +607,6 @@
607607
"@types/node" "*"
608608
"@types/responselike" "*"
609609

610-
"@types/env-ci@^3.1.0":
611-
version "3.1.0"
612-
resolved "https://registry.yarnpkg.com/@types/env-ci/-/env-ci-3.1.0.tgz#8b8b388641c641fa4eb51862cbb018db013aeb68"
613-
integrity sha512-oJTxA/GmZavFFr6wtuFcmOUH4Lfj0RU9icnYBzp8gXDj7p8rCr5XRR2W6VzYOoCuEJusQsRUjSMr+3xOJ1S6yw==
614-
615610
"@types/eslint@^7.2.6":
616611
version "7.2.6"
617612
resolved "https://registry.yarnpkg.com/@types/eslint/-/eslint-7.2.6.tgz#5e9aff555a975596c03a98b59ecd103decc70c3c"
@@ -1837,14 +1832,6 @@ enquirer@^2.3.5, enquirer@^2.3.6:
18371832
dependencies:
18381833
ansi-colors "^4.1.1"
18391834

1840-
env-ci@^5.0.2:
1841-
version "5.0.2"
1842-
resolved "https://registry.yarnpkg.com/env-ci/-/env-ci-5.0.2.tgz#48b6687f8af8cdf5e31b8fcf2987553d085249d9"
1843-
integrity sha512-Xc41mKvjouTXD3Oy9AqySz1IeyvJvHZ20Twf5ZLYbNpPPIuCnL/qHCmNlD01LoNy0JTunw9HPYVptD19Ac7Mbw==
1844-
dependencies:
1845-
execa "^4.0.0"
1846-
java-properties "^1.0.0"
1847-
18481835
error-ex@^1.2.0, error-ex@^1.3.1:
18491836
version "1.3.2"
18501837
resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.2.tgz#b4ac40648107fdcdcfae242f428bea8a14d4f1bf"
@@ -3195,11 +3182,6 @@ istanbul-reports@^3.0.2:
31953182
html-escaper "^2.0.0"
31963183
istanbul-lib-report "^3.0.0"
31973184

3198-
java-properties@^1.0.0:
3199-
version "1.0.2"
3200-
resolved "https://registry.yarnpkg.com/java-properties/-/java-properties-1.0.2.tgz#ccd1fa73907438a5b5c38982269d0e771fe78211"
3201-
integrity sha512-qjdpeo2yKlYTH7nFdK0vbZWuTCesk4o63v5iVOlhMQPfuIZQfW/HI35SjfhA+4qpg36rnFSvUK5b1m+ckIblQQ==
3202-
32033185
jest-changed-files@^26.6.2:
32043186
version "26.6.2"
32053187
resolved "https://registry.yarnpkg.com/jest-changed-files/-/jest-changed-files-26.6.2.tgz#f6198479e1cc66f22f9ae1e22acaa0b429c042d0"

0 commit comments

Comments
 (0)