Skip to content

Commit

Permalink
Improve performance significantly
Browse files Browse the repository at this point in the history
  • Loading branch information
paleite committed Jul 26, 2022
1 parent 97d9a17 commit 845deaa
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 19 deletions.
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install pretty-quick --staged
npx --no-install lint-staged
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"np": "^7.6.1",
"pinst": "^3.0.0",
"prettier": "^2.6.2",
"pretty-quick": "^3.1.3",
"size-limit": "^7.0.8",
"terser": "^5.14.2",
"ts-jest": "^28.0.4",
Expand Down
14 changes: 9 additions & 5 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const getDiffForFile = (filePath: string, staged = false): string => {
return child_process.execFileSync(COMMAND, args).toString();
};

const getDiffFileList = (): string[] => {
const getDiffFileList = (staged = false): string[] => {
const args = [
"diff",
"--diff-algorithm=histogram",
Expand All @@ -34,9 +34,12 @@ const getDiffFileList = (): string[] => {
"--name-only",
"--no-ext-diff",
"--relative",
"--staged",
staged && "--staged",
process.env.ESLINT_PLUGIN_DIFF_COMMIT ?? "HEAD",
];
].reduce<string[]>(
(acc, cur) => (typeof cur === "string" ? [...acc, cur] : acc),
[]
);

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

const getRangesForDiff = (diff: string): Range[] =>
diff.split("\n").reduce<Range[]>((ranges, line) => {
const getRangesForDiff = (diff: string): Range[] => {
return diff.split("\n").reduce<Range[]>((ranges, line) => {
if (!isHunkHeader(line)) {
return ranges;
}
Expand All @@ -138,6 +141,7 @@ const getRangesForDiff = (diff: string): Range[] =>

return [...ranges, range];
}, []);
};

export {
getDiffFileList,
Expand Down
37 changes: 26 additions & 11 deletions src/processors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,46 @@ const STAGED = true;
* them from being processed in the first place, as a performance optimization.
* This is increasingly useful the more files there are in the repository.
*/
const getPreProcessor =
(staged = false) =>
(text: string, filename: string) => {
const getPreProcessor = (staged = false) => {
const untrackedFileList = getUntrackedFileList(staged);
const diffFileList = getDiffFileList(staged);
return (text: string, filename: string) => {
const shouldBeProcessed =
process.env.VSCODE_CLI !== undefined ||
!staged ||
getDiffFileList().includes(filename);
diffFileList.includes(filename) ||
untrackedFileList.includes(filename);

return shouldBeProcessed ? [text] : [];
};
};

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

const getPostProcessor =
(staged = false) =>
(
const getPostProcessor = (staged = false) => {
const untrackedFileList = getUntrackedFileList(staged);

return (
messages: Linter.LintMessage[][],
filename: string
): Linter.LintMessage[] => {
if (!staged && getUntrackedFileList(staged).includes(filename)) {
if (messages.length === 0) {
// No need to filter, just return
return [];
}

if (untrackedFileList.includes(filename)) {
// We don't need to filter the messages of untracked files because they
// would all be kept anyway, so we return them as-is.
return messages.flat();
}

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

const diff = getDiffForFile(filename, staged);
const rangesForDiff = getRangesForDiff(getDiffForFile(filename, staged));

return messages
.map((message) => {
Expand All @@ -67,7 +81,7 @@ const getPostProcessor =
return true;
}

const isLineWithinSomeRange = getRangesForDiff(diff).some(
const isLineWithinSomeRange = rangesForDiff.some(
isLineWithinRange(line)
);

Expand All @@ -78,6 +92,7 @@ const getPostProcessor =
})
.reduce((a, b) => a.concat(b), []);
};
};

const getProcessors = (staged = false): Required<Linter.Processor> => ({
preprocess: getPreProcessor(staged),
Expand Down
77 changes: 74 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,11 @@
dependencies:
"@types/node" "*"

"@types/minimatch@^3.0.3":
version "3.0.5"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.5.tgz#1001cc5e6a3704b83c236027e77f2f58ea010f40"
integrity sha512-Klz949h02Gz2uZCMGwDUSDS1YBlTdDDgbWHi+81l29tQALUtvz4rAYi5uoVhE5Lagoq6DeqAUlbrHvW/mXDgdQ==

"@types/minimist@^1.2.0":
version "1.2.2"
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c"
Expand Down Expand Up @@ -1065,6 +1070,11 @@ argparse@^2.0.1:
resolved "https://registry.yarnpkg.com/argparse/-/argparse-2.0.1.tgz#246f50f3ca78a3240f6c997e8a9bd1eac49e4b38"
integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==

array-differ@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/array-differ/-/array-differ-3.0.0.tgz#3cbb3d0f316810eafcc47624734237d6aee4ae6b"
integrity sha512-THtfYS6KtME/yIAhKjZ2ul7XI96lQGHRputJQHO80LAWQnuGP4iCIN8vdMRboGbIEYBwU33q8Tch1os2+X0kMg==

array-includes@^3.1.4:
version "3.1.5"
resolved "https://registry.yarnpkg.com/array-includes/-/array-includes-3.1.5.tgz#2c320010db8d31031fd2a5f6b3bbd4b1aad31bdb"
Expand Down Expand Up @@ -1096,6 +1106,11 @@ arrify@^1.0.1:
resolved "https://registry.yarnpkg.com/arrify/-/arrify-1.0.1.tgz#898508da2226f380df904728456849c1501a4b0d"
integrity sha512-3CYzex9M9FGQjCGMGyi6/31c8GJbgb0qGyrx5HWxPd0aCwh4cB2YjMb2Xf9UuoogrMrlO9cTqnB5rI5GHZTcUA==

arrify@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/arrify/-/arrify-2.0.1.tgz#c9655e9331e0abcd588d2a7cad7e9956f66701fa"
integrity sha512-3duEwti880xqi4eAMN8AyR4a0ByT90zoYdLlevfrvU43vb0YZwZVfxOgxWrLXXXpyugL0hNZc9G6BiB5B3nUug==

astral-regex@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-2.0.0.tgz#483143c567aeed4785759c0865786dc77d7d2e31"
Expand Down Expand Up @@ -1335,6 +1350,14 @@ chalk@^2.0.0, chalk@^2.4.1, chalk@^2.4.2:
escape-string-regexp "^1.0.5"
supports-color "^5.3.0"

chalk@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-3.0.0.tgz#3f73c2bf526591f574cc492c51e2456349f844e4"
integrity sha512-4D3B6Wf41KOYRFdszmDqMCGq5VV/uMAB273JILmO+3jAlh8X4qDtdtgCR3fxtbLEMzSx22QdhnDcJvu2u1fVwg==
dependencies:
ansi-styles "^4.1.0"
supports-color "^7.1.0"

chalk@^4.0.0, chalk@^4.1.0:
version "4.1.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01"
Expand Down Expand Up @@ -1559,7 +1582,7 @@ cosmiconfig@^7.0.0:
path-type "^4.0.0"
yaml "^1.10.0"

cross-spawn@^7.0.2, cross-spawn@^7.0.3:
cross-spawn@^7.0.0, cross-spawn@^7.0.2, cross-spawn@^7.0.3:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==
Expand Down Expand Up @@ -2014,6 +2037,21 @@ esutils@^2.0.2:
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64"
integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==

execa@^4.0.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/execa/-/execa-4.1.0.tgz#4e5491ad1572f2f17a77d388c6c857135b22847a"
integrity sha512-j5W0//W7f8UxAn8hXVnwG8tLwdiUy4FJLcSupCg6maBYZDpyBvTApK7KyuI4bKj8KOh1r2YH+6ucuYtJv1bTZA==
dependencies:
cross-spawn "^7.0.0"
get-stream "^5.0.0"
human-signals "^1.1.1"
is-stream "^2.0.0"
merge-stream "^2.0.0"
npm-run-path "^4.0.0"
onetime "^5.1.0"
signal-exit "^3.0.2"
strip-final-newline "^2.0.0"

execa@^5.0.0:
version "5.1.1"
resolved "https://registry.yarnpkg.com/execa/-/execa-5.1.1.tgz#f80ad9cbf4298f7bd1d4c9555c21e93741c411dd"
Expand Down Expand Up @@ -2466,6 +2504,11 @@ http-cache-semantics@^4.0.0:
resolved "https://registry.yarnpkg.com/http-cache-semantics/-/http-cache-semantics-4.1.0.tgz#49e91c5cbf36c9b94bcfcd71c23d5249ec74e390"
integrity sha512-carPklcUh7ROWRK7Cv27RPtdhYhUsela/ue5/jKzjegVvXDqM2ILE9Q2BGn9JZJh1g87cp56su/FgQSzcWS8cQ==

human-signals@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-1.1.1.tgz#c5b1cd14f50aeae09ab6c59fe63ba3395fe4dfa3"
integrity sha512-SEQu7vl8KjNL2eoGBLF3+wAjpsNfA9XMlXAYj/3EdaNfAlxKthD1xjEQfGOUhllCGGJVNY34bRr6lPINhNjyZw==

human-signals@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-2.1.0.tgz#dc91fcba42e4d06e4abaed33b3e7a3c02f514ea0"
Expand Down Expand Up @@ -2495,7 +2538,7 @@ ignore-walk@^3.0.3:
dependencies:
minimatch "^3.0.4"

ignore@^5.2.0:
ignore@^5.1.4, ignore@^5.2.0:
version "5.2.0"
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.2.0.tgz#6d3bac8fa7fe0d45d9f9be7bac2fc279577e345a"
integrity sha512-CmxgYGiEPCLhfLnpPp1MoRmifwEIOgjcHXxOBjv7mY96c+eWScsOP9c112ZyLdWHi0FxHjI+4uVhKYp/gcdRmQ==
Expand Down Expand Up @@ -3726,6 +3769,11 @@ mkdirp@^1.0.4:
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==

mri@^1.1.5:
version "1.2.0"
resolved "https://registry.yarnpkg.com/mri/-/mri-1.2.0.tgz#6721480fec2a11a4889861115a48b6cbe7cc8f0b"
integrity sha512-tzzskb3bG8LvYGFF/mDTpq3jpI6Q9wc3LEmBaghu+DdCssd1FakN7Bc0hVNmEyGq1bq3RgfkCb3cmQLpNPOroA==

ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
Expand All @@ -3741,6 +3789,17 @@ ms@^2.1.1:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==

multimatch@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/multimatch/-/multimatch-4.0.0.tgz#8c3c0f6e3e8449ada0af3dd29efb491a375191b3"
integrity sha512-lDmx79y1z6i7RNx0ZGCPq1bzJ6ZoDDKbvh7jxr9SJcWLkShMzXrHbYVpTdnhNM5MXpDUxCQ4DgqVttVXlBgiBQ==
dependencies:
"@types/minimatch" "^3.0.3"
array-differ "^3.0.0"
array-union "^2.1.0"
arrify "^2.0.1"
minimatch "^3.0.4"

mute-stream@0.0.7:
version "0.0.7"
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab"
Expand Down Expand Up @@ -3875,7 +3934,7 @@ npm-name@^6.0.1:
registry-url "^5.1.0"
validate-npm-package-name "^3.0.0"

npm-run-path@^4.0.1:
npm-run-path@^4.0.0, npm-run-path@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/npm-run-path/-/npm-run-path-4.0.1.tgz#b7ecd1e5ed53da8e37a55e1c2269e0b97ed748ea"
integrity sha512-S48WzZW777zhNIrn7gxOlISNAqi9ZC/uQFnRdbeIHhZhCA6UqpkOT8T1G7BvfdgP4Er8gF4sUbaS0i7QvIfCWw==
Expand Down Expand Up @@ -4256,6 +4315,18 @@ pretty-format@^28.0.0, pretty-format@^28.1.3:
ansi-styles "^5.0.0"
react-is "^18.0.0"

pretty-quick@^3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/pretty-quick/-/pretty-quick-3.1.3.tgz#15281108c0ddf446675157ca40240099157b638e"
integrity sha512-kOCi2FJabvuh1as9enxYmrnBC6tVMoVOenMaBqRfsvBHB0cbpYHjdQEpSglpASDFEXVwplpcGR4CLEaisYAFcA==
dependencies:
chalk "^3.0.0"
execa "^4.0.0"
find-up "^4.1.0"
ignore "^5.1.4"
mri "^1.1.5"
multimatch "^4.0.0"

prompts@^2.0.1:
version "2.4.2"
resolved "https://registry.yarnpkg.com/prompts/-/prompts-2.4.2.tgz#7b57e73b3a48029ad10ebd44f74b01722a4cb069"
Expand Down

0 comments on commit 845deaa

Please sign in to comment.