2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-06-09 17:51:53 -04:00
|
|
|
import * as ts from 'typescript';
|
|
|
|
|
2020-04-07 15:43:43 -04:00
|
|
|
export type Entry = string|Directory;
|
2016-06-09 17:51:53 -04:00
|
|
|
|
2020-04-07 15:43:43 -04:00
|
|
|
export interface Directory {
|
|
|
|
[name: string]: Entry;
|
|
|
|
}
|
2016-06-09 17:51:53 -04:00
|
|
|
|
2017-10-20 12:46:41 -04:00
|
|
|
export class MockAotContext {
|
2017-11-20 13:21:17 -05:00
|
|
|
private files: Entry[];
|
|
|
|
|
2020-04-07 15:43:43 -04:00
|
|
|
constructor(public currentDirectory: string, ...files: Entry[]) {
|
|
|
|
this.files = files;
|
|
|
|
}
|
2016-06-09 17:51:53 -04:00
|
|
|
|
2020-04-07 15:43:43 -04:00
|
|
|
fileExists(fileName: string): boolean {
|
|
|
|
return typeof this.getEntry(fileName) === 'string';
|
|
|
|
}
|
2016-06-09 17:51:53 -04:00
|
|
|
|
2018-07-06 20:02:08 -04:00
|
|
|
directoryExists(path: string): boolean {
|
|
|
|
return path === this.currentDirectory || typeof this.getEntry(path) === 'object';
|
|
|
|
}
|
2016-07-13 14:15:23 -04:00
|
|
|
|
2017-04-14 17:40:56 -04:00
|
|
|
readFile(fileName: string): string {
|
2016-11-12 08:08:58 -05:00
|
|
|
const data = this.getEntry(fileName);
|
2016-06-08 19:38:52 -04:00
|
|
|
if (typeof data === 'string') {
|
2016-06-09 17:51:53 -04:00
|
|
|
return data;
|
|
|
|
}
|
2020-04-07 15:43:43 -04:00
|
|
|
return undefined!;
|
2016-06-09 17:51:53 -04:00
|
|
|
}
|
|
|
|
|
2016-11-15 14:13:20 -05:00
|
|
|
readResource(fileName: string): Promise<string> {
|
|
|
|
const result = this.readFile(fileName);
|
|
|
|
if (result == null) {
|
|
|
|
return Promise.reject(new Error(`Resource not found: ${fileName}`));
|
|
|
|
}
|
|
|
|
return Promise.resolve(result);
|
|
|
|
}
|
|
|
|
|
2016-07-13 14:15:23 -04:00
|
|
|
writeFile(fileName: string, data: string): void {
|
2016-11-12 08:08:58 -05:00
|
|
|
const parts = fileName.split('/');
|
2020-04-07 15:43:43 -04:00
|
|
|
const name = parts.pop()!;
|
2016-11-12 08:08:58 -05:00
|
|
|
const entry = this.getEntry(parts);
|
2016-06-08 19:38:52 -04:00
|
|
|
if (entry && typeof entry !== 'string') {
|
2016-06-09 17:51:53 -04:00
|
|
|
entry[name] = data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-07 15:43:43 -04:00
|
|
|
assumeFileExists(fileName: string): void {
|
|
|
|
this.writeFile(fileName, '');
|
|
|
|
}
|
2016-07-13 14:15:23 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
getEntry(fileName: string|string[]): Entry|undefined {
|
|
|
|
let parts = typeof fileName === 'string' ? fileName.split('/') : fileName;
|
2016-06-09 17:51:53 -04:00
|
|
|
if (parts[0]) {
|
|
|
|
parts = this.currentDirectory.split('/').concat(parts);
|
|
|
|
}
|
|
|
|
parts.shift();
|
|
|
|
parts = normalize(parts);
|
2017-11-20 13:21:17 -05:00
|
|
|
return first(this.files, files => getEntryFromFiles(parts, files));
|
2016-06-09 17:51:53 -04:00
|
|
|
}
|
2016-08-30 21:07:40 -04:00
|
|
|
|
|
|
|
getDirectories(path: string): string[] {
|
|
|
|
const dir = this.getEntry(path);
|
|
|
|
if (typeof dir !== 'object') {
|
|
|
|
return [];
|
|
|
|
} else {
|
|
|
|
return Object.keys(dir).filter(key => typeof dir[key] === 'object');
|
|
|
|
}
|
|
|
|
}
|
2017-11-20 13:21:17 -05:00
|
|
|
|
2020-04-07 15:43:43 -04:00
|
|
|
override(files: Entry) {
|
|
|
|
return new MockAotContext(this.currentDirectory, files, ...this.files);
|
|
|
|
}
|
2017-11-20 13:21:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function first<T>(a: T[], cb: (value: T) => T | undefined): T|undefined {
|
|
|
|
for (const value of a) {
|
|
|
|
const result = cb(value);
|
|
|
|
if (result != null) return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getEntryFromFiles(parts: string[], files: Entry) {
|
|
|
|
let current = files;
|
|
|
|
while (parts.length) {
|
2020-04-07 15:43:43 -04:00
|
|
|
const part = parts.shift()!;
|
2017-11-20 13:21:17 -05:00
|
|
|
if (typeof current === 'string') {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const next = (<Directory>current)[part];
|
|
|
|
if (next === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
current = next;
|
|
|
|
}
|
|
|
|
return current;
|
2016-06-09 17:51:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function normalize(parts: string[]): string[] {
|
2016-11-12 08:08:58 -05:00
|
|
|
const result: string[] = [];
|
2016-06-09 17:51:53 -04:00
|
|
|
while (parts.length) {
|
2020-04-07 15:43:43 -04:00
|
|
|
const part = parts.shift()!;
|
2016-06-09 17:51:53 -04:00
|
|
|
switch (part) {
|
2016-06-08 19:38:52 -04:00
|
|
|
case '.':
|
|
|
|
break;
|
|
|
|
case '..':
|
|
|
|
result.pop();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
result.push(part);
|
2016-06-09 17:51:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class MockCompilerHost implements ts.CompilerHost {
|
2016-11-17 15:24:33 -05:00
|
|
|
constructor(private context: MockAotContext) {}
|
2016-06-09 17:51:53 -04:00
|
|
|
|
2020-04-07 15:43:43 -04:00
|
|
|
fileExists(fileName: string): boolean {
|
|
|
|
return this.context.fileExists(fileName);
|
|
|
|
}
|
2016-06-09 17:51:53 -04:00
|
|
|
|
2020-04-07 15:43:43 -04:00
|
|
|
readFile(fileName: string): string {
|
|
|
|
return this.context.readFile(fileName);
|
|
|
|
}
|
2016-06-09 17:51:53 -04:00
|
|
|
|
2016-07-13 14:15:23 -04:00
|
|
|
directoryExists(directoryName: string): boolean {
|
|
|
|
return this.context.directoryExists(directoryName);
|
|
|
|
}
|
2016-06-09 17:51:53 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
getSourceFile(
|
|
|
|
fileName: string, languageVersion: ts.ScriptTarget,
|
|
|
|
onError?: (message: string) => void): ts.SourceFile {
|
2016-11-12 08:08:58 -05:00
|
|
|
const sourceText = this.context.readFile(fileName);
|
2017-09-12 12:40:28 -04:00
|
|
|
if (sourceText != null) {
|
2016-06-09 17:51:53 -04:00
|
|
|
return ts.createSourceFile(fileName, sourceText, languageVersion);
|
|
|
|
} else {
|
2020-04-07 15:43:43 -04:00
|
|
|
return undefined!;
|
2016-06-09 17:51:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getDefaultLibFileName(options: ts.CompilerOptions): string {
|
|
|
|
return ts.getDefaultLibFileName(options);
|
|
|
|
}
|
|
|
|
|
2020-04-07 15:43:43 -04:00
|
|
|
writeFile: ts.WriteFileCallback =
|
|
|
|
(fileName, text) => {
|
|
|
|
this.context.writeFile(fileName, text);
|
|
|
|
}
|
2016-06-09 17:51:53 -04:00
|
|
|
|
2020-04-07 15:43:43 -04:00
|
|
|
getCurrentDirectory(): string {
|
|
|
|
return this.context.currentDirectory;
|
|
|
|
}
|
2016-06-09 17:51:53 -04:00
|
|
|
|
2020-04-07 15:43:43 -04:00
|
|
|
getCanonicalFileName(fileName: string): string {
|
|
|
|
return fileName;
|
|
|
|
}
|
2016-06-09 17:51:53 -04:00
|
|
|
|
2020-04-07 15:43:43 -04:00
|
|
|
useCaseSensitiveFileNames(): boolean {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-09 17:51:53 -04:00
|
|
|
|
2020-04-07 15:43:43 -04:00
|
|
|
getNewLine(): string {
|
|
|
|
return '\n';
|
|
|
|
}
|
2016-08-30 21:07:40 -04:00
|
|
|
|
2020-04-07 15:43:43 -04:00
|
|
|
getDirectories(path: string): string[] {
|
|
|
|
return this.context.getDirectories(path);
|
|
|
|
}
|
2016-06-09 17:51:53 -04:00
|
|
|
}
|