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); }