Skip to content

Commit

Permalink
fix: compatible with TS 5.3 (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk authored Dec 21, 2023
1 parent 97fe7d5 commit bd79362
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 21 deletions.
39 changes: 22 additions & 17 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/5_0/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export default function (
ts: typeof import('typescript/lib/tsserverlibrary'),
sys: import('typescript/lib/tsserverlibrary').System,
host: LanguageServiceHost,
createLanguageService: (host: LanguageServiceHost) => LanguageService
createLanguageService: (host: LanguageServiceHost) => LanguageService,
_createProject: typeof createProject = createProject
) {
const hostConfiguration = { preferences: { includePackageJsonAutoImports: 'auto' } as UserPreferences };

Expand All @@ -24,7 +25,7 @@ export default function (
);
}

const project = createProject(
const project = _createProject(
ts,
host,
createLanguageService,
Expand Down
2 changes: 1 addition & 1 deletion src/5_0/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { SymlinkCache } from './symlinkCache';
import { ExportInfoMap } from './exportInfoMap';

export type Project = ReturnType<typeof createProject>;
interface ProjectOptions {
export interface ProjectOptions {
projectService: ProjectService;
compilerOptions: CompilerOptions;
currentDirectory: string;
Expand Down
12 changes: 12 additions & 0 deletions src/5_3/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type { LanguageService, LanguageServiceHost } from 'typescript/lib/tsserverlibrary';
import _50 from '../5_0';
import { createProject } from './project';

export default function (
ts: typeof import('typescript/lib/tsserverlibrary'),
sys: import('typescript/lib/tsserverlibrary').System,
host: LanguageServiceHost,
createLanguageService: (host: LanguageServiceHost) => LanguageService
) {
return _50(ts, sys, host, createLanguageService, createProject);
}
30 changes: 30 additions & 0 deletions src/5_3/project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { LanguageService, LanguageServiceHost } from 'typescript/lib/tsserverlibrary';
import { ProjectOptions, createProject as _createProject } from '../5_0/project';
import { SymlinkCache } from './symlinkCache';

export function createProject(
ts: typeof import('typescript/lib/tsserverlibrary'),
host: LanguageServiceHost,
createLanguageService: (host: LanguageServiceHost) => LanguageService,
options: ProjectOptions
) {
const { createSymlinkCache } = ts as any;
const project = _createProject(ts, host, createLanguageService, options);
project.getSymlinkCache = () => {
if (!project.symlinks) {
project.symlinks = createSymlinkCache(project.getCurrentDirectory(), project.getCanonicalFileName);
}
if (project.program && !(project.symlinks as unknown as SymlinkCache).hasProcessedResolutions()) {
(project.symlinks as unknown as SymlinkCache).setSymlinksFromResolutions(
// @ts-expect-error
project.program.forEachResolvedModule,
// @ts-expect-error
project.program.forEachResolvedTypeReferenceDirective,
// @ts-expect-error
project.program.getAutomaticTypeDirectiveResolutions(),
);
}
return project.symlinks!;
};
return project;
}
33 changes: 33 additions & 0 deletions src/5_3/symlinkCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { MultiMap, SymlinkedDirectory } from "../5_0/symlinkCache";
import type { ModeAwareCache, Path, ResolutionMode, ResolvedModuleWithFailedLookupLocations, ResolvedTypeReferenceDirectiveWithFailedLookupLocations } from "typescript/lib/tsserverlibrary";

export interface SymlinkCache {
/** Gets a map from symlink to realpath. Keys have trailing directory separators. */
getSymlinkedDirectories(): ReadonlyMap<Path, SymlinkedDirectory | false> | undefined;
/** Gets a map from realpath to symlinks. Keys have trailing directory separators. */
getSymlinkedDirectoriesByRealpath(): MultiMap<Path, string> | undefined;
/** Gets a map from symlink to realpath */
getSymlinkedFiles(): ReadonlyMap<Path, string> | undefined;
setSymlinkedDirectory(symlink: string, real: SymlinkedDirectory | false): void;
setSymlinkedFile(symlinkPath: Path, real: string): void;
/**
* @internal
* Uses resolvedTypeReferenceDirectives from program instead of from files, since files
* don't include automatic type reference directives. Must be called only when
* `hasProcessedResolutions` returns false (once per cache instance).
*/
setSymlinksFromResolutions(
forEachResolvedModule: (
callback: (resolution: ResolvedModuleWithFailedLookupLocations, moduleName: string, mode: ResolutionMode, filePath: Path) => void,
) => void,
forEachResolvedTypeReferenceDirective: (
callback: (resolution: ResolvedTypeReferenceDirectiveWithFailedLookupLocations, moduleName: string, mode: ResolutionMode, filePath: Path) => void,
) => void,
typeReferenceDirectives: ModeAwareCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>,
): void;
/**
* @internal
* Whether `setSymlinksFromResolutions` has already been called.
*/
hasProcessedResolutions(): boolean;
}
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import _40 from './4_0';
import _44 from './4_4';
import _47 from './4_7';
import _50 from './5_0';
import _53 from './5_3';

export { PackageJsonAutoImportPreference } from './5_0/projectService';

Expand All @@ -17,7 +18,10 @@ export function createLanguageService(
setPreferences?(preferences: ts.UserPreferences): void;
projectUpdated?(updatedProjectDirectory: string): void;
} {
if (semver.gte(ts.version, '5.0.0')) {
if (semver.gte(ts.version, '5.3.0')) {
return _53(ts, sys, host, createLanguageService);
}
else if (semver.gte(ts.version, '5.0.0')) {
return _50(ts, sys, host, createLanguageService);
}
else if (semver.gte(ts.version, '4.7.0')) {
Expand Down

0 comments on commit bd79362

Please sign in to comment.