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(51225): Go-to-definition on case or default should jump to the containing switch statement if available. #51236

Merged
merged 12 commits into from
Jan 11, 2024

Conversation

sviat9440
Copy link
Contributor

Fixes #51225

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Oct 20, 2022
@sviat9440
Copy link
Contributor Author

@microsoft-github-policy-service agree

@sviat9440 sviat9440 force-pushed the fix/51225 branch 2 times, most recently from 1613057 to 7b3a71a Compare October 20, 2022 19:10
@DanielRosenwasser
Copy link
Member

Can you add one more test?

switch (null) {
    case null:
        export /*start*/default 123;
}

Validate that it doesn't jump to the switch statement.

@sviat9440
Copy link
Contributor Author

sviat9440 commented Oct 20, 2022

/////*c*/export default { [|/*a*/case|] };
////[|/*b*/default|];

Is it normal that b and goes to c? This is how it works now and looks like a bug. It is expected that there will be no go to anywhere.

@DanielRosenwasser
Copy link
Member

It's questionable, but I wouldn't get hung up on fixing that specific case.

Copy link
Member

@sheetalkamat sheetalkamat left a comment

Choose a reason for hiding this comment

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

This looks good but the tests dont verify contextSpan is coming out correct.

Also should context be switch keyword plus expression or just expression.. Probably expression is fine but i am not sure if editor works out ok if context span is mutually exclusive with definition span.. (dont think we have had that)

@sviat9440
Copy link
Contributor Author

sviat9440 commented Nov 24, 2022

@sheetalkamat I changed contextSpan to keyword + expression, but i can't find any examples for testing contextSpan with fourslash

@DanielRosenwasser
Copy link
Member

Good to go @sheetalkamat?

@sviat9440
Copy link
Contributor Author

Any progress? @sheetalkamat

@sheetalkamat
Copy link
Member

@typescript-bot pack this

@sheetalkamat
Copy link
Member

I am working on framework to test contextSpan with goto def which we should merge and then rebase this PR so its clearer on what is the context that is being set for contextSpan. Will link PR here once i have it ready

@sheetalkamat
Copy link
Member

@sviat9440 #52576 is up and once that is merged you should be able to update your PR with the baselines that verify context,
Having said that, i completely mis-read context Span to be first line of declaration. It is infact the complete declaration. With that in mind i think your earlier change of having contextSpan as complete switch statement was correct and you would want to go back to that behavior. Sorry for misdirecting you on that one.

@sheetalkamat
Copy link
Member

#52576 is merged. Feel free to merge changes from main and baseline

@@ -0,0 +1,17 @@
// === goToDefinition ===
// === /tests/cases/fourslash/goToDefinitionSwitchCase1.ts ===
// [|{| contextId: 0 |}switch|] (<|null|>) {
Copy link
Member

Choose a reason for hiding this comment

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

i think contextSpan should be switch (null) instead of just null

Copy link
Member

Choose a reason for hiding this comment

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

Was the previous commit correct then?

Copy link
Member

Choose a reason for hiding this comment

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

kind of.. probably needs closing paren as well part of context ?

name,
containerKind: undefined!,
containerName: "",
contextSpan: createTextSpanFromBounds(keyword.getStart(sourceFile), statement.caseBlock.getFullStart()),
Copy link
Member

Choose a reason for hiding this comment

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

This needs to be moved to getContextNode which handled SwitchStatementthen ...FindAllReferences.getContextSpan() so that all places where we will use context span are in single place.

Copy link
Member

Choose a reason for hiding this comment

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

Looking at createDefinitionInfoFromName above, it looks like the right way to use this is first to call getContextNode to get the start/end tokens, then to use toContextSpan to turn it into a context span (see code below). Unfortunately, my getFullStart hack (which grabs the closing paren) gets lost since getContextNode returns a node pair, not a location pair. And toContextSpan currently always calls getEnd, but my hack uses getFullStart.

    const keyword = FindAllReferences.getContextNode(statement)!;
    const textSpan = createTextSpanFromNode(isContextWithStartAndEndNode(keyword) ? keyword.start : keyword, sourceFile)
    return {
        fileName: sourceFile.fileName,
        textSpan,
        kind: ScriptElementKind.keyword,
        name: "switch",
        containerKind: undefined!,
        containerName: "",
        ...FindAllReferences.toContextSpan(textSpan, sourceFile, keyword),
        isLocal: true,
        isAmbient: false,
        unverified: false,
        failedAliasResolution: undefined,
    };

Any ideas? I'm going to try hacking toContextSpan to special case switches to see how bad that is.

Copy link
Member

Choose a reason for hiding this comment

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

OK, the hack isn't too bad; there's a special case for strin gliterals there already, so I added it after that.

@sandersn sandersn merged commit 1717826 into microsoft:main Jan 11, 2024
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Go-to-definition on case or default should jump to the containing switch statement if available.
6 participants