Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify code for project check #12

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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