Skip to content

Commit

Permalink
simplify, remove useless code
Browse files Browse the repository at this point in the history
  • Loading branch information
DiFuks committed Nov 16, 2024
1 parent 2c5cbb4 commit 51cf9f4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 117 deletions.
2 changes: 2 additions & 0 deletions .yarn/versions/a859e3b4.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
releases:
ts-overrides-plugin: patch
58 changes: 57 additions & 1 deletion packages/plugin/src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,70 @@
import * as path from 'node:path';
import outmatch from 'outmatch';
import type { PluginConfig, ProgramTransformer } from 'ts-patch';
import type ts from 'typescript';

import { type Override } from '../types/Override';
import { getDiagnosticForFile, getDiagnosticsForProject, getOverridePrograms } from './utils';

interface CliPluginConfig extends PluginConfig {
overrides: Override[];
}

export const getOverridePrograms = (
rootPath: string,
typescript: typeof ts,
overridesFromConfig: Override[],
rootFileNames: readonly string[],
defaultCompilerOptions: ts.CompilerOptions,
host?: ts.CompilerHost,
): ts.Program[] => {
const dTsFiles = rootFileNames.filter(fileName => fileName.endsWith(`.d.ts`));

return overridesFromConfig.map(override => {
const isMatch = outmatch(override.files);
const filesToCurrentOverrideDiagnostic: string[] = rootFileNames.filter(fileName =>
isMatch(path.relative(rootPath, fileName)),
);

return typescript.createProgram(
[...filesToCurrentOverrideDiagnostic, ...dTsFiles],
{
...defaultCompilerOptions,
...typescript.convertCompilerOptionsFromJson(override.compilerOptions, rootPath).options,
},
host,
);
});
};

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

const overrideProgramForFile = overridePrograms.find(overrideProgram =>
overrideProgram.getRootFileNames().includes(fileName),
);

return overrideProgramForFile
? overrideProgramForFile[method](sourceFile, cancellationToken)
: target[method](sourceFile, cancellationToken);
};

export const getDiagnosticsForProject = (
program: ts.Program,
overridePrograms: ts.Program[],
cancellationToken?: ts.CancellationToken,
): ts.Diagnostic[] =>
program
.getSourceFiles()
.flatMap(sourceFile =>
getDiagnosticForFile(overridePrograms, program, sourceFile, `getSemanticDiagnostics`, cancellationToken),
);

const plugin: ProgramTransformer = (program, host, pluginConfig, extras) => {
const { overrides: overridesFromConfig } = pluginConfig as CliPluginConfig;
const { plugins, ...defaultCompilerOptions } = program.getCompilerOptions();
Expand Down
116 changes: 0 additions & 116 deletions packages/plugin/src/cli/utils.ts

This file was deleted.

0 comments on commit 51cf9f4

Please sign in to comment.