Skip to content

Commit

Permalink
Fix a few more type assertions (microsoft#223138)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjbvz authored Jul 23, 2024
1 parent d4d2ee3 commit d4bb523
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 31 deletions.
10 changes: 10 additions & 0 deletions src/vs/base/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ export function validateConstraint(arg: unknown, constraint: TypeConstraint | un
}
}

/**
* Helper type assertion that safely upcasts a type to a supertype.
*
* This can be used to make sure the argument correctly conforms to the subtype while still being able to pass it
* to contexts that expects the supertype.
*/
export function upcast<Base, Sub extends Base>(x: Sub): Base {
return x;
}

type AddFirstParameterToFunction<T, TargetFunctionsReturnType, FirstParameter> = T extends (...args: any[]) => TargetFunctionsReturnType ?
// Function: add param to function
(firstArg: FirstParameter, ...args: Parameters<T>) => ReturnType<T> :
Expand Down
11 changes: 6 additions & 5 deletions src/vs/platform/utilityProcess/electron-main/utilityProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { removeDangerousEnvVariables } from 'vs/base/common/processes';
import { deepClone } from 'vs/base/common/objects';
import { isWindows } from 'vs/base/common/platform';
import { isUNCAccessRestrictionsDisabled, getUNCHostAllowlist } from 'vs/base/node/unc';
import { upcast } from 'vs/base/common/types';

