• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import {
2    getNormalizedAbsolutePath, isRootedDiskPath, Map, normalizePath, Path, SortedArray, SortedReadonlyArray,
3    TypeAcquisition,
4} from "./_namespaces/ts";
5import { DiscoverTypings, Project } from "./_namespaces/ts.server";
6
7export enum LogLevel {
8    terse,
9    normal,
10    requestTime,
11    verbose
12}
13
14export const emptyArray: SortedReadonlyArray<never> = createSortedArray<never>();
15
16export interface Logger {
17    close(): void;
18    hasLevel(level: LogLevel): boolean;
19    loggingEnabled(): boolean;
20    perftrc(s: string): void;
21    info(s: string): void;
22    startGroup(): void;
23    endGroup(): void;
24    msg(s: string, type?: Msg): void;
25    getLogFileName(): string | undefined;
26}
27
28// TODO: Use a const enum (https://github.com/Microsoft/TypeScript/issues/16804)
29export enum Msg {
30    Err = "Err",
31    Info = "Info",
32    Perf = "Perf",
33}
34export namespace Msg {
35    /** @deprecated Only here for backwards-compatibility. Prefer just `Msg`. */
36    export type Types = Msg;
37}
38
39export function createInstallTypingsRequest(project: Project, typeAcquisition: TypeAcquisition, unresolvedImports: SortedReadonlyArray<string>, cachePath?: string): DiscoverTypings {
40    return {
41        projectName: project.getProjectName(),
42        fileNames: project.getFileNames(/*excludeFilesFromExternalLibraries*/ true, /*excludeConfigFiles*/ true).concat(project.getExcludedFiles() as NormalizedPath[]),
43        compilerOptions: project.getCompilationSettings(),
44        watchOptions: project.projectService.getWatchOptions(project),
45        typeAcquisition,
46        unresolvedImports,
47        projectRootPath: project.getCurrentDirectory() as Path,
48        cachePath,
49        kind: "discover"
50    };
51}
52
53export namespace Errors {
54    export function ThrowNoProject(): never {
55        throw new Error("No Project.");
56    }
57    export function ThrowProjectLanguageServiceDisabled(): never {
58        throw new Error("The project's language service is disabled.");
59    }
60    export function ThrowProjectDoesNotContainDocument(fileName: string, project: Project): never {
61        throw new Error(`Project '${project.getProjectName()}' does not contain document '${fileName}'`);
62    }
63}
64
65export type NormalizedPath = string & { __normalizedPathTag: any };
66
67export function toNormalizedPath(fileName: string): NormalizedPath {
68    return normalizePath(fileName) as NormalizedPath;
69}
70
71export function normalizedPathToPath(normalizedPath: NormalizedPath, currentDirectory: string, getCanonicalFileName: (f: string) => string): Path {
72    const f = isRootedDiskPath(normalizedPath) ? normalizedPath : getNormalizedAbsolutePath(normalizedPath, currentDirectory);
73    return getCanonicalFileName(f) as Path;
74}
75
76export function asNormalizedPath(fileName: string): NormalizedPath {
77    return fileName as NormalizedPath;
78}
79
80export interface NormalizedPathMap<T> {
81    get(path: NormalizedPath): T | undefined;
82    set(path: NormalizedPath, value: T): void;
83    contains(path: NormalizedPath): boolean;
84    remove(path: NormalizedPath): void;
85}
86
87export function createNormalizedPathMap<T>(): NormalizedPathMap<T> {
88    const map = new Map<string, T>();
89    return {
90        get(path) {
91            return map.get(path);
92        },
93        set(path, value) {
94            map.set(path, value);
95        },
96        contains(path) {
97            return map.has(path);
98        },
99        remove(path) {
100            map.delete(path);
101        }
102    };
103}
104
105/** @internal */
106export interface ProjectOptions {
107    configHasExtendsProperty: boolean;
108    /**
109     * true if config file explicitly listed files
110     */
111    configHasFilesProperty: boolean;
112    configHasIncludeProperty: boolean;
113    configHasExcludeProperty: boolean;
114}
115
116export function isInferredProjectName(name: string) {
117    // POSIX defines /dev/null as a device - there should be no file with this prefix
118    return /dev\/null\/inferredProject\d+\*/.test(name);
119}
120
121export function makeInferredProjectName(counter: number): string {
122    return `/dev/null/inferredProject${counter}*`;
123}
124
125/** @internal */
126export function makeAutoImportProviderProjectName(counter: number): string {
127    return `/dev/null/autoImportProviderProject${counter}*`;
128}
129
130/** @internal */
131export function makeAuxiliaryProjectName(counter: number): string {
132    return `/dev/null/auxiliaryProject${counter}*`;
133}
134
135export function createSortedArray<T>(): SortedArray<T> {
136    return [] as any as SortedArray<T>; // TODO: GH#19873
137}
138