Skip to content

[Master] fix 16407 - LS in binding element of object binding pattern #16534

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

Merged
merged 7 commits into from
Aug 11, 2017
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
22 changes: 22 additions & 0 deletions src/services/goToDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ namespace ts.GoToDefinition {
declaration => createDefinitionInfo(declaration, shorthandSymbolKind, shorthandSymbolName, shorthandContainerName));
}

// If the node is the name of a BindingElement within an ObjectBindingPattern instead of just returning the
// declaration the symbol (which is itself), we should try to get to the original type of the ObjectBindingPattern
// and return the property declaration for the referenced property.
// For example:
// import('./foo').then(({ b/*goto*/ar }) => undefined); => should get use to the declaration in file "./foo"
//
// function bar<T>(onfulfilled: (value: T) => void) { //....}
// interface Test {
// pr/*destination*/op1: number
// }
// bar<Test>(({pr/*goto*/op1})=>{});
if (isPropertyName(node) && isBindingElement(node.parent) && isObjectBindingPattern(node.parent.parent) &&
(node === (node.parent.propertyName || node.parent.name))) {
const type = typeChecker.getTypeAtLocation(node.parent.parent);
if (type) {
const propSymbols = getPropertySymbolsFromType(type, node);
if (propSymbols) {
return flatMap(propSymbols, propSymbol => getDefinitionFromSymbol(typeChecker, propSymbol, node));
}
}
}

// If the current location we want to find its definition is in an object literal, try to get the contextual type for the
// object literal, lookup the property symbol in the contextual type, and use this for goto-definition.
// For example
Expand Down
15 changes: 10 additions & 5 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2138,12 +2138,17 @@ namespace ts {
export function getPropertySymbolsFromContextualType(typeChecker: TypeChecker, node: ObjectLiteralElement): Symbol[] {
const objectLiteral = <ObjectLiteralExpression | JsxAttributes>node.parent;
const contextualType = typeChecker.getContextualType(objectLiteral);
const name = unescapeLeadingUnderscores(getTextOfPropertyName(node.name));
if (name && contextualType) {
return getPropertySymbolsFromType(contextualType, node.name);
}

/* @internal */
export function getPropertySymbolsFromType(type: Type, propName: PropertyName) {
const name = unescapeLeadingUnderscores(getTextOfPropertyName(propName));
if (name && type) {
const result: Symbol[] = [];
const symbol = contextualType.getProperty(name);
if (contextualType.flags & TypeFlags.Union) {
forEach((<UnionType>contextualType).types, t => {
const symbol = type.getProperty(name);
if (type.flags & TypeFlags.Union) {
forEach((<UnionType>type).types, t => {
const symbol = t.getProperty(name);
if (symbol) {
result.push(symbol);
Expand Down
13 changes: 13 additions & 0 deletions tests/cases/fourslash/findAllReferencesDynamicImport3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// <reference path='fourslash.ts' />

// @Filename: foo.ts
//// export function [|bar|]() { return "bar"; }

//// import('./foo').then(({ [|bar|] }) => undefined);

const [r0, r1] = test.ranges();
// This is because bindingElement at r1 are both name and value
verify.referencesOf(r0, [r1, r0, r1, r0]);
verify.referencesOf(r1, [r0, r1, r1, r0]);
verify.renameLocations(r0, [r0, r1]);
verify.renameLocations(r1, [r1, r0, r0, r1]);
8 changes: 8 additions & 0 deletions tests/cases/fourslash/goToDefinitionDynamicImport3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path='fourslash.ts' />

// @Filename: foo.ts
//// export function /*Destination*/bar() { return "bar"; }

//// import('./foo').then(({ ba/*1*/r }) => undefined);

verify.goToDefinition("1", "Destination");
8 changes: 8 additions & 0 deletions tests/cases/fourslash/goToDefinitionDynamicImport4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path='fourslash.ts' />

// @Filename: foo.ts
//// export function /*Destination*/bar() { return "bar"; }

//// import('./foo').then(({ ba/*1*/r }) => undefined);

verify.goToDefinition("1", "Destination");
12 changes: 12 additions & 0 deletions tests/cases/fourslash/gotoDefinitionInObjectBindingPattern1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/// <reference path='fourslash.ts' />

//// function bar<T>(onfulfilled: (value: T) => void) {
//// return undefined;
//// }

//// interface Test {
//// /*destination*/prop2: number
//// }
//// bar<Test>(({pr/*goto*/op2})=>{});

verify.goToDefinition("goto", "destination");
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// <reference path='fourslash.ts' />

//// var p0 = ({a/*1*/a}) => {console.log(aa)};
//// function f2({ a/*a1*/1, b/*b1*/1 }: { /*a1_dest*/a1: number, /*b1_dest*/b1: number } = { a1: 0, b1: 0 }) {}

verify.goToDefinition("1", []);
verify.goToDefinition("a1", "a1_dest");
verify.goToDefinition("b1", "b1_dest");