Skip to content

Commit c2c0323

Browse files
committed
Improve performance, but only slightly
1 parent 6f781b8 commit c2c0323

2 files changed

Lines changed: 12 additions & 9 deletions

File tree

packages/knip/src/reporters/util/util.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ const sortByPos = (a: Issue, b: Issue) => {
5151
const highlightSymbol =
5252
(issue: Issue) =>
5353
(_: unknown): string => {
54-
if (issue.specifier && issue.specifier !== issue.symbol && issue.specifier.includes(issue.symbol)) {
55-
const parts = issue.specifier.split(issue.symbol);
56-
const rest = parts.slice(1).join('');
57-
return [dim(parts[0]), bright(issue.symbol), dim(rest)].join('');
54+
const { specifier, symbol } = issue;
55+
if (specifier && specifier !== symbol) {
56+
const idx = specifier.indexOf(symbol);
57+
if (idx !== -1) {
58+
return `${dim(specifier.slice(0, idx))}${bright(symbol)}${dim(specifier.slice(idx + symbol.length))}`;
59+
}
5860
}
59-
return issue.symbol;
61+
return symbol;
6062
};
6163

6264
export const getTableForType = (

packages/knip/src/util/glob-core.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { debugLogObject } from './debug.ts';
88
import { isDirectory, isFile } from './fs.ts';
99
import { timerify } from './Performance.ts';
1010
import { expandIgnorePatterns, parseAndConvertGitignorePatterns } from './parse-and-convert-gitignores.ts';
11-
import { basename, dirname, join, relative, toPosix } from './path.ts';
11+
import { dirname, join, relative, toPosix } from './path.ts';
1212

1313
type Options = { gitignore: boolean; cwd: string };
1414

@@ -167,15 +167,16 @@ export const findAndParseGitignores = async (cwd: string, workspaceDirs?: Set<st
167167
}
168168
}
169169

170+
const cwdPrefixLen = cwd.length + 1;
170171
const walkGitignores = async () => {
171172
await new fdir()
172173
.withFullPaths()
173174
.exclude((_dirName: string, dirPath: string) => {
174-
const absPath = toPosix(dirPath.endsWith('/') ? dirPath.slice(0, -1) : dirPath);
175-
return (isRelevantDir && !isRelevantDir(absPath)) || getMatcher()(relative(cwd, absPath));
175+
const absPath = toPosix(dirPath.slice(0, -1));
176+
return (isRelevantDir && !isRelevantDir(absPath)) || getMatcher()(absPath.slice(cwdPrefixLen));
176177
})
177178
.filter((filePath: string, isDir: boolean) => {
178-
if (isDir || basename(filePath) !== '.gitignore') return false;
179+
if (isDir || !filePath.endsWith('/.gitignore')) return false;
179180
addFile(filePath);
180181
return true;
181182
})

0 commit comments

Comments
 (0)