diff --git a/packages/example/src/global.d.ts b/packages/example/src/global.d.ts new file mode 100644 index 0000000..a40048a --- /dev/null +++ b/packages/example/src/global.d.ts @@ -0,0 +1 @@ +declare const GLOBAL_VAR: string | undefined; diff --git a/packages/example/src/legacy/getDate.ts b/packages/example/src/legacy/getDate.ts index 2b74ad3..3ef1b60 100644 --- a/packages/example/src/legacy/getDate.ts +++ b/packages/example/src/legacy/getDate.ts @@ -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', diff --git a/packages/example/src/modern/getDate.ts b/packages/example/src/modern/getDate.ts index 9891742..d28c1bb 100644 --- a/packages/example/src/modern/getDate.ts +++ b/packages/example/src/modern/getDate.ts @@ -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', diff --git a/packages/plugin/src/cli/utils.ts b/packages/plugin/src/cli/utils.ts index ba1210c..56aa8c1 100644 --- a/packages/plugin/src/cli/utils.ts +++ b/packages/plugin/src/cli/utils.ts @@ -10,6 +10,7 @@ const getOverrideProgram = ( override: Override, filesToOriginalDiagnostic: string[], defaultCompilerOptions: ts.CompilerOptions, + dTsFiles: string[], host?: ts.CompilerHost, ): { overrideProgram: ts.Program; @@ -21,7 +22,7 @@ const getOverrideProgram = ( ); const overrideProgram = typescript.createProgram( - filesToCurrentOverrideDiagnostic, + [...filesToCurrentOverrideDiagnostic, ...dTsFiles], { ...defaultCompilerOptions, ...typescript.convertCompilerOptionsFromJson(override.compilerOptions, rootPath).options, @@ -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( @@ -72,6 +74,7 @@ export const getOverridePrograms = ( override, filesToOriginalDiagnostic, defaultCompilerOptions, + dTsFiles, host, ); diff --git a/packages/plugin/src/ide/index.ts b/packages/plugin/src/ide/index.ts index 188959e..8a12156 100644 --- a/packages/plugin/src/ide/index.ts +++ b/packages/plugin/src/ide/index.ts @@ -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)), ); }, }; @@ -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) => {