"Fossies" - the Fresh Open Source Software Archive

Member "angular-17.0.5/packages/language-service/src/adapters.ts" (29 Nov 2023, 6813 Bytes) of package /linux/www/angular-17.0.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /**
    2  * @license
    3  * Copyright Google LLC All Rights Reserved.
    4  *
    5  * Use of this source code is governed by an MIT-style license that can be
    6  * found in the LICENSE file at https://angular.io/license
    7  */
    8 
    9 /** @fileoverview provides adapters for communicating with the ng compiler */
   10 
   11 import {ConfigurationHost} from '@angular/compiler-cli';
   12 import {NgCompilerAdapter} from '@angular/compiler-cli/src/ngtsc/core/api';
   13 import {AbsoluteFsPath, FileStats, PathSegment, PathString} from '@angular/compiler-cli/src/ngtsc/file_system';
   14 import {isShim} from '@angular/compiler-cli/src/ngtsc/shims';
   15 import {getRootDirs} from '@angular/compiler-cli/src/ngtsc/util/src/typescript';
   16 import * as p from 'path';
   17 import ts from 'typescript/lib/tsserverlibrary';
   18 
   19 import {isTypeScriptFile} from './utils';
   20 
   21 const PRE_COMPILED_STYLE_EXTENSIONS = ['.scss', '.sass', '.less', '.styl'];
   22 
   23 export class LanguageServiceAdapter implements NgCompilerAdapter {
   24   readonly entryPoint = null;
   25   readonly constructionDiagnostics: ts.Diagnostic[] = [];
   26   readonly ignoreForEmit: Set<ts.SourceFile> = new Set();
   27   readonly unifiedModulesHost = null;  // only used in Bazel
   28   readonly rootDirs: AbsoluteFsPath[];
   29 
   30   /**
   31    * Map of resource filenames to the version of the file last read via `readResource`.
   32    *
   33    * Used to implement `getModifiedResourceFiles`.
   34    */
   35   private readonly lastReadResourceVersion = new Map<string, string>();
   36 
   37   constructor(private readonly project: ts.server.Project) {
   38     this.rootDirs = getRootDirs(this, project.getCompilationSettings());
   39   }
   40 
   41   resourceNameToFileName(
   42       url: string, fromFile: string,
   43       fallbackResolve?: (url: string, fromFile: string) => string | null): string|null {
   44     // If we are trying to resolve a `.css` file, see if we can find a pre-compiled file with the
   45     // same name instead. That way, we can provide go-to-definition for the pre-compiled files which
   46     // would generally be the desired behavior.
   47     if (url.endsWith('.css')) {
   48       const styleUrl = p.resolve(fromFile, '..', url);
   49       for (const ext of PRE_COMPILED_STYLE_EXTENSIONS) {
   50         const precompiledFileUrl = styleUrl.replace(/\.css$/, ext);
   51         if (this.fileExists(precompiledFileUrl)) {
   52           return precompiledFileUrl;
   53         }
   54       }
   55     }
   56     return fallbackResolve?.(url, fromFile) ?? null;
   57   }
   58 
   59   isShim(sf: ts.SourceFile): boolean {
   60     return isShim(sf);
   61   }
   62 
   63   isResource(sf: ts.SourceFile): boolean {
   64     const scriptInfo = this.project.getScriptInfo(sf.fileName);
   65     return scriptInfo?.scriptKind === ts.ScriptKind.Unknown;
   66   }
   67 
   68   fileExists(fileName: string): boolean {
   69     return this.project.fileExists(fileName);
   70   }
   71 
   72   readFile(fileName: string): string|undefined {
   73     return this.project.readFile(fileName);
   74   }
   75 
   76   getCurrentDirectory(): string {
   77     return this.project.getCurrentDirectory();
   78   }
   79 
   80   getCanonicalFileName(fileName: string): string {
   81     return this.project.projectService.toCanonicalFileName(fileName);
   82   }
   83 
   84   /**
   85    * Return the real path of a symlink. This method is required in order to
   86    * resolve symlinks in node_modules.
   87    */
   88   realpath(path: string): string {
   89     return this.project.realpath?.(path) ?? path;
   90   }
   91 
   92   /**
   93    * readResource() is an Angular-specific method for reading files that are not
   94    * managed by the TS compiler host, namely templates and stylesheets.
   95    * It is a method on ExtendedTsCompilerHost, see
   96    * packages/compiler-cli/src/ngtsc/core/api/src/interfaces.ts
   97    */
   98   readResource(fileName: string): string {
   99     if (isTypeScriptFile(fileName)) {
  100       throw new Error(`readResource() should not be called on TS file: ${fileName}`);
  101     }
  102     // Calling getScriptSnapshot() will actually create a ScriptInfo if it does
  103     // not exist! The same applies for getScriptVersion().
  104     // getScriptInfo() will not create one if it does not exist.
  105     // In this case, we *want* a script info to be created so that we could
  106     // keep track of its version.
  107     const version = this.project.getScriptVersion(fileName);
  108     this.lastReadResourceVersion.set(fileName, version);
  109     const scriptInfo = this.project.getScriptInfo(fileName);
  110     if (!scriptInfo) {
  111       // // This should not happen because it would have failed already at `getScriptVersion`.
  112       throw new Error(`Failed to get script info when trying to read ${fileName}`);
  113     }
  114     // Add external resources as root files to the project since we project language service
  115     // features for them (this is currently only the case for HTML files, but we could investigate
  116     // css file features in the future). This prevents the project from being closed when navigating
  117     // away from a resource file.
  118     if (!this.project.isRoot(scriptInfo)) {
  119       this.project.addRoot(scriptInfo);
  120     }
  121     const snapshot = scriptInfo.getSnapshot();
  122     return snapshot.getText(0, snapshot.getLength());
  123   }
  124 
  125   getModifiedResourceFiles(): Set<string>|undefined {
  126     const modifiedFiles = new Set<string>();
  127     for (const [fileName, oldVersion] of this.lastReadResourceVersion) {
  128       if (this.project.getScriptVersion(fileName) !== oldVersion) {
  129         modifiedFiles.add(fileName);
  130       }
  131     }
  132     return modifiedFiles.size > 0 ? modifiedFiles : undefined;
  133   }
  134 }
  135 
  136 /**
  137  * Used to read configuration files.
  138  *
  139  * A language service parse configuration host is independent of the adapter
  140  * because signatures of calls like `FileSystem#readFile` are a bit stricter
  141  * than those on the adapter.
  142  */
  143 export class LSParseConfigHost implements ConfigurationHost {
  144   constructor(private readonly serverHost: ts.server.ServerHost) {}
  145   exists(path: AbsoluteFsPath): boolean {
  146     return this.serverHost.fileExists(path) || this.serverHost.directoryExists(path);
  147   }
  148   readFile(path: AbsoluteFsPath): string {
  149     const content = this.serverHost.readFile(path);
  150     if (content === undefined) {
  151       throw new Error(`LanguageServiceFS#readFile called on unavailable file ${path}`);
  152     }
  153     return content;
  154   }
  155   lstat(path: AbsoluteFsPath): FileStats {
  156     return {
  157       isFile: () => {
  158         return this.serverHost.fileExists(path);
  159       },
  160       isDirectory: () => {
  161         return this.serverHost.directoryExists(path);
  162       },
  163       isSymbolicLink: () => {
  164         throw new Error(`LanguageServiceFS#lstat#isSymbolicLink not implemented`);
  165       },
  166     };
  167   }
  168   pwd(): AbsoluteFsPath {
  169     return this.serverHost.getCurrentDirectory() as AbsoluteFsPath;
  170   }
  171   extname(path: AbsoluteFsPath|PathSegment): string {
  172     return p.extname(path);
  173   }
  174   resolve(...paths: string[]): AbsoluteFsPath {
  175     return p.resolve(...paths) as AbsoluteFsPath;
  176   }
  177   dirname<T extends PathString>(file: T): T {
  178     return p.dirname(file) as T;
  179   }
  180   join<T extends PathString>(basePath: T, ...paths: string[]): T {
  181     return p.join(basePath, ...paths) as T;
  182   }
  183 }