Skip to content

Commit bfb8eb4

Browse files
authored
Merge pull request #7 from paleite/feature/add-ci-mode
Feature/add ci mode
2 parents 1445898 + e0a8792 commit bfb8eb4

File tree

5 files changed

+215
-168
lines changed

5 files changed

+215
-168
lines changed

.husky/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
_
1+
_

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@
4040
"@types/eslint": "^7.2.6",
4141
"@types/jest": "^26.0.20",
4242
"@types/node": "^14.14.31",
43-
"@typescript-eslint/eslint-plugin": "^4.15.1",
44-
"@typescript-eslint/parser": "^4.15.1",
43+
"@typescript-eslint/eslint-plugin": "^4.15.2",
44+
"@typescript-eslint/parser": "^4.15.2",
4545
"eslint": "^7.0.0",
4646
"eslint-config-prettier": "^8.0.0",
4747
"eslint-plugin-import": "^2.22.1",
4848
"eslint-plugin-promise": "^4.3.1",
49-
"husky": "^5.1.0",
49+
"husky": "^5.1.1",
5050
"jest": "^26.6.3",
5151
"lint-staged": "^10.5.4",
5252
"np": "^7.4.0",
53-
"pinst": "^2.1.4",
53+
"pinst": "^2.1.6",
5454
"prettier": "^2.2.1",
5555
"ts-jest": "^26.5.1",
5656
"typescript": "^4.1.5"

src/git.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,20 @@ const diffCache = new Map<string, string>();
1818
const getDiffForFile = (filePath: string, staged = false): string => {
1919
let diff = getCachedDiff(filePath, staged);
2020
if (diff === undefined) {
21-
const result = child_process
22-
.execSync(
23-
`git diff --diff-filter=ACM --unified=0 HEAD ${
24-
staged ? " --staged" : ""
25-
} -- ${sanitizeFilePath(filePath)}`
26-
)
27-
.toString();
21+
const command = [
22+
"git",
23+
"diff",
24+
"--diff-filter=ACM",
25+
staged && "--staged",
26+
"--unified=0",
27+
JSON.stringify(process.env.ESLINT_PLUGIN_DIFF_COMMIT) ?? "HEAD",
28+
"--",
29+
sanitizeFilePath(filePath),
30+
]
31+
.filter(Boolean)
32+
.join(" ");
33+
34+
const result = child_process.execSync(command).toString();
2835
setCachedDiff(filePath, staged, result);
2936
diff = result;
3037
}
@@ -35,12 +42,19 @@ const getDiffForFile = (filePath: string, staged = false): string => {
3542
let diffFileListCache: string[];
3643
const getDiffFileList = (staged = false): string[] => {
3744
if (diffFileListCache === undefined) {
45+
const command = [
46+
"git",
47+
"diff",
48+
"--diff-filter=ACM",
49+
"--name-only",
50+
staged && "--staged",
51+
JSON.stringify(process.env.ESLINT_PLUGIN_DIFF_COMMIT) ?? "HEAD",
52+
]
53+
.filter(Boolean)
54+
.join(" ");
55+
3856
diffFileListCache = child_process
39-
.execSync(
40-
`git diff --diff-filter=ACM HEAD --name-only ${
41-
staged ? "--staged" : ""
42-
}`
43-
)
57+
.execSync(command)
4458
.toString()
4559
.trim()
4660
.split("\n")

src/processors.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { Range } from "./git";
2-
import { getDiffForFile, getRangesForDiff, getDiffFileList } from "./git";
31
import type { Linter } from "eslint";
2+
import type { Range } from "./git";
3+
import { getDiffFileList, getDiffForFile, getRangesForDiff } from "./git";
44

55
const STAGED = true;
66

@@ -18,15 +18,15 @@ const diff = {
1818
messages: Linter.LintMessage[][],
1919
filename: string
2020
): Linter.LintMessage[] =>
21-
([] as Linter.LintMessage[]).concat(
22-
...messages.map((message) =>
21+
messages
22+
.map((message) =>
2323
message.filter(({ line }) =>
2424
getRangesForDiff(getDiffForFile(filename)).some(
2525
isLineWithinRange(line)
2626
)
2727
)
2828
)
29-
),
29+
.reduce((a, b) => a.concat(b), []),
3030

3131
supportsAutofix: true,
3232
};
@@ -52,15 +52,16 @@ const staged = {
5252
messages: Linter.LintMessage[][],
5353
filename: string
5454
): Linter.LintMessage[] =>
55-
([] as Linter.LintMessage[]).concat(
56-
...messages.map((message) =>
55+
messages
56+
.map((message) =>
5757
message.filter(({ line }) =>
5858
getRangesForDiff(getDiffForFile(filename, STAGED)).some(
5959
isLineWithinRange(line)
6060
)
6161
)
6262
)
63-
),
63+
.reduce((a, b) => a.concat(b), []),
64+
6465
supportsAutofix: true,
6566
};
6667

0 commit comments

Comments
 (0)