Use ??= in more places (#163594)

This commit is contained in:
Matt Bierner 2022-10-13 14:59:35 -07:00 committed by GitHub
parent 65a9097aa2
commit 186d3415a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 16 additions and 44 deletions

View file

@ -21,10 +21,7 @@ export class FindFileReferencesCommand implements Command {
) { }
public async execute(resource?: vscode.Uri) {
if (!resource) {
resource = vscode.window.activeTextEditor?.document.uri;
}
resource ??= vscode.window.activeTextEditor?.document.uri;
if (!resource) {
vscode.window.showErrorMessage(localize('error.noResource', "Find file references failed. No resource provided."));
return;

View file

@ -35,9 +35,7 @@ export class VsCodeOutputLogger extends Disposable implements ILogger {
private _outputChannel?: vscode.OutputChannel;
private get outputChannel() {
if (!this._outputChannel) {
this._outputChannel = this._register(vscode.window.createOutputChannel('Markdown'));
}
this._outputChannel ??= this._register(vscode.window.createOutputChannel('Markdown'));
return this._outputChannel;
}

View file

@ -144,9 +144,7 @@ class VSCodeExtensionMarkdownContributionProvider extends Disposable implements
public readonly onContributionsChanged = this._onContributionsChanged.event;
public get contributions(): MarkdownContributions {
if (!this._contributions) {
this._contributions = this.getCurrentContributions();
}
this._contributions ??= this.getCurrentContributions();
return this._contributions;
}

View file

@ -866,9 +866,7 @@ export class Color {
private _toString?: string;
toString(): string {
if (!this._toString) {
this._toString = Color.Format.CSS.format(this);
}
this._toString ??= Color.Format.CSS.format(this);
return this._toString;
}

View file

@ -35,9 +35,7 @@ export class SimpleBrowserManager {
const url = state?.url ?? '';
const view = SimpleBrowserView.restore(this.extensionUri, url, panel);
this.registerWebviewListeners(view);
if (!this._activeView) {
this._activeView = view;
}
this._activeView ??= view;
}
private registerWebviewListeners(view: SimpleBrowserView) {

View file

@ -117,13 +117,9 @@ class MyCompletionItem extends vscode.CompletionItem {
if (tsEntry.kindModifiers) {
const kindModifiers = parseKindModifier(tsEntry.kindModifiers);
if (kindModifiers.has(PConst.KindModifiers.optional)) {
if (!this.insertText) {
this.insertText = this.textLabel;
}
this.insertText ??= this.textLabel;
this.filterText ??= this.textLabel;
if (!this.filterText) {
this.filterText = this.textLabel;
}
if (typeof this.label === 'string') {
this.label += '?';
} else {

View file

@ -30,10 +30,7 @@ class FileReferencesCommand implements Command {
return;
}
if (!resource) {
resource = vscode.window.activeTextEditor?.document.uri;
}
resource ??= vscode.window.activeTextEditor?.document.uri;
if (!resource) {
vscode.window.showErrorMessage(localize('error.noResource', "Find file references failed. No resource provided."));
return;

View file

@ -54,14 +54,10 @@ class ConditionalRegistration {
private update() {
const enabled = this.conditions.every(condition => condition.value);
if (enabled) {
if (!this.registration) {
this.registration = this.doRegister();
}
this.registration ??= this.doRegister();
} else {
if (this.registration) {
this.registration.dispose();
this.registration = undefined;
}
this.registration?.dispose();
this.registration = undefined;
}
}
}

View file

@ -28,7 +28,7 @@ namespace TypeScriptServerPlugin {
export class PluginManager extends Disposable {
private readonly _pluginConfigurations = new Map<string, {}>();
private _plugins: Map<string, ReadonlyArray<TypeScriptServerPlugin>> | undefined;
private _plugins?: Map<string, ReadonlyArray<TypeScriptServerPlugin>>;
constructor() {
super();
@ -37,6 +37,7 @@ export class PluginManager extends Disposable {
if (!this._plugins) {
return;
}
const newPlugins = this.readPlugins();
if (!arrays.equals(Array.from(this._plugins.values()).flat(), Array.from(newPlugins.values()).flat(), TypeScriptServerPlugin.equals)) {
this._plugins = newPlugins;
@ -46,9 +47,7 @@ export class PluginManager extends Disposable {
}
public get plugins(): ReadonlyArray<TypeScriptServerPlugin> {
if (!this._plugins) {
this._plugins = this.readPlugins();
}
this._plugins ??= this.readPlugins();
return Array.from(this._plugins.values()).flat();
}

View file

@ -34,9 +34,7 @@ const getRootTempDir = (() => {
export const getInstanceTempDir = (() => {
let dir: string | undefined;
return () => {
if (!dir) {
dir = path.join(getRootTempDir(), makeRandomHexString(20));
}
dir ??= path.join(getRootTempDir(), makeRandomHexString(20));
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}

View file

@ -1305,10 +1305,7 @@ async function webviewPreloads(ctx: PreloadContext) {
}
private load(): Promise<rendererApi.RendererApi | undefined> {
if (!this._loadPromise) {
this._loadPromise = this._load();
}
this._loadPromise ??= this._load();
return this._loadPromise;
}