Path: blob/main/build/lib/mangle/staticLanguageServiceHost.ts
5237 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import ts from 'typescript';6import path from 'path';78export class StaticLanguageServiceHost implements ts.LanguageServiceHost {910private readonly _cmdLine: ts.ParsedCommandLine;11private readonly _scriptSnapshots: Map<string, ts.IScriptSnapshot> = new Map();12readonly projectPath: string;1314constructor(projectPath: string) {15this.projectPath = projectPath;16const existingOptions: Partial<ts.CompilerOptions> = {};17const parsed = ts.readConfigFile(projectPath, ts.sys.readFile);18if (parsed.error) {19throw parsed.error;20}21this._cmdLine = ts.parseJsonConfigFileContent(parsed.config, ts.sys, path.dirname(projectPath), existingOptions);22if (this._cmdLine.errors.length > 0) {23throw parsed.error;24}25}26getCompilationSettings(): ts.CompilerOptions {27return this._cmdLine.options;28}29getScriptFileNames(): string[] {30return this._cmdLine.fileNames;31}32getScriptVersion(_fileName: string): string {33return '1';34}35getProjectVersion(): string {36return '1';37}38getScriptSnapshot(fileName: string): ts.IScriptSnapshot | undefined {39let result: ts.IScriptSnapshot | undefined = this._scriptSnapshots.get(fileName);40if (result === undefined) {41const content = ts.sys.readFile(fileName);42if (content === undefined) {43return undefined;44}45result = ts.ScriptSnapshot.fromString(content);46this._scriptSnapshots.set(fileName, result);47}48return result;49}50getCurrentDirectory(): string {51return path.dirname(this.projectPath);52}53getDefaultLibFileName(options: ts.CompilerOptions): string {54return ts.getDefaultLibFilePath(options);55}56directoryExists = ts.sys.directoryExists;57getDirectories = ts.sys.getDirectories;58fileExists = ts.sys.fileExists;59readFile = ts.sys.readFile;60readDirectory = ts.sys.readDirectory;61// this is necessary to make source references work.62realpath = ts.sys.realpath;63}646566