Skip to content

Commit

Permalink
fix global types
Browse files Browse the repository at this point in the history
  • Loading branch information
DiFuks committed Nov 16, 2024
1 parent 8f71a0a commit 2c5cbb4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
1 change: 1 addition & 0 deletions packages/example/src/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare const GLOBAL_VAR: string | undefined;
3 changes: 3 additions & 0 deletions packages/example/src/legacy/getDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export const getDate = (date) => {
// ok with strict: false
modern.split('');

// 'GLOBAL_VAR' not nullable
GLOBAL_VAR.toLowerCase();

return date.toLocaleDateString('ru-RU', {
day: 'numeric',
month: 'long',
Expand Down
3 changes: 3 additions & 0 deletions packages/example/src/modern/getDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export const getDate = (date) => {
// 'modern' is possibly 'undefined'
modern.split('');

// 'GLOBAL_VAR' correct types
GLOBAL_VAR.toLowerCase();

return date.toLocaleDateString('ru-RU', {
day: 'numeric',
month: 'long',
Expand Down
5 changes: 4 additions & 1 deletion packages/plugin/src/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const getOverrideProgram = (
override: Override,
filesToOriginalDiagnostic: string[],
defaultCompilerOptions: ts.CompilerOptions,
dTsFiles: string[],
host?: ts.CompilerHost,
): {
overrideProgram: ts.Program;
Expand All @@ -21,7 +22,7 @@ const getOverrideProgram = (
);

const overrideProgram = typescript.createProgram(
filesToCurrentOverrideDiagnostic,
[...filesToCurrentOverrideDiagnostic, ...dTsFiles],
{
...defaultCompilerOptions,
...typescript.convertCompilerOptionsFromJson(override.compilerOptions, rootPath).options,
Expand Down Expand Up @@ -64,6 +65,7 @@ export const getOverridePrograms = (
filesToOriginalDiagnostic: string[];
} => {
let filesToOriginalDiagnostic: string[] = [...rootFileNames];
const dTsFiles = rootFileNames.filter(fileName => fileName.endsWith(`.d.ts`));

const resultOverrides: ts.Program[] = overridesFromConfig.map(override => {
const { overrideProgram, filesToCurrentOverrideDiagnostic } = getOverrideProgram(
Expand All @@ -72,6 +74,7 @@ export const getOverridePrograms = (
override,
filesToOriginalDiagnostic,
defaultCompilerOptions,
dTsFiles,
host,
);

Expand Down
41 changes: 16 additions & 25 deletions packages/plugin/src/ide/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,29 @@ interface IdePluginConfig {
const getOverrideLanguageServices = (
typescript: typeof ts,
overridesFromConfig: Override[],
languageServiceHost: ts.LanguageServiceHost,
project: ts.server.Project,
docRegistry: ts.DocumentRegistry,
): ts.LanguageService[] =>
[...overridesFromConfig].reverse().map(override => {
const overrideLanguageServiceHost: ts.LanguageServiceHost = {
// don't remove this, it's needed for TS 4
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
fileExists: path => !!languageServiceHost.fileExists?.(path),
getCurrentDirectory: (): string => languageServiceHost.getCurrentDirectory(),
getDefaultLibFileName: (options: ts.CompilerOptions): string =>
languageServiceHost.getDefaultLibFileName(options),
getScriptSnapshot: fileName => languageServiceHost.getScriptSnapshot(fileName),
getScriptVersion: fileName => languageServiceHost.getScriptVersion(fileName),
// don't remove this, it's needed for TS 4
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
readFile: (path, encoding) => languageServiceHost.readFile?.(path, encoding),
fileExists: path => project.fileExists(path),
getCurrentDirectory: () => project.getCurrentDirectory(),
getDefaultLibFileName: () => project.getDefaultLibFileName(),
getScriptSnapshot: fileName => project.getScriptSnapshot(fileName),
getScriptVersion: fileName => project.getScriptVersion(fileName),
readFile: path => project.readFile(path),
getCompilationSettings: () => ({
...languageServiceHost.getCompilationSettings(),
...typescript.convertCompilerOptionsFromJson(
override.compilerOptions,
languageServiceHost.getCurrentDirectory(),
).options,
...project.getCompilationSettings(),
...typescript.convertCompilerOptionsFromJson(override.compilerOptions, project.getCurrentDirectory())
.options,
}),
getScriptFileNames: () => {
const originalFiles = languageServiceHost.getScriptFileNames();
const originalFiles = project.getScriptFileNames();
const isMatch = outmatch(override.files);

return originalFiles.filter(fileName =>
isMatch(relative(languageServiceHost.getCurrentDirectory(), fileName)),
return originalFiles.filter(
fileName =>
fileName.endsWith(`.d.ts`) || isMatch(relative(project.getCurrentDirectory(), fileName)),
);
},
};
Expand Down Expand Up @@ -72,14 +66,11 @@ const plugin: ts.server.PluginModuleFactory = ({ typescript }) => ({
const overrideLanguageServices = getOverrideLanguageServices(
typescript,
overridesFromConfig,
info.languageServiceHost,
info.project,
docRegistry,
);

const originalLanguageServiceWithDocRegistry = typescript.createLanguageService(
info.languageServiceHost,
docRegistry,
);
const originalLanguageServiceWithDocRegistry = typescript.createLanguageService(info.project, docRegistry);

return new Proxy(originalLanguageServiceWithDocRegistry, {
get: (target, property: keyof ts.LanguageService) => {
Expand Down

0 comments on commit 2c5cbb4

Please sign in to comment.