Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Fixing an issue where webhint was not being loaded #2418

Merged
merged 7 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 46 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@
"utf-8-validate": "6.0.4",
"vscode-chrome-debug-core": "6.8.11",
"vscode-extension-telemetry": "0.4.1",
"vscode-webhint": "2.1.11",
"vscode-webhint": "2.1.15",
"ws": "8.18.0",
"xmlhttprequest": "1.8.0"
},
Expand Down Expand Up @@ -712,7 +712,7 @@
"uglify-js": "3.19.3",
"unzipper": "0.12.3",
"jest-environment-jsdom": "^28.1.3",
"vscode-languageclient": "7.0.0",
"vscode-languageclient": "9.0.1",
"vscode-test": "1.6.1",
"webpack": "5.94.0",
"webpack-cli": "5.1.4"
Expand Down
67 changes: 33 additions & 34 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ import {
import { LaunchConfigManager, providedHeadlessDebugConfig, providedLaunchDevToolsConfig } from './launchConfigManager';
import { ErrorReporter } from './errorReporter';
import { ErrorCodes } from './common/errorCodes';
import {
LanguageClient,
import type {
LanguageClientOptions,
ServerOptions,
} from 'vscode-languageclient/node';
import {
LanguageClient,
TransportKind,
} from 'vscode-languageclient/node';
import type { installFailed, showOutput } from 'vscode-webhint/dist/src/utils/notifications';
Expand Down Expand Up @@ -257,12 +259,12 @@ export function activate(context: vscode.ExtensionContext): void {

const settingsConfig = vscode.workspace.getConfiguration(SETTINGS_STORE_NAME);
if (settingsConfig.get('webhint')) {
startWebhint(context);
void startWebhint(context);
}
vscode.workspace.onDidChangeConfiguration(event => {
if (event.affectsConfiguration(`${SETTINGS_STORE_NAME}.webhint`)) {
if (vscode.workspace.getConfiguration(SETTINGS_STORE_NAME).get('webhint')) {
startWebhint(context);
void startWebhint(context);
} else {
void stopWebhint();
}
Expand Down Expand Up @@ -303,7 +305,7 @@ export async function launchScreencast(context: vscode.ExtensionContext, fileUri
}
}

function startWebhint(context: vscode.ExtensionContext): void {
async function startWebhint(context: vscode.ExtensionContext): Promise<void> {
const args = [context.globalStoragePath, languageServerName];
const module = context.asAbsolutePath('node_modules/vscode-webhint/dist/src/server.js');
const transport = TransportKind.ipc;
Expand Down Expand Up @@ -370,37 +372,34 @@ function startWebhint(context: vscode.ExtensionContext): void {

// Create and start the client (also starts the server).
client = new LanguageClient('Microsoft Edge Tools', serverOptions, clientOptions);

void client.onReady().then(() => {
// Listen for notification that the webhint install failed.
const installFailedNotification: typeof installFailed = 'vscode-webhint/install-failed';
const disableInstallFailedNotification = vscode.workspace.getConfiguration(SETTINGS_STORE_NAME).get('webhintInstallNotification');
client.onNotification(installFailedNotification, () => {
if (!telemetryReporter) {
telemetryReporter = createTelemetryReporter(context);
}
telemetryReporter.sendTelemetryEvent('user/webhint/install-failed');
if (!disableInstallFailedNotification) {
const message = 'Ensure `node` and `npm` are installed to enable automatically reporting issues in source files pertaining to accessibility, compatibility, security, and more.';
void vscode.window.showInformationMessage(message, 'Remind me Later', 'Don\'t show again', 'Disable Extension').then(button => {
if (button === 'Disable Extension') {
void vscode.workspace.getConfiguration(SETTINGS_STORE_NAME).update('webhint', false, vscode.ConfigurationTarget.Global);
}
if (button === 'Don\'t show again') {
void vscode.workspace.getConfiguration(SETTINGS_STORE_NAME).update('webhintInstallNotification', true, vscode.ConfigurationTarget.Global);
}
});
}
});
// Listen for requests to show the output panel for this extension.
const showOutputNotification: typeof showOutput = 'vscode-webhint/show-output';
client.onNotification(showOutputNotification, () => {
client.outputChannel.clear();
client.outputChannel.show(true);
});
// Listen for notification that the webhint install failed.
const installFailedNotification: typeof installFailed = 'vscode-webhint/install-failed';
const disableInstallFailedNotification = vscode.workspace.getConfiguration(SETTINGS_STORE_NAME).get('webhintInstallNotification');
client.onNotification(installFailedNotification, () => {
if (!telemetryReporter) {
telemetryReporter = createTelemetryReporter(context);
}
telemetryReporter.sendTelemetryEvent('user/webhint/install-failed');
if (!disableInstallFailedNotification) {
const message = 'Ensure `node` and `npm` are installed to enable automatically reporting issues in source files pertaining to accessibility, compatibility, security, and more.';
void vscode.window.showInformationMessage(message, 'Remind me Later', 'Don\'t show again', 'Disable Extension').then(button => {
if (button === 'Disable Extension') {
void vscode.workspace.getConfiguration(SETTINGS_STORE_NAME).update('webhint', false, vscode.ConfigurationTarget.Global);
}
if (button === 'Don\'t show again') {
void vscode.workspace.getConfiguration(SETTINGS_STORE_NAME).update('webhintInstallNotification', true, vscode.ConfigurationTarget.Global);
}
});
}
});

client.start();
// Listen for requests to show the output panel for this extension.
const showOutputNotification: typeof showOutput = 'vscode-webhint/show-output';
client.onNotification(showOutputNotification, () => {
client.outputChannel.clear();
client.outputChannel.show(true);
});
await client.start();
}

async function stopWebhint(): Promise<void> {
Expand Down
6 changes: 5 additions & 1 deletion test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { ExtensionContext, Uri} from "vscode";
import TelemetryReporter from "vscode-extension-telemetry";
import { createFakeExtensionContext, createFakeTelemetryReporter, createFakeVSCode, Mocked } from "./helpers/helpers";
import { createFakeExtensionContext, createFakeTelemetryReporter, createFakeVSCode, createFakeLanguageClient, Mocked } from "./helpers/helpers";
import {
buttonCode,
IRemoteTargetJson,
Expand All @@ -14,6 +14,7 @@ import {
} from "../src/utils";

jest.mock("vscode", () => createFakeVSCode(), { virtual: true });
jest.mock("vscode-languageclient/node", () => createFakeLanguageClient(), { virtual: true });

describe("extension", () => {
const fakeRuntimeConfig: Partial<IRuntimeConfig> = {};
Expand Down Expand Up @@ -60,6 +61,9 @@ describe("extension", () => {
};
});

const mockLanguageClient = createFakeLanguageClient()
jest.doMock("vscode-languageclient/node", () => mockLanguageClient, { virtual: true });

// Mock out vscode command registration
const mockVSCode = createFakeVSCode();
commandMock = mockVSCode.commands.registerCommand;
Expand Down
16 changes: 16 additions & 0 deletions test/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,19 @@ export function getFirstCallback(mock: jest.Mock, callbackArgIndex: number = 0):
{ callback: Function, thisObj: object } {
return { callback: mock.mock.calls[0][callbackArgIndex], thisObj: mock.mock.instances[0] };
}

export function createFakeLanguageClient() {
const createFakeLanguageClient = jest.fn().mockImplementation(() => {
return {
LanguageClient: function LanguageClient() { /* constructor */ }
}
});
const createFakeLTransportKind = jest.fn().mockImplementation(() => {
return {
TransportKind: function TransportKind() { /* constructor */ }
}
});
return { LanguageClient: createFakeLanguageClient,
TransportKind: createFakeLTransportKind
}
}
Loading