From c2197737538ac4a04f51ac76016141f5eeab6c55 Mon Sep 17 00:00:00 2001 From: Dmitry Fuks <d.fuks@dodobrands.io> Date: Wed, 17 Jul 2024 03:06:26 +0300 Subject: [PATCH] simplify code for project check --- .yarn/versions/62d5e99c.yml | 2 ++ packages/plugin/src/cli/index.ts | 20 +++------------- packages/plugin/src/cli/utils.ts | 39 ++++++++------------------------ 3 files changed, 14 insertions(+), 47 deletions(-) create mode 100644 .yarn/versions/62d5e99c.yml diff --git a/.yarn/versions/62d5e99c.yml b/.yarn/versions/62d5e99c.yml new file mode 100644 index 0000000..b212163 --- /dev/null +++ b/.yarn/versions/62d5e99c.yml @@ -0,0 +1,2 @@ +releases: + ts-overrides-plugin: patch diff --git a/packages/plugin/src/cli/index.ts b/packages/plugin/src/cli/index.ts index 8af424b..c196d37 100644 --- a/packages/plugin/src/cli/index.ts +++ b/packages/plugin/src/cli/index.ts @@ -3,23 +3,19 @@ import type { PluginConfig, ProgramTransformer } from 'ts-patch'; import type ts from 'typescript'; import { type Override } from '../types/Override'; -import { getDiagnosticForFile, getDiagnosticsForProject, getOverridePrograms, type OverridePrograms } from './utils'; +import { getDiagnosticForFile, getDiagnosticsForProject, getOverridePrograms } from './utils'; interface CliPluginConfig extends PluginConfig { overrides: Override[]; } -let overridePrograms: OverridePrograms | null = null; - const plugin: ProgramTransformer = (program, host, pluginConfig, extras) => { const { overrides: overridesFromConfig } = pluginConfig as CliPluginConfig; const { plugins, ...defaultCompilerOptions } = program.getCompilerOptions(); const sortedOverridesFromConfig = [...overridesFromConfig].reverse(); const rootPath = defaultCompilerOptions.project ? path.dirname(defaultCompilerOptions.project) : process.cwd(); - overridePrograms = null; - - overridePrograms = getOverridePrograms( + const overridePrograms = getOverridePrograms( rootPath, extras.ts, sortedOverridesFromConfig, @@ -48,17 +44,7 @@ const plugin: ProgramTransformer = (program, host, pluginConfig, extras) => { return ((sourceFile, cancellationToken) => { // for build ForkTsCheckerWebpackPlugin and tspc if (!sourceFile) { - overridePrograms = null; - - return getDiagnosticsForProject( - rootPath, - extras.ts, - sortedOverridesFromConfig, - target, - defaultCompilerOptions, - cancellationToken, - host, - ); + return getDiagnosticsForProject(target, overridePrograms, cancellationToken); } // for ts-loader - watch and build diff --git a/packages/plugin/src/cli/utils.ts b/packages/plugin/src/cli/utils.ts index d7835f0..ba1210c 100644 --- a/packages/plugin/src/cli/utils.ts +++ b/packages/plugin/src/cli/utils.ts @@ -33,38 +33,17 @@ const getOverrideProgram = ( }; export const getDiagnosticsForProject = ( - rootPath: string, - typescript: typeof ts, - overridesFromConfig: Override[], program: ts.Program, - defaultCompilerOptions: ts.CompilerOptions, + overridePrograms: OverridePrograms, cancellationToken?: ts.CancellationToken, - host?: ts.CompilerHost, ): ts.Diagnostic[] => { - let filesToOriginalDiagnostic: string[] = [...program.getRootFileNames()]; - - const resultDiagnostic: ts.Diagnostic[] = overridesFromConfig.flatMap(override => { - const { overrideProgram, filesToCurrentOverrideDiagnostic } = getOverrideProgram( - rootPath, - typescript, - override, - filesToOriginalDiagnostic, - defaultCompilerOptions, - host, - ); - - filesToOriginalDiagnostic = filesToOriginalDiagnostic.filter( - fileName => !filesToCurrentOverrideDiagnostic.includes(fileName), - ); - - return filesToCurrentOverrideDiagnostic.flatMap(fileName => { - const sourceFile = overrideProgram.getSourceFile(fileName); - - return sourceFile ? overrideProgram.getSemanticDiagnostics(sourceFile, cancellationToken) : []; - }); - }); + const resultDiagnostic: ts.Diagnostic[] = overridePrograms.resultOverrides.flatMap(override => + override + .getRootFileNames() + .flatMap(fileName => override.getSemanticDiagnostics(override.getSourceFile(fileName), cancellationToken)), + ); - const originalDiagnostics = filesToOriginalDiagnostic.flatMap(fileName => { + const originalDiagnostics = overridePrograms.filesToOriginalDiagnostic.flatMap(fileName => { const sourceFile = program.getSourceFile(fileName); return sourceFile ? program.getSemanticDiagnostics(sourceFile, cancellationToken) : []; @@ -112,7 +91,7 @@ export interface OverridePrograms { } export const getDiagnosticForFile = ( - overridePrograms: OverridePrograms | null, + overridePrograms: OverridePrograms, target: ts.Program, sourceFile: ts.SourceFile, method: 'getSemanticDiagnostics' | 'getBindAndCheckDiagnostics', @@ -120,7 +99,7 @@ export const getDiagnosticForFile = ( ): readonly ts.Diagnostic[] => { const { fileName } = sourceFile; - if (!overridePrograms || overridePrograms.filesToOriginalDiagnostic.includes(fileName)) { + if (overridePrograms.filesToOriginalDiagnostic.includes(fileName)) { return target[method](sourceFile, cancellationToken); }