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 default Anchor param of context menu #10540

Closed
wants to merge 1 commit into from

Conversation

shuyaqian
Copy link
Contributor

Signed-off-by: shuyaqian 717749594@qq.com

What it does

Fix default Anchor param of context menu.

How to test

  1. click 'Start Debugging' menu
    image
  2. you will get error
    image
  3. because the param debugSessionOptions received is an anchor
    image

Review checklist

Reminder for reviewers

@colin-grant-work colin-grant-work self-requested a review December 16, 2021 15:45
@colin-grant-work
Copy link
Contributor

colin-grant-work commented Dec 16, 2021

A similar change has been proposed previously, but it was rejected because there was code that relied on the current behavior. I believe that this recent change should reduce our dependence on that behavior upstream, but there may be quite a lot of downstream code that still relies on it.

Given that, there are other possibilities for working around this problem in the current context.

  • Modify the handler for the command that is failing to account for the possibility of an anchor as the first argument.
  • Modify the code that opens that menu to explicitly pass undefined as the first argument (or first several arguments)

@shuyaqian
Copy link
Contributor Author

A similar change has been proposed previously, but it was rejected because there was code that relied on the current behavior. I believe that this recent change should reduce our dependence on that behavior upstream, but there may be quite a lot of downstream code that still relies on it.

Given that, there are other possibilities for working around this problem in the current context.

  • Modify the handler for the command that is failing to account for the possibility of an anchor as the first argument.
  • Modify the code that opens that menu to explicitly pass undefined as the first argument (or first several arguments)

I defined the isAnchor function, and verify the first argument.

Comment on lines 32 to 33
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isAnchor(arg: any): boolean {
if (arg && 'x' in arg && 'y' in arg && Object.keys(arg).length === 2) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: We try to limit our any usage in the framework.

Suggested change
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isAnchor(arg: any): boolean {
if (arg && 'x' in arg && 'y' in arg && Object.keys(arg).length === 2) {
export function isAnchor(arg: unknown): boolean {
if (typeof arg === 'object' && arg && 'x' in arg && 'y' in arg && Object.keys(arg).length === 2) {

@@ -1197,7 +1197,7 @@ export class DebugFrontendApplicationContribution extends AbstractViewContributi
}

async start(noDebug?: boolean, debugSessionOptions?: DebugSessionOptions): Promise<void> {
let current = debugSessionOptions ? debugSessionOptions : this.configurations.current;
let current = debugSessionOptions && !isAnchor(debugSessionOptions) ? debugSessionOptions : this.configurations.current;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to check this condition in the command handler (around line 640), since the possibility of receiving of anchor is limited to that context.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to check this condition in the command handler (around line 640), since the possibility of receiving of anchor is limited to that context.

ok, done

Comment on lines 32 to 37
export function isAnchor(arg?: Anchor): boolean {
if (arg && 'x' in arg && 'y' in arg && Object.keys(arg).length === 2) {
return true;
}
return false;
}
Copy link
Contributor

@colin-grant-work colin-grant-work Dec 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct and sufficient? The Anchor type also includes MouseEvents (l. 26).

export interface Coordinate { x: number; y: number; }
export const Coordinate = Symbol('Coordinate');
export type Anchor = MouseEvent | Coordinate;

That also seems to be the error that is killing the build.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the type to unknown.

Signed-off-by: shuyaqian 717749594@qq.com
@colin-grant-work colin-grant-work self-requested a review December 22, 2021 14:47
@colin-grant-work colin-grant-work added commands issues related to application commands menus issues related to the menu labels Dec 22, 2021
Comment on lines +643 to +646
execute: (config?: DebugSessionOptions) => this.start(false, isAnchor(config) ? undefined : config)
});
registry.registerCommand(DebugCommands.START_NO_DEBUG, {
execute: (config?: DebugSessionOptions) => this.start(true, config)
execute: (config?: DebugSessionOptions) => this.start(true, isAnchor(config) ? undefined : config)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to change my mind here, but even though the cause of the problem is that an Anchor is being passed in, but what we really want to guarantee is that the config is an instance of DebugSessionOptions - the problem with the CommandHandler system is that we're allowed to say config?: DebugSessionOption, but in fact anything can be passed in - Anchors, or anything else..

So rather than adding a check for whether this is an Anchor, it would be more correct in this context to implement a check that something is a DebugSessionOptions. Something like.

export namespace DebugSessionOptions {
    export function is(candidate: unknown): candidate is DebugSessionOptions {
        const options = candidate as DebugSessionOptions;
        return typeof options === 'object' && !!candidate && 'configuration' in candidate && DebugConfiguration.is(options.candidate);
    }
}

Again, sorry for the change of direction!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say hold off on this pending a general resolution of the problems introduced by window.menuBarVisibility compact. As I say here, the correct, non-breaking solution is probably just to fix the behavior of the menu introduced by that PR, rather than fix the commands one by one or change the existing behavior of real context menus.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
commands issues related to application commands menus issues related to the menu
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants