Remove most <type> assertions in TS ext (#211648)

These can easily hide typing errors
This commit is contained in:
Matt Bierner 2024-04-29 09:23:51 -07:00 committed by GitHub
parent ba7b9278de
commit c1cc1a902d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 45 additions and 46 deletions

View file

@ -32,8 +32,8 @@ export class OpenJsDocLinkCommand implements Command {
public async execute(args: OpenJsDocLinkCommand_Args): Promise<void> {
const { line, character } = args.position;
const position = new vscode.Position(line, character);
await vscode.commands.executeCommand('vscode.open', vscode.Uri.from(args.file), <vscode.TextDocumentShowOptions>{
await vscode.commands.executeCommand('vscode.open', vscode.Uri.from(args.file), {
selection: new vscode.Range(position, position),
});
} satisfies vscode.TextDocumentShowOptions);
}
}

View file

@ -8,7 +8,7 @@ import { basename, dirname } from 'path';
export class MemFs implements vscode.FileSystemProvider {
private readonly root = new FsEntry(
private readonly root = new FsDirectoryEntry(
new Map(),
0,
0,
@ -31,8 +31,11 @@ export class MemFs implements vscode.FileSystemProvider {
if (!entry) {
throw vscode.FileSystemError.FileNotFound();
}
if (!(entry instanceof FsDirectoryEntry)) {
throw vscode.FileSystemError.FileNotADirectory();
}
return [...entry.contents.entries()].map(([name, entry]) => [name, entry.type]);
return Array.from(entry.contents.entries(), ([name, entry]) => [name, entry.type]);
}
readFile(uri: vscode.Uri): Uint8Array {
@ -43,6 +46,10 @@ export class MemFs implements vscode.FileSystemProvider {
throw vscode.FileSystemError.FileNotFound();
}
if (!(entry instanceof FsFileEntry)) {
throw vscode.FileSystemError.FileIsADirectory(uri);
}
return entry.data;
}
@ -58,12 +65,16 @@ export class MemFs implements vscode.FileSystemProvider {
const entry = dirContents.get(basename(uri.path));
if (!entry) {
if (create) {
dirContents.set(fileName, new FsEntry(content, time, time));
dirContents.set(fileName, new FsFileEntry(content, time, time));
this._emitter.fire([{ type: vscode.FileChangeType.Created, uri }]);
} else {
throw vscode.FileSystemError.FileNotFound();
}
} else {
if (entry instanceof FsDirectoryEntry) {
throw vscode.FileSystemError.FileIsADirectory(uri);
}
if (overwrite) {
entry.mtime = time;
entry.data = content;
@ -90,10 +101,10 @@ export class MemFs implements vscode.FileSystemProvider {
// console.log('createDirectory', uri.toString());
const dir = this.getParent(uri);
const now = Date.now() / 1000;
dir.contents.set(basename(uri.path), new FsEntry(new Map(), now, now));
dir.contents.set(basename(uri.path), new FsDirectoryEntry(new Map(), now, now));
}
private getEntry(uri: vscode.Uri): FsEntry | void {
private getEntry(uri: vscode.Uri): FsEntry | undefined {
// TODO: have this throw FileNotFound itself?
// TODO: support configuring case sensitivity
let node: FsEntry = this.root;
@ -104,13 +115,12 @@ export class MemFs implements vscode.FileSystemProvider {
continue;
}
if (node.type !== vscode.FileType.Directory) {
if (!(node instanceof FsDirectoryEntry)) {
// We're looking at a File or such, so bail.
return;
}
const next = node.contents.get(component);
if (!next) {
// not found!
return;
@ -121,11 +131,14 @@ export class MemFs implements vscode.FileSystemProvider {
return node;
}
private getParent(uri: vscode.Uri) {
private getParent(uri: vscode.Uri): FsDirectoryEntry {
const dir = this.getEntry(uri.with({ path: dirname(uri.path) }));
if (!dir) {
throw vscode.FileSystemError.FileNotFound();
}
if (!(dir instanceof FsDirectoryEntry)) {
throw vscode.FileSystemError.FileNotADirectory();
}
return dir;
}
@ -153,46 +166,32 @@ export class MemFs implements vscode.FileSystemProvider {
}
}
class FsEntry {
get type(): vscode.FileType {
if (this._data instanceof Uint8Array) {
return vscode.FileType.File;
} else {
return vscode.FileType.Directory;
}
}
class FsFileEntry {
readonly type = vscode.FileType.File;
get size(): number {
if (this.type === vscode.FileType.Directory) {
return [...this.contents.values()].reduce((acc: number, entry: FsEntry) => acc + entry.size, 0);
} else {
return this.data.length;
}
return this.data.length;
}
constructor(
private _data: Uint8Array | Map<string, FsEntry>,
public ctime: number,
public data: Uint8Array,
public readonly ctime: number,
public mtime: number,
) { }
get data() {
if (this.type === vscode.FileType.Directory) {
throw vscode.FileSystemError.FileIsADirectory;
}
return <Uint8Array>this._data;
}
set data(val: Uint8Array) {
if (this.type === vscode.FileType.Directory) {
throw vscode.FileSystemError.FileIsADirectory;
}
this._data = val;
}
get contents() {
if (this.type !== vscode.FileType.Directory) {
throw vscode.FileSystemError.FileNotADirectory;
}
return <Map<string, FsEntry>>this._data;
}
}
class FsDirectoryEntry {
readonly type = vscode.FileType.Directory;
get size(): number {
return [...this.contents.values()].reduce((acc: number, entry: FsEntry) => acc + entry.size, 0);
}
constructor(
public readonly contents: Map<string, FsEntry>,
public readonly ctime: number,
public readonly mtime: number,
) { }
}
type FsEntry = FsFileEntry | FsDirectoryEntry;

View file

@ -20,5 +20,5 @@ export function equals<T>(
}
export function coalesce<T>(array: ReadonlyArray<T | undefined>): T[] {
return <T[]>array.filter(e => !!e);
return array.filter((e): e is T => !!e);
}