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(50750): Object type literal with string literal property in contextual typing position causes language service error on all literal type references #50757

Merged
merged 2 commits into from Oct 3, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/services/utilities.ts
Expand Up @@ -790,6 +790,8 @@ namespace ts {
}

export function getContextualTypeFromParentOrAncestorTypeNode(node: Expression, checker: TypeChecker): Type | undefined {
if (node.flags & (NodeFlags.JSDoc & ~NodeFlags.JavaScriptFile)) return undefined;
a-tarasyuk marked this conversation as resolved.
Show resolved Hide resolved

const contextualType = getContextualTypeFromParent(node, checker);
if (contextualType) return contextualType;

Expand Down
@@ -0,0 +1,52 @@
// === /a.ts ===
// interface Foo {
// property: /*FIND ALL REFS*/"[|foo|]";
// }
// /**
// * @type {{ property: "foo"}}
// */
// const obj: Foo = {
// property: "[|foo|]",
// }

[
{
"definition": {
"containerKind": "",
"containerName": "",
"fileName": "/a.ts",
"kind": "var",
"name": "foo",
"textSpan": {
"start": 31,
"length": 3
},
"displayParts": [
{
"text": "\"foo\"",
"kind": "stringLiteral"
}
]
},
"references": [
{
"textSpan": {
"start": 31,
"length": 3
},
"fileName": "/a.ts",
"isWriteAccess": false,
"isInString": true
},
{
"textSpan": {
"start": 111,
"length": 3
},
"fileName": "/a.ts",
"isWriteAccess": false,
"isInString": true
}
]
}
]
11 changes: 11 additions & 0 deletions tests/baselines/reference/renameForStringLiteral.baseline
@@ -0,0 +1,11 @@
/*====== /a.ts ======*/

interface Foo {
property: "RENAME";
}
/**
* @type {{ property: "foo"}}
*/
const obj: Foo = {
property: "RENAME",
}
14 changes: 14 additions & 0 deletions tests/cases/fourslash/findAllRefsForStringLiteral.ts
@@ -0,0 +1,14 @@
/// <reference path="fourslash.ts" />

// @filename: /a.ts
////interface Foo {
//// property: /**/"foo";
////}
/////**
//// * @type {{ property: "foo"}}
//// */
////const obj: Foo = {
//// property: "foo",
////}

verify.baselineFindAllReferences("");
14 changes: 14 additions & 0 deletions tests/cases/fourslash/renameForStringLiteral.ts
@@ -0,0 +1,14 @@
/// <reference path="fourslash.ts" />

// @filename: /a.ts
////interface Foo {
//// property: /**/"foo";
////}
/////**
//// * @type {{ property: "foo"}}
//// */
////const obj: Foo = {
//// property: "foo",
////}

verify.baselineRename("", {});