From 3c99d50da5a579d9fa92d02664b1b66d4ff55944 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 6 Dec 2022 11:52:17 -0800 Subject: [PATCH] Replace a few type assertions with annotations and `satisfies`. (#51685) --- src/compiler/checker.ts | 12 +- src/harness/vfsUtil.ts | 6 +- src/server/editorServices.ts | 2 +- src/testRunner/parallel/host.ts | 4 +- .../unittests/tsserver/cancellationToken.ts | 112 +++++++++--------- 5 files changed, 68 insertions(+), 68 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3295f897ca0..22452a4eb32 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2410,10 +2410,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { if (sourceSymbolFile && targetSymbolFile && amalgamatedDuplicates && !isEitherEnum && sourceSymbolFile !== targetSymbolFile) { const firstFile = comparePaths(sourceSymbolFile.path, targetSymbolFile.path) === Comparison.LessThan ? sourceSymbolFile : targetSymbolFile; const secondFile = firstFile === sourceSymbolFile ? targetSymbolFile : sourceSymbolFile; - const filesDuplicates = getOrUpdate(amalgamatedDuplicates, `${firstFile.path}|${secondFile.path}`, () => - ({ firstFile, secondFile, conflictingSymbols: new Map() } as DuplicateInfoForFiles)); - const conflictingSymbolInfo = getOrUpdate(filesDuplicates.conflictingSymbols, symbolName, () => - ({ isBlockScoped: isEitherBlockScoped, firstFileLocations: [], secondFileLocations: [] } as DuplicateInfoForSymbol)); + const filesDuplicates = getOrUpdate(amalgamatedDuplicates, `${firstFile.path}|${secondFile.path}`, (): DuplicateInfoForFiles => + ({ firstFile, secondFile, conflictingSymbols: new Map() })); + const conflictingSymbolInfo = getOrUpdate(filesDuplicates.conflictingSymbols, symbolName, (): DuplicateInfoForSymbol => + ({ isBlockScoped: isEitherBlockScoped, firstFileLocations: [], secondFileLocations: [] })); if (!isSourcePlainJs) addDuplicateLocations(conflictingSymbolInfo.firstFileLocations, source); if (!isTargetPlainJs) addDuplicateLocations(conflictingSymbolInfo.secondFileLocations, target); } @@ -5070,7 +5070,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { interface ExportCollisionTracker { specifierText: string; - exportsWithDuplicate: ExportDeclaration[]; + exportsWithDuplicate?: ExportDeclaration[]; } type ExportCollisionTrackerTable = UnderscoreEscapedMap; @@ -5090,7 +5090,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { if (lookupTable && exportNode) { lookupTable.set(id, { specifierText: getTextOfNode(exportNode.moduleSpecifier!) - } as ExportCollisionTracker); + }); } } else if (lookupTable && exportNode && targetSymbol && resolveSymbol(targetSymbol) !== resolveSymbol(sourceSymbol)) { diff --git a/src/harness/vfsUtil.ts b/src/harness/vfsUtil.ts index 764b2fa63d0..4547e378cf3 100644 --- a/src/harness/vfsUtil.ts +++ b/src/harness/vfsUtil.ts @@ -876,7 +876,7 @@ export class FileSystem { private _mknod(dev: number, type: typeof S_IFREG, mode: number, time?: number): FileInode; private _mknod(dev: number, type: typeof S_IFDIR, mode: number, time?: number): DirectoryInode; private _mknod(dev: number, type: typeof S_IFLNK, mode: number, time?: number): SymlinkInode; - private _mknod(dev: number, type: number, mode: number, time = this.time()) { + private _mknod(dev: number, type: number, mode: number, time = this.time()): Inode { return { dev, ino: ++inoCount, @@ -886,7 +886,7 @@ export class FileSystem { ctimeMs: time, birthtimeMs: time, nlink: 0 - } as Inode; + }; } private _addLink(parent: DirectoryInode | undefined, links: collections.SortedMap, name: string, node: Inode, time = this.time()) { @@ -979,7 +979,7 @@ export class FileSystem { birthtimeMs: root.birthtimeMs, nlink: root.nlink, shadowRoot: root - } as Inode; + }; if (isSymlink(root)) (shadow as SymlinkInode).symlink = root.symlink; shadows.set(shadow.ino, shadow); diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 6b27c3296bc..ba1e906bbfe 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -2660,7 +2660,7 @@ export class ProjectService { this.eventHandler({ eventName: ConfigFileDiagEvent, data: { configFileName: project.getConfigFilePath(), diagnostics, triggerFile } - } as ConfigFileDiagEvent); + } satisfies ConfigFileDiagEvent); } private getOrCreateInferredProjectForProjectRootPathIfEnabled(info: ScriptInfo, projectRootPath: NormalizedPath | undefined): InferredProject | undefined { diff --git a/src/testRunner/parallel/host.ts b/src/testRunner/parallel/host.ts index 7ff6ca57011..1ace1b58a42 100644 --- a/src/testRunner/parallel/host.ts +++ b/src/testRunner/parallel/host.ts @@ -372,10 +372,10 @@ export function start() { } worker.currentTasks = taskList; if (taskList.length === 1) { - worker.process.send({ type: "test", payload: taskList[0] } as ParallelHostMessage); // TODO: GH#18217 + worker.process.send({ type: "test", payload: taskList[0] } satisfies ParallelHostMessage); // TODO: GH#18217 } else { - worker.process.send({ type: "batch", payload: taskList } as ParallelHostMessage); // TODO: GH#18217 + worker.process.send({ type: "batch", payload: taskList } satisfies ParallelHostMessage); // TODO: GH#18217 } } } diff --git a/src/testRunner/unittests/tsserver/cancellationToken.ts b/src/testRunner/unittests/tsserver/cancellationToken.ts index e030bccd687..4fb05770c6d 100644 --- a/src/testRunner/unittests/tsserver/cancellationToken.ts +++ b/src/testRunner/unittests/tsserver/cancellationToken.ts @@ -39,22 +39,22 @@ describe("unittests:: tsserver:: cancellationToken", () => { const session = createSession(host, { cancellationToken }); expectedRequestId = session.getNextSeq(); - session.executeCommandSeq({ - command: "open", + session.executeCommandSeq({ + command: ts.server.protocol.CommandTypes.Open, arguments: { file: f1.path } - } as ts.server.protocol.OpenRequest); + }); expectedRequestId = session.getNextSeq(); - session.executeCommandSeq({ - command: "geterr", - arguments: { files: [f1.path] } - } as ts.server.protocol.GeterrRequest); + session.executeCommandSeq({ + command: ts.server.protocol.CommandTypes.Geterr, + arguments: { files: [f1.path], delay: 0 } + }); expectedRequestId = session.getNextSeq(); - session.executeCommandSeq({ - command: "occurrences", + session.executeCommandSeq({ + command: ts.server.protocol.CommandTypes.Occurrences, arguments: { file: f1.path, line: 1, offset: 6 } - } as ts.server.protocol.OccurrencesRequest); + }); expectedRequestId = 2; host.runQueuedImmediateCallbacks(); @@ -82,15 +82,15 @@ describe("unittests:: tsserver:: cancellationToken", () => { cancellationToken }); { - session.executeCommandSeq({ - command: "open", + session.executeCommandSeq({ + command: ts.server.protocol.CommandTypes.Open, arguments: { file: f1.path } - } as ts.server.protocol.OpenRequest); + }); // send geterr for missing file - session.executeCommandSeq({ - command: "geterr", - arguments: { files: ["/a/missing"] } - } as ts.server.protocol.GeterrRequest); + session.executeCommandSeq({ + command: ts.server.protocol.CommandTypes.Geterr, + arguments: { files: ["/a/missing"], delay: 0 } + }); // Queued files assert.equal(host.getOutput().length, 0, "expected 0 message"); host.checkTimeoutQueueLengthAndRun(1); @@ -101,18 +101,18 @@ describe("unittests:: tsserver:: cancellationToken", () => { { const getErrId = session.getNextSeq(); // send geterr for a valid file - session.executeCommandSeq({ - command: "geterr", - arguments: { files: [f1.path] } - } as ts.server.protocol.GeterrRequest); + session.executeCommandSeq({ + command: ts.server.protocol.CommandTypes.Geterr, + arguments: { files: [f1.path], delay: 0 } + }); assert.equal(host.getOutput().length, 0, "expect 0 messages"); // run new request - session.executeCommandSeq({ - command: "projectInfo", - arguments: { file: f1.path } - } as ts.server.protocol.ProjectInfoRequest); + session.executeCommandSeq({ + command: ts.server.protocol.CommandTypes.ProjectInfo, + arguments: { file: f1.path, needFileNameList: false } + }); session.clearMessages(); // cancel previously issued Geterr @@ -126,10 +126,10 @@ describe("unittests:: tsserver:: cancellationToken", () => { } { const getErrId = session.getNextSeq(); - session.executeCommandSeq({ - command: "geterr", - arguments: { files: [f1.path] } - } as ts.server.protocol.GeterrRequest); + session.executeCommandSeq({ + command: ts.server.protocol.CommandTypes.Geterr, + arguments: { files: [f1.path], delay: 0 } + }); assert.equal(host.getOutput().length, 0, "expect 0 messages"); // run first step @@ -148,10 +148,10 @@ describe("unittests:: tsserver:: cancellationToken", () => { } { const getErrId = session.getNextSeq(); - session.executeCommandSeq({ - command: "geterr", - arguments: { files: [f1.path] } - } as ts.server.protocol.GeterrRequest); + session.executeCommandSeq({ + command: ts.server.protocol.CommandTypes.Geterr, + arguments: { files: [f1.path], delay: 0 } + }); assert.equal(host.getOutput().length, 0, "expect 0 messages"); // run first step @@ -178,10 +178,10 @@ describe("unittests:: tsserver:: cancellationToken", () => { } { const getErr1 = session.getNextSeq(); - session.executeCommandSeq({ - command: "geterr", - arguments: { files: [f1.path] } - } as ts.server.protocol.GeterrRequest); + session.executeCommandSeq({ + command: ts.server.protocol.CommandTypes.Geterr, + arguments: { files: [f1.path], delay: 0 } + }); assert.equal(host.getOutput().length, 0, "expect 0 messages"); // run first step host.runQueuedTimeoutCallbacks(); @@ -190,10 +190,10 @@ describe("unittests:: tsserver:: cancellationToken", () => { assert.equal(e1.event, "syntaxDiag"); session.clearMessages(); - session.executeCommandSeq({ - command: "geterr", - arguments: { files: [f1.path] } - } as ts.server.protocol.GeterrRequest); + session.executeCommandSeq({ + command: ts.server.protocol.CommandTypes.Geterr, + arguments: { files: [f1.path], delay: 0 } + }); // make sure that getErr1 is completed verifyRequestCompleted(getErr1, 0); } @@ -230,34 +230,34 @@ describe("unittests:: tsserver:: cancellationToken", () => { throttleWaitMilliseconds: 0 }); { - session.executeCommandSeq({ - command: "open", + session.executeCommandSeq({ + command: ts.server.protocol.CommandTypes.Open, arguments: { file: f1.path } - } as ts.server.protocol.OpenRequest); + }); // send navbar request (normal priority) - session.executeCommandSeq({ - command: "navbar", + session.executeCommandSeq({ + command: ts.server.protocol.CommandTypes.NavBar, arguments: { file: f1.path } - } as ts.server.protocol.NavBarRequest); + }); // ensure the nav bar request can be canceled - verifyExecuteCommandSeqIsCancellable({ - command: "navbar", + verifyExecuteCommandSeqIsCancellable({ + command: ts.server.protocol.CommandTypes.NavBar, arguments: { file: f1.path } - } as ts.server.protocol.NavBarRequest); + }); // send outlining spans request (normal priority) - session.executeCommandSeq({ - command: "outliningSpans", + session.executeCommandSeq({ + command: ts.server.protocol.CommandTypes.GetOutliningSpansFull, arguments: { file: f1.path } - } as ts.server.protocol.OutliningSpansRequestFull); + }); // ensure the outlining spans request can be canceled - verifyExecuteCommandSeqIsCancellable({ - command: "outliningSpans", + verifyExecuteCommandSeqIsCancellable({ + command: ts.server.protocol.CommandTypes.GetOutliningSpansFull, arguments: { file: f1.path } - } as ts.server.protocol.OutliningSpansRequestFull); + }); } function verifyExecuteCommandSeqIsCancellable(request: TestSessionRequest) {