Skip to content

Commit 845deaa

Browse files
committed
Improve performance significantly
1 parent 97d9a17 commit 845deaa

File tree

5 files changed

+111
-19
lines changed

5 files changed

+111
-19
lines changed

.husky/pre-commit

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/sh
22
. "$(dirname "$0")/_/husky.sh"
33

4+
npx --no-install pretty-quick --staged
45
npx --no-install lint-staged

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"np": "^7.6.1",
6767
"pinst": "^3.0.0",
6868
"prettier": "^2.6.2",
69+
"pretty-quick": "^3.1.3",
6970
"size-limit": "^7.0.8",
7071
"terser": "^5.14.2",
7172
"ts-jest": "^28.0.4",

src/git.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const getDiffForFile = (filePath: string, staged = false): string => {
2525
return child_process.execFileSync(COMMAND, args).toString();
2626
};
2727

28-
const getDiffFileList = (): string[] => {
28+
const getDiffFileList = (staged = false): string[] => {
2929
const args = [
3030
"diff",
3131
"--diff-algorithm=histogram",
@@ -34,9 +34,12 @@ const getDiffFileList = (): string[] => {
3434
"--name-only",
3535
"--no-ext-diff",
3636
"--relative",
37-
"--staged",
37+
staged && "--staged",
3838
process.env.ESLINT_PLUGIN_DIFF_COMMIT ?? "HEAD",
39-
];
39+
].reduce<string[]>(
40+
(acc, cur) => (typeof cur === "string" ? [...acc, cur] : acc),
41+
[]
42+
);
4043

4144
return child_process
4245
.execFileSync(COMMAND, args)
@@ -125,8 +128,8 @@ const getRangeForChangedLines = (line: string) => {
125128
return hasAddedLines ? new Range(start, end) : null;
126129
};
127130

128-
const getRangesForDiff = (diff: string): Range[] =>
129-
diff.split("\n").reduce<Range[]>((ranges, line) => {
131+
const getRangesForDiff = (diff: string): Range[] => {
132+
return diff.split("\n").reduce<Range[]>((ranges, line) => {
130133
if (!isHunkHeader(line)) {
131134
return ranges;
132135
}
@@ -138,6 +141,7 @@ const getRangesForDiff = (diff: string): Range[] =>
138141

139142
return [...ranges, range];
140143
}, []);
144+
};
141145

142146
export {
143147
getDiffFileList,

src/processors.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,46 @@ const STAGED = true;
1717
* them from being processed in the first place, as a performance optimization.
1818
* This is increasingly useful the more files there are in the repository.
1919
*/
20-
const getPreProcessor =
21-
(staged = false) =>
22-
(text: string, filename: string) => {
20+
const getPreProcessor = (staged = false) => {
21+
const untrackedFileList = getUntrackedFileList(staged);
22+
const diffFileList = getDiffFileList(staged);
23+
return (text: string, filename: string) => {
2324
const shouldBeProcessed =
2425
process.env.VSCODE_CLI !== undefined ||
25-
!staged ||
26-
getDiffFileList().includes(filename);
26+
diffFileList.includes(filename) ||
27+
untrackedFileList.includes(filename);
2728

2829
return shouldBeProcessed ? [text] : [];
2930
};
31+
};
3032

3133
const isLineWithinRange = (line: number) => (range: Range) => {
3234
return range.isWithinRange(line);
3335
};
3436

35-
const getPostProcessor =
36-
(staged = false) =>
37-
(
37+
const getPostProcessor = (staged = false) => {
38+
const untrackedFileList = getUntrackedFileList(staged);
39+
40+
return (
3841
messages: Linter.LintMessage[][],
3942
filename: string
4043
): Linter.LintMessage[] => {
41-
if (!staged && getUntrackedFileList(staged).includes(filename)) {
44+
if (messages.length === 0) {
45+
// No need to filter, just return
46+
return [];
47+
}
48+
49+
if (untrackedFileList.includes(filename)) {
50+
// We don't need to filter the messages of untracked files because they
51+
// would all be kept anyway, so we return them as-is.
4252
return messages.flat();
4353
}
4454

4555
if (staged && !hasCleanIndex(filename)) {
56+
// When we only want to diff staged files, but the file is partially
57+
// staged, the ranges of the staged diff might not match the ranges of the
58+
// unstaged diff and could cause a conflict, so we return a fatal
59+
// error-message instead.
4660
const fatal = true;
4761
const message = `${filename} has unstaged changes. Please stage or remove the changes.`;
4862
const severity: Linter.Severity = 2;
@@ -58,7 +72,7 @@ const getPostProcessor =
5872
return [fatalError];
5973
}
6074

61-
const diff = getDiffForFile(filename, staged);
75+
const rangesForDiff = getRangesForDiff(getDiffForFile(filename, staged));
6276

6377
return messages
6478
.map((message) => {
@@ -67,7 +81,7 @@ const getPostProcessor =
6781
return true;
6882
}
6983

70-
const isLineWithinSomeRange = getRangesForDiff(diff).some(
84+
const isLineWithinSomeRange = rangesForDiff.some(
7185
isLineWithinRange(line)
7286
);
7387

@@ -78,6 +92,7 @@ const getPostProcessor =
7892
})
7993
.reduce((a, b) => a.concat(b), []);
8094
};
95+
};
8196

8297
const getProcessors = (staged = false): Required<Linter.Processor> => ({
8398
preprocess: getPreProcessor(staged),

yarn.lock

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,11 @@
800800
dependencies:
801801
"@types/node" "*"
802802

803+
"@types/minimatch@^3.0.3":
804+
version "3.0.5"
805+
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40"
806+
integrity sha512-Klz949h02Gz2uZCMGwDUSDS1YBlTdDDgbWHi+81l29tQALUtvz4rAYi5uoVhE5Lagoq6DeqAUlbrHvW/mXDgdQ==
807+
803808
"@types/minimist@^1.2.0":
804809
version "1.2.2"
805810
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c"
@@ -1065,6 +1070,11 @@ argparse@^2.0.1:
10651070
resolved "https://registry.yarnpkg.com/argparse/-/argparse-2.0.1.tgz#246f50f3ca78a3240f6c997e8a9bd1eac49e4b38"
10661071
integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==
10671072

1073+
array-differ@^3.0.0:
1074+
version "3.0.0"
1075+
resolved "https://registry.yarnpkg.com/array-differ/-/array-differ-3.0.0.tgz#3cbb3d0f316810eafcc47624734237d6aee4ae6b"
1076+
integrity sha512-THtfYS6KtME/yIAhKjZ2ul7XI96lQGHRputJQHO80LAWQnuGP4iCIN8vdMRboGbIEYBwU33q8Tch1os2+X0kMg==
1077+
10681078
array-includes@^3.1.4:
10691079
version "3.1.5"
10701080
resolved "https://registry.yarnpkg.com/array-includes/-/array-includes-3.1.5.tgz#2c320010db8d31031fd2a5f6b3bbd4b1aad31bdb"
@@ -1096,6 +1106,11 @@ arrify@^1.0.1:
10961106
resolved "https://registry.yarnpkg.com/arrify/-/arrify-1.0.1.tgz#898508da2226f380df904728456849c1501a4b0d"
10971107
integrity sha512-3CYzex9M9FGQjCGMGyi6/31c8GJbgb0qGyrx5HWxPd0aCwh4cB2YjMb2Xf9UuoogrMrlO9cTqnB5rI5GHZTcUA==
10981108

1109+
arrify@^2.0.1:
1110+
version "2.0.1"
1111+
resolved "https://registry.yarnpkg.com/arrify/-/arrify-2.0.1.tgz#c9655e9331e0abcd588d2a7cad7e9956f66701fa"
1112+
integrity sha512-3duEwti880xqi4eAMN8AyR4a0ByT90zoYdLlevfrvU43vb0YZwZVfxOgxWrLXXXpyugL0hNZc9G6BiB5B3nUug==
1113+
10991114
astral-regex@^2.0.0:
11001115
version "2.0.0"
11011116
resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-2.0.0.tgz#483143c567aeed4785759c0865786dc77d7d2e31"
@@ -1335,6 +1350,14 @@ chalk@^2.0.0, chalk@^2.4.1, chalk@^2.4.2:
13351350
escape-string-regexp "^1.0.5"
13361351
supports-color "^5.3.0"
13371352

1353+
chalk@^3.0.0:
1354+
version "3.0.0"
1355+
resolved "https://registry.yarnpkg.com/chalk/-/chalk-3.0.0.tgz#3f73c2bf526591f574cc492c51e2456349f844e4"
1356+
integrity sha512-4D3B6Wf41KOYRFdszmDqMCGq5VV/uMAB273JILmO+3jAlh8X4qDtdtgCR3fxtbLEMzSx22QdhnDcJvu2u1fVwg==
1357+
dependencies:
1358+
ansi-styles "^4.1.0"
1359+
supports-color "^7.1.0"
1360+
13381361
chalk@^4.0.0, chalk@^4.1.0:
13391362
version "4.1.2"
13401363
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01"
@@ -1559,7 +1582,7 @@ cosmiconfig@^7.0.0:
15591582
path-type "^4.0.0"
15601583
yaml "^1.10.0"
15611584

1562-
cross-spawn@^7.0.2, cross-spawn@^7.0.3:
1585+
cross-spawn@^7.0.0, cross-spawn@^7.0.2, cross-spawn@^7.0.3:
15631586
version "7.0.3"
15641587
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
15651588
integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==
@@ -2014,6 +2037,21 @@ esutils@^2.0.2:
20142037
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64"
20152038
integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==
20162039

2040+
execa@^4.0.0:
2041+
version "4.1.0"
2042+
resolved "https://registry.yarnpkg.com/execa/-/execa-4.1.0.tgz#4e5491ad1572f2f17a77d388c6c857135b22847a"
2043+
integrity sha512-j5W0//W7f8UxAn8hXVnwG8tLwdiUy4FJLcSupCg6maBYZDpyBvTApK7KyuI4bKj8KOh1r2YH+6ucuYtJv1bTZA==
2044+
dependencies:
2045+
cross-spawn "^7.0.0"
2046+
get-stream "^5.0.0"
2047+
human-signals "^1.1.1"
2048+
is-stream "^2.0.0"
2049+
merge-stream "^2.0.0"
2050+
npm-run-path "^4.0.0"
2051+
onetime "^5.1.0"
2052+
signal-exit "^3.0.2"
2053+
strip-final-newline "^2.0.0"
2054+
20172055
execa@^5.0.0:
20182056
version "5.1.1"
20192057
resolved "https://registry.yarnpkg.com/execa/-/execa-5.1.1.tgz#f80ad9cbf4298f7bd1d4c9555c21e93741c411dd"
@@ -2466,6 +2504,11 @@ http-cache-semantics@^4.0.0:
24662504
resolved "https://registry.yarnpkg.com/http-cache-semantics/-/http-cache-semantics-4.1.0.tgz#49e91c5cbf36c9b94bcfcd71c23d5249ec74e390"
24672505
integrity sha512-carPklcUh7ROWRK7Cv27RPtdhYhUsela/ue5/jKzjegVvXDqM2ILE9Q2BGn9JZJh1g87cp56su/FgQSzcWS8cQ==
24682506

2507+
human-signals@^1.1.1:
2508+
version "1.1.1"
2509+
resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-1.1.1.tgz#c5b1cd14f50aeae09ab6c59fe63ba3395fe4dfa3"
2510+
integrity sha512-SEQu7vl8KjNL2eoGBLF3+wAjpsNfA9XMlXAYj/3EdaNfAlxKthD1xjEQfGOUhllCGGJVNY34bRr6lPINhNjyZw==
2511+
24692512
human-signals@^2.1.0:
24702513
version "2.1.0"
24712514
resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-2.1.0.tgz#dc91fcba42e4d06e4abaed33b3e7a3c02f514ea0"
@@ -2495,7 +2538,7 @@ ignore-walk@^3.0.3:
24952538
dependencies:
24962539
minimatch "^3.0.4"
24972540

2498-
ignore@^5.2.0:
2541+
ignore@^5.1.4, ignore@^5.2.0:
24992542
version "5.2.0"
25002543
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.2.0.tgz#6d3bac8fa7fe0d45d9f9be7bac2fc279577e345a"
25012544
integrity sha512-CmxgYGiEPCLhfLnpPp1MoRmifwEIOgjcHXxOBjv7mY96c+eWScsOP9c112ZyLdWHi0FxHjI+4uVhKYp/gcdRmQ==
@@ -3726,6 +3769,11 @@ mkdirp@^1.0.4:
37263769
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
37273770
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==
37283771

3772+
mri@^1.1.5:
3773+
version "1.2.0"
3774+
resolved "https://registry.yarnpkg.com/mri/-/mri-1.2.0.tgz#6721480fec2a11a4889861115a48b6cbe7cc8f0b"
3775+
integrity sha512-tzzskb3bG8LvYGFF/mDTpq3jpI6Q9wc3LEmBaghu+DdCssd1FakN7Bc0hVNmEyGq1bq3RgfkCb3cmQLpNPOroA==
3776+
37293777
37303778
version "2.0.0"
37313779
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
@@ -3741,6 +3789,17 @@ ms@^2.1.1:
37413789
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
37423790
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==
37433791

3792+
multimatch@^4.0.0:
3793+
version "4.0.0"
3794+
resolved "https://registry.yarnpkg.com/multimatch/-/multimatch-4.0.0.tgz#8c3c0f6e3e8449ada0af3dd29efb491a375191b3"
3795+
integrity sha512-lDmx79y1z6i7RNx0ZGCPq1bzJ6ZoDDKbvh7jxr9SJcWLkShMzXrHbYVpTdnhNM5MXpDUxCQ4DgqVttVXlBgiBQ==
3796+
dependencies:
3797+
"@types/minimatch" "^3.0.3"
3798+
array-differ "^3.0.0"
3799+
array-union "^2.1.0"
3800+
arrify "^2.0.1"
3801+
minimatch "^3.0.4"
3802+
37443803
37453804
version "0.0.7"
37463805
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab"
@@ -3875,7 +3934,7 @@ npm-name@^6.0.1:
38753934
registry-url "^5.1.0"
38763935
validate-npm-package-name "^3.0.0"
38773936

3878-
npm-run-path@^4.0.1:
3937+
npm-run-path@^4.0.0, npm-run-path@^4.0.1:
38793938
version "4.0.1"
38803939
resolved "https://registry.yarnpkg.com/npm-run-path/-/npm-run-path-4.0.1.tgz#b7ecd1e5ed53da8e37a55e1c2269e0b97ed748ea"
38813940
integrity sha512-S48WzZW777zhNIrn7gxOlISNAqi9ZC/uQFnRdbeIHhZhCA6UqpkOT8T1G7BvfdgP4Er8gF4sUbaS0i7QvIfCWw==
@@ -4256,6 +4315,18 @@ pretty-format@^28.0.0, pretty-format@^28.1.3:
42564315
ansi-styles "^5.0.0"
42574316
react-is "^18.0.0"
42584317

4318+
pretty-quick@^3.1.3:
4319+
version "3.1.3"
4320+
resolved "https://registry.yarnpkg.com/pretty-quick/-/pretty-quick-3.1.3.tgz#15281108c0ddf446675157ca40240099157b638e"
4321+
integrity sha512-kOCi2FJabvuh1as9enxYmrnBC6tVMoVOenMaBqRfsvBHB0cbpYHjdQEpSglpASDFEXVwplpcGR4CLEaisYAFcA==
4322+
dependencies:
4323+
chalk "^3.0.0"
4324+
execa "^4.0.0"
4325+
find-up "^4.1.0"
4326+
ignore "^5.1.4"
4327+
mri "^1.1.5"
4328+
multimatch "^4.0.0"
4329+
42594330
prompts@^2.0.1:
42604331
version "2.4.2"
42614332
resolved "https://registry.yarnpkg.com/prompts/-/prompts-2.4.2.tgz#7b57e73b3a48029ad10ebd44f74b01722a4cb069"

0 commit comments

Comments
 (0)