Skip to content

Commit

Permalink
simplify code for project check
Browse files Browse the repository at this point in the history
  • Loading branch information
DiFuks committed Jul 17, 2024
1 parent 9e39322 commit c219773
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 47 deletions.
2 changes: 2 additions & 0 deletions .yarn/versions/62d5e99c.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
releases:
ts-overrides-plugin: patch
20 changes: 3 additions & 17 deletions packages/plugin/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
39 changes: 9 additions & 30 deletions packages/plugin/src/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) : [];
Expand Down Expand Up @@ -112,15 +91,15 @@ export interface OverridePrograms {
}

export const getDiagnosticForFile = (
overridePrograms: OverridePrograms | null,
overridePrograms: OverridePrograms,
target: ts.Program,
sourceFile: ts.SourceFile,
method: 'getSemanticDiagnostics' | 'getBindAndCheckDiagnostics',
cancellationToken?: ts.CancellationToken,
): readonly ts.Diagnostic[] => {
const { fileName } = sourceFile;

if (!overridePrograms || overridePrograms.filesToOriginalDiagnostic.includes(fileName)) {
if (overridePrograms.filesToOriginalDiagnostic.includes(fileName)) {
return target[method](sourceFile, cancellationToken);
}

Expand Down

0 comments on commit c219773

Please sign in to comment.