Skip to content

Commit 4463cf6

Browse files
committed
Infer diff range for CI-mode
1 parent 1445898 commit 4463cf6

File tree

4 files changed

+83
-7
lines changed

4 files changed

+83
-7
lines changed

package.json

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

src/git.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ const getCachedDiff = (filePath: string, staged: boolean) =>
1515
diffCache.get(diffCacheKey(filePath, staged));
1616

1717
const diffCache = new Map<string, string>();
18-
const getDiffForFile = (filePath: string, staged = false): string => {
18+
const getDiffForFile = (
19+
filePath: string,
20+
staged = false,
21+
changesBetween = "HEAD"
22+
): string => {
1923
let diff = getCachedDiff(filePath, staged);
2024
if (diff === undefined) {
2125
const result = child_process
2226
.execSync(
23-
`git diff --diff-filter=ACM --unified=0 HEAD ${
27+
`git diff --diff-filter=ACM --unified=0 ${changesBetween} ${
2428
staged ? " --staged" : ""
2529
} -- ${sanitizeFilePath(filePath)}`
2630
)
@@ -33,11 +37,11 @@ const getDiffForFile = (filePath: string, staged = false): string => {
3337
};
3438

3539
let diffFileListCache: string[];
36-
const getDiffFileList = (staged = false): string[] => {
40+
const getDiffFileList = (staged = false, changesBetween = "HEAD"): string[] => {
3741
if (diffFileListCache === undefined) {
3842
diffFileListCache = child_process
3943
.execSync(
40-
`git diff --diff-filter=ACM HEAD --name-only ${
44+
`git diff --diff-filter=ACM ${changesBetween} --name-only ${
4145
staged ? "--staged" : ""
4246
}`
4347
)

src/processors.ts

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1-
import type { Range } from "./git";
2-
import { getDiffForFile, getRangesForDiff, getDiffFileList } from "./git";
1+
import { default as envCi } from "env-ci";
32
import type { Linter } from "eslint";
3+
import type { Range } from "./git";
4+
import { getDiffFileList, getDiffForFile, getRangesForDiff } from "./git";
45

56
const STAGED = true;
67

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+
720
const isLineWithinRange = (line: number) => (range: Range) =>
821
range.isWithinRange(line);
922

@@ -61,6 +74,7 @@ const staged = {
6174
)
6275
)
6376
),
77+
6478
supportsAutofix: true,
6579
};
6680

@@ -74,4 +88,40 @@ const stagedConfig = {
7488
],
7589
};
7690

77-
export { diff, diffConfig, staged, stagedConfig };
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+
([] as Linter.LintMessage[]).concat(
105+
...messages.map((message) =>
106+
message.filter(({ line }) =>
107+
getRangesForDiff(
108+
getDiffForFile(filename, false, changesBetween())
109+
).some(isLineWithinRange(line))
110+
)
111+
)
112+
),
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 };

yarn.lock

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,11 @@
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+
610615
"@types/eslint@^7.2.6":
611616
version "7.2.6"
612617
resolved "https://registry.yarnpkg.com/@types/eslint/-/eslint-7.2.6.tgz#5e9aff555a975596c03a98b59ecd103decc70c3c"
@@ -1832,6 +1837,14 @@ enquirer@^2.3.5, enquirer@^2.3.6:
18321837
dependencies:
18331838
ansi-colors "^4.1.1"
18341839

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+
18351848
error-ex@^1.2.0, error-ex@^1.3.1:
18361849
version "1.3.2"
18371850
resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.2.tgz#b4ac40648107fdcdcfae242f428bea8a14d4f1bf"
@@ -3182,6 +3195,11 @@ istanbul-reports@^3.0.2:
31823195
html-escaper "^2.0.0"
31833196
istanbul-lib-report "^3.0.0"
31843197

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+
31853203
jest-changed-files@^26.6.2:
31863204
version "26.6.2"
31873205
resolved "https://registry.yarnpkg.com/jest-changed-files/-/jest-changed-files-26.6.2.tgz#f6198479e1cc66f22f9ae1e22acaa0b429c042d0"

0 commit comments

Comments
 (0)