Skip to content

Commit

Permalink
removed notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
ntotten committed Mar 16, 2021
1 parent 7962e74 commit 899e952
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 212 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to the "prettier-vscode" extension will be documented in thi

<!-- Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. -->

## [6.3.0]

- Removed notifications, all messages logged with status icon update.

## [6.2.1]

- Fixed regressions where VS Code settings `settings.json` could not be formatted
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "prettier-vscode",
"displayName": "Prettier - Code formatter",
"description": "Code formatter using prettier",
"version": "6.2.1",
"version": "6.3.0",
"publisher": "esbenp",
"author": "Prettier <@prettiercode>",
"galleryBanner": {
Expand Down
107 changes: 69 additions & 38 deletions src/ModuleResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ import * as path from "path";
import * as prettier from "prettier";
import * as resolve from "resolve";
import * as semver from "semver";
import { commands, Disposable, Uri } from "vscode";
import {
commands,
Disposable,
MessageItem,
Uri,
window,
workspace,
} from "vscode";
import { resolveGlobalNodePath, resolveGlobalYarnPath } from "./Files";
import { LoggingService } from "./LoggingService";
import {
FAILED_TO_LOAD_MODULE_MESSAGE,
INVALID_PRETTIER_PATH_MESSAGE,
OUTDATED_PRETTIER_INSTALLED,
OUTDATED_PRETTIER_VERSION_MESSAGE,
USING_BUNDLED_PRETTIER,
} from "./message";
import {
ConfirmationSelection,
NotificationService,
} from "./NotificationService";
import {
getFromGlobalState,
getFromWorkspaceState,
Expand All @@ -34,6 +37,16 @@ declare const __non_webpack_require__: typeof require;
const alwaysAllowedExecutionStateKey = "PRETTIER_MODULE_ALWAYS_ALLOWED";
const moduleExecutionStateKey = "moduleExecutionState";

export enum ConfirmationSelection {
deny = 1,
allow = 2,
alwaysAllow = 3,
}

export interface ConfirmMessageItem extends MessageItem {
value: ConfirmationSelection;
}

interface PrettierExecutionState {
libs: { [key: string]: boolean };
}
Expand Down Expand Up @@ -73,14 +86,46 @@ function globalPathGet(packageManager: PackageManagers): string | undefined {
return undefined;
}

async function askForModuleApproval(
modulePath: string,
isGlobal: boolean
): Promise<ConfirmationSelection> {
const libraryUri = Uri.file(modulePath);
const folder = workspace.getWorkspaceFolder(libraryUri);
let message: string;
if (folder !== undefined) {
const relativePath = workspace.asRelativePath(libraryUri);
message = `The Prettier extension will use '${relativePath}' for validation, which is installed locally in folder '${folder.name}'. Do you allow the execution of this Prettier version including all plugins and configuration files it will load on your behalf?\n\nPress 'Allow Everywhere' to remember the choice for all workspaces.`;
} else {
message = isGlobal
? `The Prettier extension will use a globally installed Prettier library for validation. Do you allow the execution of this Prettier version including all plugins and configuration files it will load on your behalf?\n\nPress 'Always Allow' to remember the choice for all workspaces.`
: `The Prettier extension will use a locally installed Prettier library for validation. Do you allow the execution of this Prettier version including all plugins and configuration files it will load on your behalf?\n\nPress 'Always Allow' to remember the choice for all workspaces.`;
}

const messageItems: ConfirmMessageItem[] = [
{ title: "Allow Everywhere", value: ConfirmationSelection.alwaysAllow },
{ title: "Allow", value: ConfirmationSelection.allow },
{ title: "Deny", value: ConfirmationSelection.deny },
];
const item = await window.showInformationMessage<ConfirmMessageItem>(
message,
{ modal: true },
...messageItems
);

// Dialog got canceled.
if (item === undefined) {
return ConfirmationSelection.deny;
} else {
return item.value;
}
}

export class ModuleResolver implements Disposable {
private path2Module = new Map<string, PrettierModule>();
private deniedModules = new Set<string>();

constructor(
private loggingService: LoggingService,
private notificationService: NotificationService
) {}
constructor(private loggingService: LoggingService) {}

/**
* Returns an instance of the prettier module.
Expand Down Expand Up @@ -117,13 +162,12 @@ export class ModuleResolver implements Disposable {
}
}

this.loggingService.logError(`Failed to local Prettier module.`, error);

this.notificationService.showErrorMessage(FAILED_TO_LOAD_MODULE_MESSAGE, [
this.loggingService.logInfo(
`Attempted to load Prettier module from ${
modulePath || moduleDirectory || "package.json"
}`,
]);
}`
);
this.loggingService.logError(FAILED_TO_LOAD_MODULE_MESSAGE, error);

// Return here because there is a local module, but we can't resolve it.
// Must do NPM install for prettier to work.
Expand Down Expand Up @@ -170,19 +214,12 @@ export class ModuleResolver implements Disposable {
return undefined;
}
} catch (error) {
this.loggingService.logError(
`Failed to load Prettier module.`,
error
);

this.notificationService.showErrorMessage(
FAILED_TO_LOAD_MODULE_MESSAGE,
[
`Attempted to load Prettier module from ${
modulePath || "package.json"
}`,
]
this.loggingService.logInfo(
`Attempted to load Prettier module from ${
modulePath || "package.json"
}`
);
this.loggingService.logError(FAILED_TO_LOAD_MODULE_MESSAGE, error);

// Returning here because module didn't load.
return undefined;
Expand All @@ -207,17 +244,14 @@ export class ModuleResolver implements Disposable {

if (!isPrettierInstance && prettierPath) {
this.loggingService.logError(INVALID_PRETTIER_PATH_MESSAGE);
this.notificationService.showErrorMessage(
INVALID_PRETTIER_PATH_MESSAGE
);
return undefined;
}

if (!isValidVersion) {
// We only prompt when formatting a file. If we did it on load there
// could be lots of these notifications which would be annoying.
this.notificationService.warnOutdatedPrettierVersion(modulePath);
this.loggingService.logError(OUTDATED_PRETTIER_INSTALLED);
this.loggingService.logInfo(
`Attempted to load Prettier module from ${modulePath}`
);
this.loggingService.logError(OUTDATED_PRETTIER_VERSION_MESSAGE);
return undefined;
}
}
Expand Down Expand Up @@ -269,10 +303,7 @@ export class ModuleResolver implements Disposable {

let isTrustedModule = moduleState.libs[modulePath];
if (!isTrustedModule) {
const approvalResult = await this.notificationService.askForModuleApproval(
modulePath,
isGlobal
);
const approvalResult = await askForModuleApproval(modulePath, isGlobal);

if (approvalResult === ConfirmationSelection.alwaysAllow) {
isTrustedModule = true;
Expand Down
81 changes: 0 additions & 81 deletions src/NotificationService.ts

This file was deleted.

12 changes: 2 additions & 10 deletions src/PrettierEditService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@ import {
getParserFromLanguageId,
} from "./languageFilters";
import { LoggingService } from "./LoggingService";
import {
INVALID_PRETTIER_CONFIG,
RESTART_TO_ENABLE,
UNABLE_TO_LOAD_PRETTIER,
} from "./message";
import { INVALID_PRETTIER_CONFIG, RESTART_TO_ENABLE } from "./message";
import { ModuleResolver } from "./ModuleResolver";
import { NotificationService } from "./NotificationService";
import { PrettierEditProvider } from "./PrettierEditProvider";
import { FormatterStatus, StatusBar } from "./StatusBar";
import {
Expand Down Expand Up @@ -65,7 +60,6 @@ export default class PrettierEditService implements Disposable {
constructor(
private moduleResolver: ModuleResolver,
private loggingService: LoggingService,
private notificationService: NotificationService,
private statusBar: StatusBar
) {}

Expand Down Expand Up @@ -182,7 +176,6 @@ export default class PrettierEditService implements Disposable {
this.loggingService.logError(
"The Prettier extension is blocked from execution in this project."
);
//this.notificationService.showErrorMessage(INVALID_PRETTIER_CONFIG);
this.statusBar.update(FormatterStatus.Disabled);
this.registeredWorkspaces.add(workspaceFolder.uri.fsPath);
return;
Expand Down Expand Up @@ -390,7 +383,7 @@ export default class PrettierEditService implements Disposable {
"Invalid prettier configuration file detected.",
error
);
this.notificationService.showErrorMessage(INVALID_PRETTIER_CONFIG);
this.loggingService.logError(INVALID_PRETTIER_CONFIG);
this.statusBar.update(FormatterStatus.Error);
return;
}
Expand Down Expand Up @@ -430,7 +423,6 @@ export default class PrettierEditService implements Disposable {
this.loggingService.logError(
"Prettier could not be loaded. See previous logs for more information."
);
this.notificationService.showErrorMessage(UNABLE_TO_LOAD_PRETTIER);
this.statusBar.update(FormatterStatus.Error);
return;
}
Expand Down
8 changes: 1 addition & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { commands, ExtensionContext, workspace } from "vscode";
import { createConfigFile } from "./commands";
import { LoggingService } from "./LoggingService";
import { ModuleResolver } from "./ModuleResolver";
import { NotificationService } from "./NotificationService";
import PrettierEditService from "./PrettierEditService";
import { StatusBar } from "./StatusBar";
import { TemplateService } from "./TemplateService";
Expand Down Expand Up @@ -42,19 +41,14 @@ export function activate(context: ExtensionContext) {
setWorkspaceState(context.workspaceState);

const templateService = new TemplateService(loggingService);
const notificationService = new NotificationService(loggingService);

const moduleResolver = new ModuleResolver(
loggingService,
notificationService
);
const moduleResolver = new ModuleResolver(loggingService);

const statusBar = new StatusBar();

const editService = new PrettierEditService(
moduleResolver,
loggingService,
notificationService,
statusBar
);
editService.registerGlobal();
Expand Down
2 changes: 0 additions & 2 deletions src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@ export const UNABLE_TO_LOAD_PRETTIER =
export const RESTART_TO_ENABLE =
"To enable or disable prettier after changing the `enable` setting, you must restart VS Code.";
export const USING_BUNDLED_PRETTIER = "Using bundled version of prettier.";
export const OUTDATED_PRETTIER_INSTALLED =
"Outdated version of prettier installed. You must upgrade in order to use the Prettier extension.";
export const EXTENSION_DISABLED =
"Extension is disabled. No formatters will be registered. To enable, change the `prettier.enable` to `true` and restart VS Code.";
9 changes: 3 additions & 6 deletions src/test/suite/ModuleResolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import * as sinon from "sinon";
import { getWorkspaceFolderUri } from "./format.test";
import { ModuleResolver } from "../../ModuleResolver";
import { LoggingService } from "../../LoggingService";
import { NotificationService } from "../../NotificationService";
import {
OUTDATED_PRETTIER_INSTALLED,
OUTDATED_PRETTIER_VERSION_MESSAGE,
USING_BUNDLED_PRETTIER,
} from "../../message";

Expand All @@ -21,9 +20,7 @@ suite("Test ModuleResolver", function () {
const loggingService = new LoggingService();
logErrorSpy = sinon.spy(loggingService, "logError");
logDebugSpy = sinon.spy(loggingService, "logDebug");
const notificationService = new NotificationService(loggingService);

moduleResolver = new ModuleResolver(loggingService, notificationService);
moduleResolver = new ModuleResolver(loggingService);
});

suite("getPrettierInstance", () => {
Expand All @@ -50,7 +47,7 @@ suite("Test ModuleResolver", function () {
);

assert.strictEqual(prettierInstance, undefined);
assert(logErrorSpy.calledWith(OUTDATED_PRETTIER_INSTALLED));
assert(logErrorSpy.calledWith(OUTDATED_PRETTIER_VERSION_MESSAGE));
});

test("it returns prettier version from package.json", async () => {
Expand Down
Loading

0 comments on commit 899e952

Please sign in to comment.