diff --git a/src/services/services.ts b/src/services/services.ts index a634b2baf1a..731db036289 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -5615,6 +5615,21 @@ namespace ts { return (symbol.flags & SymbolFlags.Alias) && !!getDeclarationOfKind(symbol, SyntaxKind.ImportSpecifier); } + function isObjectBindingPatternElementWithoutPropertyName(symbol: Symbol) { + const bindingElement = getDeclarationOfKind(symbol, SyntaxKind.BindingElement); + return bindingElement && + bindingElement.parent.kind === SyntaxKind.ObjectBindingPattern && + !bindingElement.propertyName; + } + + function getPropertySymbolIfObjectBindingPatternWithoutPropertyName(symbol: Symbol) { + if (isObjectBindingPatternElementWithoutPropertyName(symbol)) { + const bindingElement = getDeclarationOfKind(symbol, SyntaxKind.BindingElement); + const typeOfPattern = typeChecker.getTypeAtLocation(bindingElement.parent); + return typeOfPattern && typeChecker.getPropertyOfType(typeOfPattern, (bindingElement.name).text); + } + } + function getInternedName(symbol: Symbol, location: Node, declarations: Declaration[]): string { // If this is an export or import specifier it could have been renamed using the 'as' syntax. // If so we want to search for whatever under the cursor. @@ -5660,6 +5675,12 @@ namespace ts { return undefined; } + // If symbol is of object binding pattern element without property name we would want to + // look for property too and that could be anywhere + if (isObjectBindingPatternElementWithoutPropertyName(symbol)) { + return undefined; + } + // if this symbol is visible from its parent container, e.g. exported, then bail out // if symbol correspond to the union property - bail out if (symbol.parent || (symbol.flags & SymbolFlags.SyntheticProperty)) { @@ -6103,6 +6124,13 @@ namespace ts { result = result.concat(typeChecker.getSymbolsOfParameterPropertyDeclaration(symbol.valueDeclaration, symbol.name)); } + // If this is symbol of binding element without propertyName declaration in Object binding pattern + // Include the property in the search + const bindingElementPropertySymbol = getPropertySymbolIfObjectBindingPatternWithoutPropertyName(symbol); + if (bindingElementPropertySymbol) { + result.push(bindingElementPropertySymbol); + } + // If this is a union property, add all the symbols from all its source symbols in all unioned types. // If the symbol is an instantiation from a another symbol (e.g. widened symbol) , add the root the list forEach(typeChecker.getRootSymbols(symbol), rootSymbol => { @@ -6212,6 +6240,14 @@ namespace ts { }); } + // If the reference location is the binding element and doesn't have property name + // then include the binding element in the related symbols + // let { a } : { a }; + const bindingElementPropertySymbol = getPropertySymbolIfObjectBindingPatternWithoutPropertyName(referenceSymbol); + if (bindingElementPropertySymbol && searchSymbols.indexOf(bindingElementPropertySymbol) >= 0) { + return bindingElementPropertySymbol; + } + // Unwrap symbols to get to the root (e.g. transient symbols as a result of widening) // Or a union property, use its underlying unioned symbols return forEach(typeChecker.getRootSymbols(referenceSymbol), rootSymbol => { diff --git a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName03.ts b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName03.ts index 304fa9e42e9..8dc2b1e7bb8 100644 --- a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName03.ts +++ b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName03.ts @@ -6,7 +6,7 @@ ////} //// ////var foo: I; -////var [{ [|property1|]: prop1 }, { property1, property2 } ] = [foo, foo]; +////var [{ [|property1|]: prop1 }, { [|property1|], property2 } ] = [foo, foo]; let ranges = test.ranges(); for (let range of ranges) { diff --git a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName04.ts b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName04.ts index bdb37525f71..dfa0997774e 100644 --- a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName04.ts +++ b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName04.ts @@ -6,14 +6,12 @@ ////} //// ////function f({ /**/[|property1|]: p1 }: I, -//// { /*SHOULD_BE_A_REFERENCE*/property1 }: I, +//// { [|property1|] }: I, //// { property1: p2 }) { //// -//// return property1 + 1; +//// return [|property1|] + 1; ////} -// NOTE: In the future, the identifier at -// SHOULD_BE_A_REFERENCE should be in the set of ranges. goTo.marker(); let ranges = test.ranges(); diff --git a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName06.ts b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName06.ts index 67c7029861e..d416b22027b 100644 --- a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName06.ts +++ b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName06.ts @@ -8,12 +8,12 @@ ////var elems: I[]; ////for (let { [|property1|]: p } of elems) { ////} -////for (let { property1 } of elems) { +////for (let { [|property1|] } of elems) { ////} ////for (var { [|property1|]: p1 } of elems) { ////} ////var p2; -////for ({ property1 : p2 } of elems) { +////for ({ /*This should be referenced too*/property1 : p2 } of elems) { ////} // Note: if this test ever changes, consider updating @@ -23,6 +23,7 @@ let ranges = test.ranges(); for (let range of ranges) { goTo.position(range.start); + debugger; verify.referencesCountIs(ranges.length); for (let expectedRange of ranges) { verify.referencesAtPositionContains(expectedRange); diff --git a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName09.ts b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName09.ts index e45359bcf1c..0b82c73e31d 100644 --- a/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName09.ts +++ b/tests/cases/fourslash/findAllRefsObjectBindingElementPropertyName09.ts @@ -1,19 +1,17 @@ /// ////interface I { -//// /*SHOULD_BE_A_REFERENCE1*/property1: number; +//// [|property1|]: number; //// property2: string; ////} //// -////function f({ /*SHOULD_BE_A_REFERENCE2*/property1: p1 }: I, +////function f({ [|property1|]: p1 }: I, //// { /**/[|property1|] }: I, //// { property1: p2 }) { //// //// return [|property1|] + 1; ////} -// NOTE: In the future, the identifiers at -// SHOULD_BE_A_REFERENCE[1/2] should be in the set of ranges. goTo.marker(); let ranges = test.ranges(); diff --git a/tests/cases/fourslash/renameDestructuringClassProperty.ts b/tests/cases/fourslash/renameDestructuringClassProperty.ts new file mode 100644 index 00000000000..acc58f999c4 --- /dev/null +++ b/tests/cases/fourslash/renameDestructuringClassProperty.ts @@ -0,0 +1,23 @@ +/// + +////class A { +//// [|foo|]: string; +////} +////class B { +//// syntax1(a: A): void { +//// let { [|foo|] } = a; +//// } +//// syntax2(a: A): void { +//// let { [|foo|]: foo } = a; +//// } +//// syntax11(a: A): void { +//// let { [|foo|] } = a; +//// [|foo|] = "newString"; +//// } +////} + +let ranges = test.ranges() +for (let range of ranges) { + goTo.position(range.start); + verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ false); +} diff --git a/tests/cases/fourslash/renameDestructuringFunctionParameter.ts b/tests/cases/fourslash/renameDestructuringFunctionParameter.ts new file mode 100644 index 00000000000..d1df0f58275 --- /dev/null +++ b/tests/cases/fourslash/renameDestructuringFunctionParameter.ts @@ -0,0 +1,10 @@ +/// + +////function f({[|a|]}: {[|a|]}) { +//// f({[|a|]}); +////} +let ranges = test.ranges(); +for (let range of ranges) { + goTo.position(range.start); + verify.renameLocations(/*findInStrings*/ false, /*findInComments*/ false); +}