fix: avoid fs dependency in web (#192565)

* feat: quick fix for redundant activation events

* fix: avoid `fs` dependency in web
This commit is contained in:
Joyce Er
2023-09-08 05:23:04 -07:00
committed by GitHub
parent f4700718ba
commit 0795888f65
4 changed files with 40 additions and 3 deletions

View File

@@ -0,0 +1,9 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { l10n } from 'vscode';
export const implicitActivationEvent = l10n.t("This activation event cannot be explicitly listed by your extension.");
export const redundantImplicitActivationEvent = l10n.t("This activation event can be removed as VS Code generates these automatically from your package.json contribution declarations.");

View File

@@ -12,10 +12,12 @@ export function activate(context: vscode.ExtensionContext) {
//package.json suggestions
context.subscriptions.push(registerPackageDocumentCompletions());
//package.json code actions for lint warnings
context.subscriptions.push(registerCodeActionsProvider());
context.subscriptions.push(new ExtensionLinter());
}
function registerPackageDocumentCompletions(): vscode.Disposable {
return vscode.languages.registerCompletionItemProvider({ language: 'json', pattern: '**/package.json' }, {
provideCompletionItems(document, position, token) {
@@ -23,3 +25,11 @@ function registerPackageDocumentCompletions(): vscode.Disposable {
}
});
}
function registerCodeActionsProvider(): vscode.Disposable {
return vscode.languages.registerCodeActionsProvider({ language: 'json', pattern: '**/package.json' }, {
provideCodeActions(document, range, context, token) {
return new PackageDocument(document).provideCodeActions(range, context, token);
}
});
}

View File

@@ -13,6 +13,7 @@ import * as MarkdownItType from 'markdown-it';
import { commands, languages, workspace, Disposable, TextDocument, Uri, Diagnostic, Range, DiagnosticSeverity, Position, env, l10n } from 'vscode';
import { INormalizedVersion, normalizeVersion, parseVersion } from './extensionEngineValidation';
import { JsonStringScanner } from './jsonReconstruct';
import { implicitActivationEvent, redundantImplicitActivationEvent } from './constants';
const product = JSON.parse(fs.readFileSync(path.join(env.appRoot, 'product.json'), { encoding: 'utf-8' }));
const allowedBadgeProviders: string[] = (product.extensionAllowedBadgeProviders || []).map((s: string) => s.toLowerCase());
@@ -32,8 +33,6 @@ const dataUrlsNotValid = l10n.t("Data URLs are not a valid image source.");
const relativeUrlRequiresHttpsRepository = l10n.t("Relative image URLs require a repository with HTTPS protocol to be specified in the package.json.");
const relativeBadgeUrlRequiresHttpsRepository = l10n.t("Relative badge URLs require a repository with HTTPS protocol to be specified in this package.json.");
const apiProposalNotListed = l10n.t("This proposal cannot be used because for this extension the product defines a fixed set of API proposals. You can test your extension but before publishing you MUST reach out to the VS Code team.");
const implicitActivationEvent = l10n.t("This activation event cannot be explicitly listed by your extension.");
const redundantImplicitActivationEvent = l10n.t("This activation event can be removed as VS Code generates these automatically from your package.json contribution declarations.");
const bumpEngineForImplicitActivationEvents = l10n.t("This activation event can be removed for extensions targeting engine version ^1.75 as VS Code will generate these automatically from your package.json contribution declarations.");
const starActivation = l10n.t("Using '*' activation is usually a bad idea as it impacts performance.");
const parsingErrorHeader = l10n.t("Error parsing the when-clause:");

View File

@@ -5,6 +5,7 @@
import * as vscode from 'vscode';
import { getLocation, Location } from 'jsonc-parser';
import { implicitActivationEvent, redundantImplicitActivationEvent } from './constants';
export class PackageDocument {
@@ -21,6 +22,24 @@ export class PackageDocument {
return undefined;
}
public provideCodeActions(_range: vscode.Range, context: vscode.CodeActionContext, _token: vscode.CancellationToken): vscode.ProviderResult<vscode.CodeAction[]> {
const codeActions: vscode.CodeAction[] = [];
for (const diagnostic of context.diagnostics) {
if (diagnostic.message === implicitActivationEvent || diagnostic.message === redundantImplicitActivationEvent) {
const codeAction = new vscode.CodeAction(vscode.l10n.t("Remove activation event"), vscode.CodeActionKind.QuickFix);
codeAction.edit = new vscode.WorkspaceEdit();
const rangeForCharAfter = diagnostic.range.with(diagnostic.range.end, diagnostic.range.end.translate(0, 1));
if (this.document.getText(rangeForCharAfter) === ',') {
codeAction.edit.delete(this.document.uri, diagnostic.range.with(undefined, diagnostic.range.end.translate(0, 1)));
} else {
codeAction.edit.delete(this.document.uri, diagnostic.range);
}
codeActions.push(codeAction);
}
}
return codeActions;
}
private provideLanguageOverridesCompletionItems(location: Location, position: vscode.Position): vscode.ProviderResult<vscode.CompletionItem[]> {
let range = this.getReplaceRange(location, position);
const text = this.document.getText(range);