From d8164960d21336d4a726d8179b314f05fe3aeb22 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 20 Feb 2023 17:29:28 +1030 Subject: [PATCH] feat(typescript-estree): expose a wrapper cache clearing function for advanced usecases (#6476) --- .gitignore | 1 + .../typescript-estree/src/clear-caches.ts | 21 +++++++++++++++++++ packages/typescript-estree/src/index.ts | 3 +-- .../src/parseSettings/createParseSettings.ts | 4 ++++ .../src/parseSettings/resolveProjectList.ts | 4 ++++ 5 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 packages/typescript-estree/src/clear-caches.ts diff --git a/.gitignore b/.gitignore index 0973f04542e..7fad8b15206 100644 --- a/.gitignore +++ b/.gitignore @@ -76,6 +76,7 @@ jspm_packages/ .idea dist _ts3.4 +_ts4.2 *.tsbuildinfo .watchmanconfig .rollup.cache diff --git a/packages/typescript-estree/src/clear-caches.ts b/packages/typescript-estree/src/clear-caches.ts new file mode 100644 index 00000000000..c223fe85275 --- /dev/null +++ b/packages/typescript-estree/src/clear-caches.ts @@ -0,0 +1,21 @@ +import { clearWatchCaches } from './create-program/getWatchProgramsForProjects'; +import { clearProgramCache as clearProgramCacheOriginal } from './parser'; +import { clearTSConfigMatchCache } from './parseSettings/createParseSettings'; +import { clearGlobCache } from './parseSettings/resolveProjectList'; + +/** + * Clears all of the internal caches. + * Generally you shouldn't need or want to use this. + * Examples of intended uses: + * - In tests to reset parser state to keep tests isolated. + * - In custom lint tooling that iteratively lints one project at a time to prevent OOMs. + */ +export function clearCaches(): void { + clearProgramCacheOriginal(); + clearWatchCaches(); + clearTSConfigMatchCache(); + clearGlobCache(); +} + +// TODO - delete this in next major +export const clearProgramCache = clearCaches(); diff --git a/packages/typescript-estree/src/index.ts b/packages/typescript-estree/src/index.ts index bc7ed6024f3..511ba6c769c 100644 --- a/packages/typescript-estree/src/index.ts +++ b/packages/typescript-estree/src/index.ts @@ -5,16 +5,15 @@ export { parseWithNodeMaps, ParseAndGenerateServicesResult, ParseWithNodeMapsResult, - clearProgramCache, } from './parser'; export { ParserServices, TSESTreeOptions } from './parser-options'; export { simpleTraverse } from './simple-traverse'; export * from './ts-estree'; -export { clearWatchCaches as clearCaches } from './create-program/getWatchProgramsForProjects'; export { createProgramFromConfigFile as createProgram } from './create-program/useProvidedPrograms'; export * from './create-program/getScriptKind'; export { typescriptVersionIsAtLeast } from './version-check'; export * from './getModifiers'; +export * from './clear-caches'; // re-export for backwards-compat export { visitorKeys } from '@typescript-eslint/visitor-keys'; diff --git a/packages/typescript-estree/src/parseSettings/createParseSettings.ts b/packages/typescript-estree/src/parseSettings/createParseSettings.ts index 028088765a2..7518b0b686c 100644 --- a/packages/typescript-estree/src/parseSettings/createParseSettings.ts +++ b/packages/typescript-estree/src/parseSettings/createParseSettings.ts @@ -121,6 +121,10 @@ export function createParseSettings( return parseSettings; } +export function clearTSConfigMatchCache(): void { + TSCONFIG_MATCH_CACHE?.clear(); +} + /** * Ensures source code is a string. */ diff --git a/packages/typescript-estree/src/parseSettings/resolveProjectList.ts b/packages/typescript-estree/src/parseSettings/resolveProjectList.ts index 72e9539d2b9..f9d935b88cd 100644 --- a/packages/typescript-estree/src/parseSettings/resolveProjectList.ts +++ b/packages/typescript-estree/src/parseSettings/resolveProjectList.ts @@ -21,6 +21,10 @@ const log = debug( let RESOLUTION_CACHE: ExpiringCache | null = null; +export function clearGlobCache(): void { + RESOLUTION_CACHE?.clear(); +} + /** * Normalizes, sanitizes, resolves and filters the provided project paths */