Skip to content

Commit 999e52c

Browse files
committed
Make some easy optimizations
1 parent edc37e7 commit 999e52c

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/compiler/core.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,26 @@ namespace ts {
16341634
};
16351635
}
16361636

1637+
export function memoizeOne<ArgsT extends unknown[], ReturnT>(callback: (...args: ArgsT) => ReturnT) {
1638+
let value: ReturnT;
1639+
let cachedArgs: ArgsT;
1640+
return (...args: ArgsT) => {
1641+
const length = args.length;
1642+
if (cachedArgs && cachedArgs.length === length) {
1643+
for (let i = 0; i < length; i++) {
1644+
if (args[i] !== cachedArgs[i]) {
1645+
cachedArgs = args;
1646+
return value = callback(...args);
1647+
}
1648+
}
1649+
return value;
1650+
}
1651+
cachedArgs = args;
1652+
return value = callback(...args);
1653+
};
1654+
}
1655+
1656+
16371657
/**
16381658
* High-order function, composes functions. Note that functions are composed inside-out;
16391659
* for example, `compose(a, b)` is the equivalent of `x => b(a(x))`.

src/compiler/moduleSpecifiers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ namespace ts.moduleSpecifiers {
173173
return [getPathFromPathComponents(aParts), getPathFromPathComponents(bParts)];
174174
}
175175

176-
function discoverProbableSymlinks(files: ReadonlyArray<SourceFile>, getCanonicalFileName: GetCanonicalFileName, cwd: string): ReadonlyMap<string> {
176+
const discoverProbableSymlinks = memoizeOne((files: ReadonlyArray<SourceFile>, getCanonicalFileName: GetCanonicalFileName, cwd: string): ReadonlyMap<string> => {
177177
const result = createMap<string>();
178178
const symlinks = flatten<readonly [string, string]>(mapDefined(files, sf =>
179179
sf.resolvedModules && compact(arrayFrom(mapIterator(sf.resolvedModules.values(), res =>
@@ -183,7 +183,7 @@ namespace ts.moduleSpecifiers {
183183
result.set(commonOriginal, commonResolved);
184184
}
185185
return result;
186-
}
186+
});
187187

188188
/**
189189
* Looks for existing imports that use symlinks to this module.

src/services/codefixes/importFixes.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,9 @@ namespace ts.codefix {
593593
});
594594

595595
function getNodeModulesPackageNameFromFileName(importedFileName: string): string | undefined {
596+
if (!stringContains(importedFileName, "node_modules")) {
597+
return undefined;
598+
}
596599
const specifier = moduleSpecifiers.getModuleSpecifier(
597600
compilerOptions,
598601
from,

0 commit comments

Comments
 (0)