export interface IUtilityProcessConfiguration {

Expand Down Expand Up @@ -249,18 +250,18 @@ export class UtilityProcess extends Disposable {
this.log('creating new...', Severity.Info);

// Fork utility process
this.process = utilityProcess.fork(modulePath, args, {
this.process = utilityProcess.fork(modulePath, args, upcast<ForkOptions, ForkOptions & {
forceAllocationsToV8Sandbox?: boolean;
respondToAuthRequestsFromMainProcess?: boolean;
}>({
serviceName,
env,
execArgv,
allowLoadingUnsignedLibraries,
forceAllocationsToV8Sandbox,
respondToAuthRequestsFromMainProcess,
stdio
} as ForkOptions & {
forceAllocationsToV8Sandbox?: Boolean;
respondToAuthRequestsFromMainProcess?: boolean;
});
}));

// Register to events
this.registerListeners(this.process, this.configuration, serviceName);
Expand Down
8 changes: 4 additions & 4 deletions src/vs/workbench/api/common/extHost.api.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AsyncIterableObject } from 'vs/base/common/async';
import { CancellationTokenSource } from 'vs/base/common/cancellation';
import * as errors from 'vs/base/common/errors';
import { Emitter, Event } from 'vs/base/common/event';
import { combinedDisposable, IDisposable } from 'vs/base/common/lifecycle';
import { combinedDisposable } from 'vs/base/common/lifecycle';
import { Schemas, matchesScheme } from 'vs/base/common/network';
import Severity from 'vs/base/common/severity';
import { URI } from 'vs/base/common/uri';
Expand Down Expand Up @@ -1206,17 +1206,17 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
},
registerFileSearchProviderNew: (scheme: string, provider: vscode.FileSearchProviderNew) => {
checkProposedApiEnabled(extension, 'fileSearchProviderNew');
return <IDisposable>{ dispose: () => { } };
return { dispose: () => { } };
},
registerTextSearchProviderNew: (scheme: string, provider: vscode.TextSearchProviderNew) => {
checkProposedApiEnabled(extension, 'textSearchProviderNew');
return <IDisposable>{ dispose: () => { } };
return { dispose: () => { } };
},
registerAITextSearchProviderNew: (scheme: string, provider: vscode.AITextSearchProviderNew) => {
// there are some dependencies on textSearchProvider, so we need to check for both
checkProposedApiEnabled(extension, 'aiTextSearchProviderNew');
checkProposedApiEnabled(extension, 'textSearchProviderNew');
return <IDisposable>{ dispose: () => { } };
return { dispose: () => { } };
},
registerRemoteAuthorityResolver: (authorityPrefix: string, resolver: vscode.RemoteAuthorityResolver) => {
checkProposedApiEnabled(extension, 'resolvers');
Expand Down
30 changes: 15 additions & 15 deletions src/vs/workbench/api/common/extHostTypeConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ export namespace SymbolTag {

export namespace WorkspaceSymbol {
export function from(info: vscode.SymbolInformation): search.IWorkspaceSymbol {
return <search.IWorkspaceSymbol>{
return {
name: info.name,
kind: SymbolKind.from(info.kind),
tags: info.tags && info.tags.map(SymbolTag.from),
Expand Down Expand Up @@ -966,7 +966,7 @@ export namespace Hover {

export namespace EvaluatableExpression {
export function from(expression: vscode.EvaluatableExpression): languages.EvaluatableExpression {
return <languages.EvaluatableExpression>{
return {
range: Range.from(expression.range),
expression: expression.expression
};
Expand All @@ -980,24 +980,24 @@ export namespace EvaluatableExpression {
export namespace InlineValue {
export function from(inlineValue: vscode.InlineValue): languages.InlineValue {
if (inlineValue instanceof types.InlineValueText) {
return <languages.InlineValueText>{
return {
type: 'text',
range: Range.from(inlineValue.range),
text: inlineValue.text
};
} satisfies languages.InlineValueText;
} else if (inlineValue instanceof types.InlineValueVariableLookup) {
return <languages.InlineValueVariableLookup>{
return {
type: 'variable',
range: Range.from(inlineValue.range),
variableName: inlineValue.variableName,
caseSensitiveLookup: inlineValue.caseSensitiveLookup
};
} satisfies languages.InlineValueVariableLookup;
} else if (inlineValue instanceof types.InlineValueEvaluatableExpression) {
return <languages.InlineValueExpression>{
return {
type: 'expression',
range: Range.from(inlineValue.range),
expression: inlineValue.expression
};
} satisfies languages.InlineValueExpression;
} else {
throw new Error(`Unknown 'InlineValue' type`);
}
Expand All @@ -1006,28 +1006,28 @@ export namespace InlineValue {
export function to(inlineValue: languages.InlineValue): vscode.InlineValue {
switch (inlineValue.type) {
case 'text':
return <vscode.InlineValueText>{
return {
range: Range.to(inlineValue.range),
text: inlineValue.text
};
} satisfies vscode.InlineValueText;
case 'variable':
return <vscode.InlineValueVariableLookup>{
return {
range: Range.to(inlineValue.range),
variableName: inlineValue.variableName,
caseSensitiveLookup: inlineValue.caseSensitiveLookup
};
} satisfies vscode.InlineValueVariableLookup;
case 'expression':
return <vscode.InlineValueEvaluatableExpression>{
return {
range: Range.to(inlineValue.range),
expression: inlineValue.expression
};
} satisfies vscode.InlineValueEvaluatableExpression;
}
}
}

export namespace InlineValueContext {
export function from(inlineValueContext: vscode.InlineValueContext): extHostProtocol.IInlineValueContextDto {
return <extHostProtocol.IInlineValueContextDto>{
return {
frameId: inlineValueContext.frameId,
stoppedLocation: Range.from(inlineValueContext.stoppedLocation)
};
Expand Down
9 changes: 2 additions & 7 deletions src/vs/workbench/test/browser/workbenchTestServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ import { IEnterWorkspaceResult, IRecent, IRecentlyOpened, IWorkspaceFolderCreati
import { IWorkspaceTrustManagementService, IWorkspaceTrustRequestService } from 'vs/platform/workspace/common/workspaceTrust';
import { IExtensionTerminalProfile, IShellLaunchConfig, ITerminalBackend, ITerminalLogService, ITerminalProfile, TerminalIcon, TerminalLocation, TerminalShellType } from 'vs/platform/terminal/common/terminal';
import { ICreateTerminalOptions, IDeserializedTerminalEditorInput, ITerminalConfigurationService, ITerminalEditorService, ITerminalGroup, ITerminalGroupService, ITerminalInstance, ITerminalInstanceService, TerminalEditorLocation } from 'vs/workbench/contrib/terminal/browser/terminal';
import { assertIsDefined } from 'vs/base/common/types';
import { assertIsDefined, upcast } from 'vs/base/common/types';
import { IRegisterContributedProfileArgs, IShellLaunchConfigResolveOptions, ITerminalProfileProvider, ITerminalProfileResolverService, ITerminalProfileService } from 'vs/workbench/contrib/terminal/common/terminal';
import { EditorResolverService } from 'vs/workbench/services/editor/browser/editorResolverService';
import { FILE_EDITOR_INPUT_ID } from 'vs/workbench/contrib/files/common/files';
Expand Down Expand Up @@ -211,12 +211,7 @@ export class TestTextFileEditor extends TextFileEditor {
}

setSelection(selection: Selection | undefined, reason: EditorPaneSelectionChangeReason): void {
if (selection) {
const options: ITextEditorOptions = { selection };
this._options = options;
} else {
this._options = undefined;
}
this._options = selection ? upcast<IEditorOptions, ITextEditorOptions>({ selection }) : undefined;

this._onDidChangeSelection.fire({ reason });
}
Expand Down

0 comments on commit d4bb523

Please sign in to comment.