style(compiler-cli): reformat of codebase with new clang-format version (#36520)
This commit reformats the packages/compiler-cli tree using the new version of clang-format. PR Close #36520
This commit is contained in:
@ -25,7 +25,7 @@ export class CachedFileSystem implements FileSystem {
|
||||
if (!this.existsCache.has(path)) {
|
||||
this.existsCache.set(path, this.delegate.exists(path));
|
||||
}
|
||||
return this.existsCache.get(path) !;
|
||||
return this.existsCache.get(path)!;
|
||||
}
|
||||
|
||||
invalidateCaches(path: AbsoluteFsPath) {
|
||||
@ -131,15 +131,33 @@ export class CachedFileSystem implements FileSystem {
|
||||
}
|
||||
|
||||
// The following methods simply call through to the delegate.
|
||||
readdir(path: AbsoluteFsPath): PathSegment[] { return this.delegate.readdir(path); }
|
||||
pwd(): AbsoluteFsPath { return this.delegate.pwd(); }
|
||||
chdir(path: AbsoluteFsPath): void { this.delegate.chdir(path); }
|
||||
extname(path: AbsoluteFsPath|PathSegment): string { return this.delegate.extname(path); }
|
||||
isCaseSensitive(): boolean { return this.delegate.isCaseSensitive(); }
|
||||
isRoot(path: AbsoluteFsPath): boolean { return this.delegate.isRoot(path); }
|
||||
isRooted(path: string): boolean { return this.delegate.isRooted(path); }
|
||||
resolve(...paths: string[]): AbsoluteFsPath { return this.delegate.resolve(...paths); }
|
||||
dirname<T extends PathString>(file: T): T { return this.delegate.dirname(file); }
|
||||
readdir(path: AbsoluteFsPath): PathSegment[] {
|
||||
return this.delegate.readdir(path);
|
||||
}
|
||||
pwd(): AbsoluteFsPath {
|
||||
return this.delegate.pwd();
|
||||
}
|
||||
chdir(path: AbsoluteFsPath): void {
|
||||
this.delegate.chdir(path);
|
||||
}
|
||||
extname(path: AbsoluteFsPath|PathSegment): string {
|
||||
return this.delegate.extname(path);
|
||||
}
|
||||
isCaseSensitive(): boolean {
|
||||
return this.delegate.isCaseSensitive();
|
||||
}
|
||||
isRoot(path: AbsoluteFsPath): boolean {
|
||||
return this.delegate.isRoot(path);
|
||||
}
|
||||
isRooted(path: string): boolean {
|
||||
return this.delegate.isRooted(path);
|
||||
}
|
||||
resolve(...paths: string[]): AbsoluteFsPath {
|
||||
return this.delegate.resolve(...paths);
|
||||
}
|
||||
dirname<T extends PathString>(file: T): T {
|
||||
return this.delegate.dirname(file);
|
||||
}
|
||||
join<T extends PathString>(basePath: T, ...paths: string[]): T {
|
||||
return this.delegate.join(basePath, ...paths);
|
||||
}
|
||||
@ -149,7 +167,13 @@ export class CachedFileSystem implements FileSystem {
|
||||
basename(filePath: string, extension?: string|undefined): PathSegment {
|
||||
return this.delegate.basename(filePath, extension);
|
||||
}
|
||||
realpath(filePath: AbsoluteFsPath): AbsoluteFsPath { return this.delegate.realpath(filePath); }
|
||||
getDefaultLibLocation(): AbsoluteFsPath { return this.delegate.getDefaultLibLocation(); }
|
||||
normalize<T extends PathString>(path: T): T { return this.delegate.normalize(path); }
|
||||
realpath(filePath: AbsoluteFsPath): AbsoluteFsPath {
|
||||
return this.delegate.realpath(filePath);
|
||||
}
|
||||
getDefaultLibLocation(): AbsoluteFsPath {
|
||||
return this.delegate.getDefaultLibLocation();
|
||||
}
|
||||
normalize<T extends PathString>(path: T): T {
|
||||
return this.delegate.normalize(path);
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,9 @@ export class NgtscCompilerHost implements ts.CompilerHost {
|
||||
return this.fs.join(this.getDefaultLibLocation(), ts.getDefaultLibFileName(options));
|
||||
}
|
||||
|
||||
getDefaultLibLocation(): string { return this.fs.getDefaultLibLocation(); }
|
||||
getDefaultLibLocation(): string {
|
||||
return this.fs.getDefaultLibLocation();
|
||||
}
|
||||
|
||||
writeFile(
|
||||
fileName: string, data: string, writeByteOrderMark: boolean,
|
||||
@ -37,13 +39,17 @@ export class NgtscCompilerHost implements ts.CompilerHost {
|
||||
this.fs.writeFile(path, data);
|
||||
}
|
||||
|
||||
getCurrentDirectory(): string { return this.fs.pwd(); }
|
||||
getCurrentDirectory(): string {
|
||||
return this.fs.pwd();
|
||||
}
|
||||
|
||||
getCanonicalFileName(fileName: string): string {
|
||||
return this.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase();
|
||||
}
|
||||
|
||||
useCaseSensitiveFileNames(): boolean { return this.fs.isCaseSensitive(); }
|
||||
useCaseSensitiveFileNames(): boolean {
|
||||
return this.fs.isCaseSensitive();
|
||||
}
|
||||
|
||||
getNewLine(): string {
|
||||
switch (this.options.newLine) {
|
||||
|
@ -16,32 +16,84 @@ import {AbsoluteFsPath, FileStats, FileSystem, PathSegment, PathString} from './
|
||||
* the `FileSystem` under the hood.
|
||||
*/
|
||||
export class InvalidFileSystem implements FileSystem {
|
||||
exists(path: AbsoluteFsPath): boolean { throw makeError(); }
|
||||
readFile(path: AbsoluteFsPath): string { throw makeError(); }
|
||||
writeFile(path: AbsoluteFsPath, data: string, exclusive?: boolean): void { throw makeError(); }
|
||||
removeFile(path: AbsoluteFsPath): void { throw makeError(); }
|
||||
symlink(target: AbsoluteFsPath, path: AbsoluteFsPath): void { throw makeError(); }
|
||||
readdir(path: AbsoluteFsPath): PathSegment[] { throw makeError(); }
|
||||
lstat(path: AbsoluteFsPath): FileStats { throw makeError(); }
|
||||
stat(path: AbsoluteFsPath): FileStats { throw makeError(); }
|
||||
pwd(): AbsoluteFsPath { throw makeError(); }
|
||||
chdir(path: AbsoluteFsPath): void { throw makeError(); }
|
||||
extname(path: AbsoluteFsPath|PathSegment): string { throw makeError(); }
|
||||
copyFile(from: AbsoluteFsPath, to: AbsoluteFsPath): void { throw makeError(); }
|
||||
moveFile(from: AbsoluteFsPath, to: AbsoluteFsPath): void { throw makeError(); }
|
||||
ensureDir(path: AbsoluteFsPath): void { throw makeError(); }
|
||||
removeDeep(path: AbsoluteFsPath): void { throw makeError(); }
|
||||
isCaseSensitive(): boolean { throw makeError(); }
|
||||
resolve(...paths: string[]): AbsoluteFsPath { throw makeError(); }
|
||||
dirname<T extends PathString>(file: T): T { throw makeError(); }
|
||||
join<T extends PathString>(basePath: T, ...paths: string[]): T { throw makeError(); }
|
||||
isRoot(path: AbsoluteFsPath): boolean { throw makeError(); }
|
||||
isRooted(path: string): boolean { throw makeError(); }
|
||||
relative<T extends PathString>(from: T, to: T): PathSegment { throw makeError(); }
|
||||
basename(filePath: string, extension?: string): PathSegment { throw makeError(); }
|
||||
realpath(filePath: AbsoluteFsPath): AbsoluteFsPath { throw makeError(); }
|
||||
getDefaultLibLocation(): AbsoluteFsPath { throw makeError(); }
|
||||
normalize<T extends PathString>(path: T): T { throw makeError(); }
|
||||
exists(path: AbsoluteFsPath): boolean {
|
||||
throw makeError();
|
||||
}
|
||||
readFile(path: AbsoluteFsPath): string {
|
||||
throw makeError();
|
||||
}
|
||||
writeFile(path: AbsoluteFsPath, data: string, exclusive?: boolean): void {
|
||||
throw makeError();
|
||||
}
|
||||
removeFile(path: AbsoluteFsPath): void {
|
||||
throw makeError();
|
||||
}
|
||||
symlink(target: AbsoluteFsPath, path: AbsoluteFsPath): void {
|
||||
throw makeError();
|
||||
}
|
||||
readdir(path: AbsoluteFsPath): PathSegment[] {
|
||||
throw makeError();
|
||||
}
|
||||
lstat(path: AbsoluteFsPath): FileStats {
|
||||
throw makeError();
|
||||
}
|
||||
stat(path: AbsoluteFsPath): FileStats {
|
||||
throw makeError();
|
||||
}
|
||||
pwd(): AbsoluteFsPath {
|
||||
throw makeError();
|
||||
}
|
||||
chdir(path: AbsoluteFsPath): void {
|
||||
throw makeError();
|
||||
}
|
||||
extname(path: AbsoluteFsPath|PathSegment): string {
|
||||
throw makeError();
|
||||
}
|
||||
copyFile(from: AbsoluteFsPath, to: AbsoluteFsPath): void {
|
||||
throw makeError();
|
||||
}
|
||||
moveFile(from: AbsoluteFsPath, to: AbsoluteFsPath): void {
|
||||
throw makeError();
|
||||
}
|
||||
ensureDir(path: AbsoluteFsPath): void {
|
||||
throw makeError();
|
||||
}
|
||||
removeDeep(path: AbsoluteFsPath): void {
|
||||
throw makeError();
|
||||
}
|
||||
isCaseSensitive(): boolean {
|
||||
throw makeError();
|
||||
}
|
||||
resolve(...paths: string[]): AbsoluteFsPath {
|
||||
throw makeError();
|
||||
}
|
||||
dirname<T extends PathString>(file: T): T {
|
||||
throw makeError();
|
||||
}
|
||||
join<T extends PathString>(basePath: T, ...paths: string[]): T {
|
||||
throw makeError();
|
||||
}
|
||||
isRoot(path: AbsoluteFsPath): boolean {
|
||||
throw makeError();
|
||||
}
|
||||
isRooted(path: string): boolean {
|
||||
throw makeError();
|
||||
}
|
||||
relative<T extends PathString>(from: T, to: T): PathSegment {
|
||||
throw makeError();
|
||||
}
|
||||
basename(filePath: string, extension?: string): PathSegment {
|
||||
throw makeError();
|
||||
}
|
||||
realpath(filePath: AbsoluteFsPath): AbsoluteFsPath {
|
||||
throw makeError();
|
||||
}
|
||||
getDefaultLibLocation(): AbsoluteFsPath {
|
||||
throw makeError();
|
||||
}
|
||||
normalize<T extends PathString>(path: T): T {
|
||||
throw makeError();
|
||||
}
|
||||
}
|
||||
|
||||
function makeError() {
|
||||
|
@ -91,7 +91,7 @@ export class LogicalFileSystem {
|
||||
}
|
||||
this.cache.set(physicalFile, logicalFile);
|
||||
}
|
||||
return this.cache.get(physicalFile) !;
|
||||
return this.cache.get(physicalFile)!;
|
||||
}
|
||||
|
||||
private createLogicalProjectPath(file: AbsoluteFsPath, rootDir: AbsoluteFsPath):
|
||||
|
@ -17,20 +17,42 @@ import {AbsoluteFsPath, FileStats, FileSystem, PathSegment, PathString} from './
|
||||
*/
|
||||
export class NodeJSFileSystem implements FileSystem {
|
||||
private _caseSensitive: boolean|undefined = undefined;
|
||||
exists(path: AbsoluteFsPath): boolean { return fs.existsSync(path); }
|
||||
readFile(path: AbsoluteFsPath): string { return fs.readFileSync(path, 'utf8'); }
|
||||
exists(path: AbsoluteFsPath): boolean {
|
||||
return fs.existsSync(path);
|
||||
}
|
||||
readFile(path: AbsoluteFsPath): string {
|
||||
return fs.readFileSync(path, 'utf8');
|
||||
}
|
||||
writeFile(path: AbsoluteFsPath, data: string, exclusive: boolean = false): void {
|
||||
fs.writeFileSync(path, data, exclusive ? {flag: 'wx'} : undefined);
|
||||
}
|
||||
removeFile(path: AbsoluteFsPath): void { fs.unlinkSync(path); }
|
||||
symlink(target: AbsoluteFsPath, path: AbsoluteFsPath): void { fs.symlinkSync(target, path); }
|
||||
readdir(path: AbsoluteFsPath): PathSegment[] { return fs.readdirSync(path) as PathSegment[]; }
|
||||
lstat(path: AbsoluteFsPath): FileStats { return fs.lstatSync(path); }
|
||||
stat(path: AbsoluteFsPath): FileStats { return fs.statSync(path); }
|
||||
pwd(): AbsoluteFsPath { return this.normalize(process.cwd()) as AbsoluteFsPath; }
|
||||
chdir(dir: AbsoluteFsPath): void { process.chdir(dir); }
|
||||
copyFile(from: AbsoluteFsPath, to: AbsoluteFsPath): void { fs.copyFileSync(from, to); }
|
||||
moveFile(from: AbsoluteFsPath, to: AbsoluteFsPath): void { fs.renameSync(from, to); }
|
||||
removeFile(path: AbsoluteFsPath): void {
|
||||
fs.unlinkSync(path);
|
||||
}
|
||||
symlink(target: AbsoluteFsPath, path: AbsoluteFsPath): void {
|
||||
fs.symlinkSync(target, path);
|
||||
}
|
||||
readdir(path: AbsoluteFsPath): PathSegment[] {
|
||||
return fs.readdirSync(path) as PathSegment[];
|
||||
}
|
||||
lstat(path: AbsoluteFsPath): FileStats {
|
||||
return fs.lstatSync(path);
|
||||
}
|
||||
stat(path: AbsoluteFsPath): FileStats {
|
||||
return fs.statSync(path);
|
||||
}
|
||||
pwd(): AbsoluteFsPath {
|
||||
return this.normalize(process.cwd()) as AbsoluteFsPath;
|
||||
}
|
||||
chdir(dir: AbsoluteFsPath): void {
|
||||
process.chdir(dir);
|
||||
}
|
||||
copyFile(from: AbsoluteFsPath, to: AbsoluteFsPath): void {
|
||||
fs.copyFileSync(from, to);
|
||||
}
|
||||
moveFile(from: AbsoluteFsPath, to: AbsoluteFsPath): void {
|
||||
fs.renameSync(from, to);
|
||||
}
|
||||
ensureDir(path: AbsoluteFsPath): void {
|
||||
const parents: AbsoluteFsPath[] = [];
|
||||
while (!this.isRoot(path) && !this.exists(path)) {
|
||||
@ -38,10 +60,12 @@ export class NodeJSFileSystem implements FileSystem {
|
||||
path = this.dirname(path);
|
||||
}
|
||||
while (parents.length) {
|
||||
this.safeMkdir(parents.pop() !);
|
||||
this.safeMkdir(parents.pop()!);
|
||||
}
|
||||
}
|
||||
removeDeep(path: AbsoluteFsPath): void { fsExtra.removeSync(path); }
|
||||
removeDeep(path: AbsoluteFsPath): void {
|
||||
fsExtra.removeSync(path);
|
||||
}
|
||||
isCaseSensitive(): boolean {
|
||||
if (this._caseSensitive === undefined) {
|
||||
this._caseSensitive = this.exists(togglePathCase(__filename));
|
||||
@ -52,20 +76,30 @@ export class NodeJSFileSystem implements FileSystem {
|
||||
return this.normalize(p.resolve(...paths)) as AbsoluteFsPath;
|
||||
}
|
||||
|
||||
dirname<T extends string>(file: T): T { return this.normalize(p.dirname(file)) as T; }
|
||||
dirname<T extends string>(file: T): T {
|
||||
return this.normalize(p.dirname(file)) as T;
|
||||
}
|
||||
join<T extends string>(basePath: T, ...paths: string[]): T {
|
||||
return this.normalize(p.join(basePath, ...paths)) as T;
|
||||
}
|
||||
isRoot(path: AbsoluteFsPath): boolean { return this.dirname(path) === this.normalize(path); }
|
||||
isRooted(path: string): boolean { return p.isAbsolute(path); }
|
||||
isRoot(path: AbsoluteFsPath): boolean {
|
||||
return this.dirname(path) === this.normalize(path);
|
||||
}
|
||||
isRooted(path: string): boolean {
|
||||
return p.isAbsolute(path);
|
||||
}
|
||||
relative<T extends PathString>(from: T, to: T): PathSegment {
|
||||
return relativeFrom(this.normalize(p.relative(from, to)));
|
||||
}
|
||||
basename(filePath: string, extension?: string): PathSegment {
|
||||
return p.basename(filePath, extension) as PathSegment;
|
||||
}
|
||||
extname(path: AbsoluteFsPath|PathSegment): string { return p.extname(path); }
|
||||
realpath(path: AbsoluteFsPath): AbsoluteFsPath { return this.resolve(fs.realpathSync(path)); }
|
||||
extname(path: AbsoluteFsPath|PathSegment): string {
|
||||
return p.extname(path);
|
||||
}
|
||||
realpath(path: AbsoluteFsPath): AbsoluteFsPath {
|
||||
return this.resolve(fs.realpathSync(path));
|
||||
}
|
||||
getDefaultLibLocation(): AbsoluteFsPath {
|
||||
return this.resolve(require.resolve('typescript'), '..');
|
||||
}
|
||||
|
@ -12,7 +12,7 @@
|
||||
* A `string` is not assignable to a `BrandedPath`, but a `BrandedPath` is assignable to a `string`.
|
||||
* Two `BrandedPath`s with different brands are not mutually assignable.
|
||||
*/
|
||||
export type BrandedPath<B extends string> = string & {
|
||||
export type BrandedPath<B extends string> = string&{
|
||||
_brand: B;
|
||||
};
|
||||
|
||||
@ -63,7 +63,7 @@ export interface FileSystem {
|
||||
normalize<T extends PathString>(path: T): T;
|
||||
}
|
||||
|
||||
export type PathString = string | AbsoluteFsPath | PathSegment;
|
||||
export type PathString = string|AbsoluteFsPath|PathSegment;
|
||||
|
||||
/**
|
||||
* Information about an object in the FileSystem.
|
||||
|
@ -28,8 +28,8 @@ export function stripExtension(path: string): string {
|
||||
export function getSourceFileOrError(program: ts.Program, fileName: AbsoluteFsPath): ts.SourceFile {
|
||||
const sf = program.getSourceFile(fileName);
|
||||
if (sf === undefined) {
|
||||
throw new Error(
|
||||
`Program does not contain "${fileName}" - available files are ${program.getSourceFiles().map(sf => sf.fileName).join(', ')}`);
|
||||
throw new Error(`Program does not contain "${fileName}" - available files are ${
|
||||
program.getSourceFiles().map(sf => sf.fileName).join(', ')}`);
|
||||
}
|
||||
return sf;
|
||||
}
|
||||
|
@ -11,37 +11,49 @@ import {absoluteFrom, relativeFrom, setFileSystem} from '../src/helpers';
|
||||
import {NodeJSFileSystem} from '../src/node_js_file_system';
|
||||
|
||||
describe('path types', () => {
|
||||
beforeEach(() => { setFileSystem(new NodeJSFileSystem()); });
|
||||
beforeEach(() => {
|
||||
setFileSystem(new NodeJSFileSystem());
|
||||
});
|
||||
|
||||
describe('absoluteFrom', () => {
|
||||
it('should not throw when creating one from an absolute path',
|
||||
() => { expect(() => absoluteFrom('/test.txt')).not.toThrow(); });
|
||||
it('should not throw when creating one from an absolute path', () => {
|
||||
expect(() => absoluteFrom('/test.txt')).not.toThrow();
|
||||
});
|
||||
|
||||
if (os.platform() === 'win32') {
|
||||
it('should not throw when creating one from a windows absolute path',
|
||||
() => { expect(absoluteFrom('C:\\test.txt')).toEqual('C:/test.txt'); });
|
||||
it('should not throw when creating one from a windows absolute path', () => {
|
||||
expect(absoluteFrom('C:\\test.txt')).toEqual('C:/test.txt');
|
||||
});
|
||||
it('should not throw when creating one from a windows absolute path with POSIX separators',
|
||||
() => { expect(absoluteFrom('C:/test.txt')).toEqual('C:/test.txt'); });
|
||||
it('should support windows drive letters',
|
||||
() => { expect(absoluteFrom('D:\\foo\\test.txt')).toEqual('D:/foo/test.txt'); });
|
||||
it('should convert Windows path separators to POSIX separators',
|
||||
() => { expect(absoluteFrom('C:\\foo\\test.txt')).toEqual('C:/foo/test.txt'); });
|
||||
() => {
|
||||
expect(absoluteFrom('C:/test.txt')).toEqual('C:/test.txt');
|
||||
});
|
||||
it('should support windows drive letters', () => {
|
||||
expect(absoluteFrom('D:\\foo\\test.txt')).toEqual('D:/foo/test.txt');
|
||||
});
|
||||
it('should convert Windows path separators to POSIX separators', () => {
|
||||
expect(absoluteFrom('C:\\foo\\test.txt')).toEqual('C:/foo/test.txt');
|
||||
});
|
||||
}
|
||||
|
||||
it('should throw when creating one from a non-absolute path',
|
||||
() => { expect(() => absoluteFrom('test.txt')).toThrow(); });
|
||||
it('should throw when creating one from a non-absolute path', () => {
|
||||
expect(() => absoluteFrom('test.txt')).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('relativeFrom', () => {
|
||||
it('should not throw when creating one from a relative path',
|
||||
() => { expect(() => relativeFrom('a/b/c.txt')).not.toThrow(); });
|
||||
it('should not throw when creating one from a relative path', () => {
|
||||
expect(() => relativeFrom('a/b/c.txt')).not.toThrow();
|
||||
});
|
||||
|
||||
it('should throw when creating one from an absolute path',
|
||||
() => { expect(() => relativeFrom('/a/b/c.txt')).toThrow(); });
|
||||
it('should throw when creating one from an absolute path', () => {
|
||||
expect(() => relativeFrom('/a/b/c.txt')).toThrow();
|
||||
});
|
||||
|
||||
if (os.platform() === 'win32') {
|
||||
it('should throw when creating one from a Windows absolute path',
|
||||
() => { expect(() => relativeFrom('C:/a/b/c.txt')).toThrow(); });
|
||||
it('should throw when creating one from a Windows absolute path', () => {
|
||||
expect(() => relativeFrom('C:/a/b/c.txt')).toThrow();
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -10,4 +10,4 @@ export {Folder, MockFileSystem} from './src/mock_file_system';
|
||||
export {MockFileSystemNative} from './src/mock_file_system_native';
|
||||
export {MockFileSystemPosix} from './src/mock_file_system_posix';
|
||||
export {MockFileSystemWindows} from './src/mock_file_system_windows';
|
||||
export {TestFile, initMockFileSystem, runInEachFileSystem} from './src/test_helper';
|
||||
export {initMockFileSystem, runInEachFileSystem, TestFile} from './src/test_helper';
|
||||
|
@ -21,9 +21,13 @@ export abstract class MockFileSystem implements FileSystem {
|
||||
this._cwd = this.normalize(cwd);
|
||||
}
|
||||
|
||||
isCaseSensitive() { return this._isCaseSensitive; }
|
||||
isCaseSensitive() {
|
||||
return this._isCaseSensitive;
|
||||
}
|
||||
|
||||
exists(path: AbsoluteFsPath): boolean { return this.findFromPath(path).entity !== null; }
|
||||
exists(path: AbsoluteFsPath): boolean {
|
||||
return this.findFromPath(path).entity !== null;
|
||||
}
|
||||
|
||||
readFile(path: AbsoluteFsPath): string {
|
||||
const {entity} = this.findFromPath(path);
|
||||
@ -142,7 +146,9 @@ export abstract class MockFileSystem implements FileSystem {
|
||||
delete entity[basename];
|
||||
}
|
||||
|
||||
isRoot(path: AbsoluteFsPath): boolean { return this.dirname(path) === path; }
|
||||
isRoot(path: AbsoluteFsPath): boolean {
|
||||
return this.dirname(path) === path;
|
||||
}
|
||||
|
||||
extname(path: AbsoluteFsPath|PathSegment): string {
|
||||
const match = /.+(\.[^.]*)$/.exec(path);
|
||||
@ -159,9 +165,13 @@ export abstract class MockFileSystem implements FileSystem {
|
||||
}
|
||||
}
|
||||
|
||||
pwd(): AbsoluteFsPath { return this._cwd; }
|
||||
pwd(): AbsoluteFsPath {
|
||||
return this._cwd;
|
||||
}
|
||||
|
||||
chdir(path: AbsoluteFsPath): void { this._cwd = this.normalize(path); }
|
||||
chdir(path: AbsoluteFsPath): void {
|
||||
this._cwd = this.normalize(path);
|
||||
}
|
||||
|
||||
getDefaultLibLocation(): AbsoluteFsPath {
|
||||
// Mimic the node module resolution algorithm and start in the current directory, then look
|
||||
@ -201,8 +211,12 @@ export abstract class MockFileSystem implements FileSystem {
|
||||
abstract normalize<T extends PathString>(path: T): T;
|
||||
protected abstract splitPath<T extends PathString>(path: T): string[];
|
||||
|
||||
dump(): Folder { return cloneFolder(this._fileTree); }
|
||||
init(folder: Folder): void { this._fileTree = cloneFolder(folder); }
|
||||
dump(): Folder {
|
||||
return cloneFolder(this._fileTree);
|
||||
}
|
||||
init(folder: Folder): void {
|
||||
this._fileTree = cloneFolder(folder);
|
||||
}
|
||||
|
||||
protected findFromPath(path: AbsoluteFsPath, options?: {followSymLinks: boolean}): FindResult {
|
||||
const followSymLinks = !!options && options.followSymLinks;
|
||||
@ -215,7 +229,7 @@ export abstract class MockFileSystem implements FileSystem {
|
||||
segments[0] = '';
|
||||
let current: Entity|null = this._fileTree;
|
||||
while (segments.length) {
|
||||
current = current[segments.shift() !];
|
||||
current = current[segments.shift()!];
|
||||
if (current === undefined) {
|
||||
return {path, entity: null};
|
||||
}
|
||||
@ -239,7 +253,7 @@ export abstract class MockFileSystem implements FileSystem {
|
||||
|
||||
protected splitIntoFolderAndFile(path: AbsoluteFsPath): [AbsoluteFsPath, string] {
|
||||
const segments = this.splitPath(path);
|
||||
const file = segments.pop() !;
|
||||
const file = segments.pop()!;
|
||||
return [path.substring(0, path.length - file.length - 1) as AbsoluteFsPath, file];
|
||||
}
|
||||
}
|
||||
@ -247,8 +261,10 @@ export interface FindResult {
|
||||
path: AbsoluteFsPath;
|
||||
entity: Entity|null;
|
||||
}
|
||||
export type Entity = Folder | File | SymLink;
|
||||
export interface Folder { [pathSegments: string]: Entity; }
|
||||
export type Entity = Folder|File|SymLink;
|
||||
export interface Folder {
|
||||
[pathSegments: string]: Entity;
|
||||
}
|
||||
export type File = string;
|
||||
export class SymLink {
|
||||
constructor(public path: AbsoluteFsPath) {}
|
||||
@ -256,24 +272,32 @@ export class SymLink {
|
||||
|
||||
class MockFileStats implements FileStats {
|
||||
constructor(private entity: Entity) {}
|
||||
isFile(): boolean { return isFile(this.entity); }
|
||||
isDirectory(): boolean { return isFolder(this.entity); }
|
||||
isSymbolicLink(): boolean { return isSymLink(this.entity); }
|
||||
isFile(): boolean {
|
||||
return isFile(this.entity);
|
||||
}
|
||||
isDirectory(): boolean {
|
||||
return isFolder(this.entity);
|
||||
}
|
||||
isSymbolicLink(): boolean {
|
||||
return isSymLink(this.entity);
|
||||
}
|
||||
}
|
||||
|
||||
class MockFileSystemError extends Error {
|
||||
constructor(public code: string, public path: string, message: string) { super(message); }
|
||||
constructor(public code: string, public path: string, message: string) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
export function isFile(item: Entity | null): item is File {
|
||||
export function isFile(item: Entity|null): item is File {
|
||||
return typeof item === 'string';
|
||||
}
|
||||
|
||||
export function isSymLink(item: Entity | null): item is SymLink {
|
||||
export function isSymLink(item: Entity|null): item is SymLink {
|
||||
return item instanceof SymLink;
|
||||
}
|
||||
|
||||
export function isFolder(item: Entity | null): item is Folder {
|
||||
export function isFolder(item: Entity|null): item is Folder {
|
||||
return item !== null && !isFile(item) && !isSymLink(item);
|
||||
}
|
||||
|
||||
|
@ -15,7 +15,9 @@ import {MockFileSystem} from './mock_file_system';
|
||||
const isWindows = os.platform() === 'win32';
|
||||
|
||||
export class MockFileSystemNative extends MockFileSystem {
|
||||
constructor(cwd: AbsoluteFsPath = '/' as AbsoluteFsPath) { super(undefined, cwd); }
|
||||
constructor(cwd: AbsoluteFsPath = '/' as AbsoluteFsPath) {
|
||||
super(undefined, cwd);
|
||||
}
|
||||
|
||||
// Delegate to the real NodeJSFileSystem for these path related methods
|
||||
|
||||
@ -36,9 +38,13 @@ export class MockFileSystemNative extends MockFileSystem {
|
||||
return NodeJSFileSystem.prototype.basename.call(this, filePath, extension);
|
||||
}
|
||||
|
||||
isCaseSensitive() { return NodeJSFileSystem.prototype.isCaseSensitive.call(this); }
|
||||
isCaseSensitive() {
|
||||
return NodeJSFileSystem.prototype.isCaseSensitive.call(this);
|
||||
}
|
||||
|
||||
isRooted(path: string): boolean { return NodeJSFileSystem.prototype.isRooted.call(this, path); }
|
||||
isRooted(path: string): boolean {
|
||||
return NodeJSFileSystem.prototype.isRooted.call(this, path);
|
||||
}
|
||||
|
||||
isRoot(path: AbsoluteFsPath): boolean {
|
||||
return NodeJSFileSystem.prototype.isRoot.call(this, path);
|
||||
@ -57,5 +63,7 @@ export class MockFileSystemNative extends MockFileSystem {
|
||||
return NodeJSFileSystem.prototype.normalize.call(this, path) as T;
|
||||
}
|
||||
|
||||
protected splitPath<T>(path: string): string[] { return path.split(/[\\\/]/); }
|
||||
protected splitPath<T>(path: string): string[] {
|
||||
return path.split(/[\\\/]/);
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,9 @@ export class MockFileSystemPosix extends MockFileSystem {
|
||||
return this.normalize(resolved) as AbsoluteFsPath;
|
||||
}
|
||||
|
||||
dirname<T extends string>(file: T): T { return this.normalize(p.posix.dirname(file)) as T; }
|
||||
dirname<T extends string>(file: T): T {
|
||||
return this.normalize(p.posix.dirname(file)) as T;
|
||||
}
|
||||
|
||||
join<T extends string>(basePath: T, ...paths: string[]): T {
|
||||
return this.normalize(p.posix.join(basePath, ...paths)) as T;
|
||||
@ -31,9 +33,13 @@ export class MockFileSystemPosix extends MockFileSystem {
|
||||
return p.posix.basename(filePath, extension) as PathSegment;
|
||||
}
|
||||
|
||||
isRooted(path: string): boolean { return path.startsWith('/'); }
|
||||
isRooted(path: string): boolean {
|
||||
return path.startsWith('/');
|
||||
}
|
||||
|
||||
protected splitPath<T extends PathString>(path: T): string[] { return path.split('/'); }
|
||||
protected splitPath<T extends PathString>(path: T): string[] {
|
||||
return path.split('/');
|
||||
}
|
||||
|
||||
normalize<T extends PathString>(path: T): T {
|
||||
return path.replace(/^[a-z]:\//i, '/').replace(/\\/g, '/') as T;
|
||||
|
@ -17,7 +17,9 @@ export class MockFileSystemWindows extends MockFileSystem {
|
||||
return this.normalize(resolved as AbsoluteFsPath);
|
||||
}
|
||||
|
||||
dirname<T extends string>(path: T): T { return this.normalize(p.win32.dirname(path) as T); }
|
||||
dirname<T extends string>(path: T): T {
|
||||
return this.normalize(p.win32.dirname(path) as T);
|
||||
}
|
||||
|
||||
join<T extends string>(basePath: T, ...paths: string[]): T {
|
||||
return this.normalize(p.win32.join(basePath, ...paths)) as T;
|
||||
@ -31,9 +33,13 @@ export class MockFileSystemWindows extends MockFileSystem {
|
||||
return p.win32.basename(filePath, extension) as PathSegment;
|
||||
}
|
||||
|
||||
isRooted(path: string): boolean { return /^([A-Z]:)?([\\\/]|$)/i.test(path); }
|
||||
isRooted(path: string): boolean {
|
||||
return /^([A-Z]:)?([\\\/]|$)/i.test(path);
|
||||
}
|
||||
|
||||
protected splitPath<T extends PathString>(path: T): string[] { return path.split(/[\\\/]/); }
|
||||
protected splitPath<T extends PathString>(path: T): string[] {
|
||||
return path.split(/[\\\/]/);
|
||||
}
|
||||
|
||||
normalize<T extends PathString>(path: T): T {
|
||||
return path.replace(/^[\/\\]/i, 'C:/').replace(/\\/g, '/') as T;
|
||||
|
@ -47,7 +47,9 @@ function runInFileSystem(os: string, callback: (os: string) => void, error: bool
|
||||
afterEach(() => setFileSystem(new InvalidFileSystem()));
|
||||
callback(os);
|
||||
if (error) {
|
||||
afterAll(() => { throw new Error(`runInFileSystem limited to ${os}, cannot pass`); });
|
||||
afterAll(() => {
|
||||
throw new Error(`runInFileSystem limited to ${os}, cannot pass`);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -125,14 +127,16 @@ function monkeyPatchTypeScript(os: string, fs: MockFileSystem) {
|
||||
return {files, directories};
|
||||
}
|
||||
|
||||
function realPath(path: string): string { return fs.realpath(fs.resolve(path)); }
|
||||
function realPath(path: string): string {
|
||||
return fs.realpath(fs.resolve(path));
|
||||
}
|
||||
|
||||
// Rather than completely re-implementing we are using the `ts.matchFiles` function,
|
||||
// which is internal to the `ts` namespace.
|
||||
const tsMatchFiles: (
|
||||
path: string, extensions: ReadonlyArray<string>| undefined,
|
||||
excludes: ReadonlyArray<string>| undefined, includes: ReadonlyArray<string>| undefined,
|
||||
useCaseSensitiveFileNames: boolean, currentDirectory: string, depth: number | undefined,
|
||||
path: string, extensions: ReadonlyArray<string>|undefined,
|
||||
excludes: ReadonlyArray<string>|undefined, includes: ReadonlyArray<string>|undefined,
|
||||
useCaseSensitiveFileNames: boolean, currentDirectory: string, depth: number|undefined,
|
||||
getFileSystemEntries: (path: string) => FileSystemEntries,
|
||||
realpath: (path: string) => string) => string[] = (ts as any).matchFiles;
|
||||
|
||||
|
Reference in New Issue
Block a user