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

Emit correct occurrences for destructured objects #185

Merged
merged 2 commits into from
Oct 19, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions snapshots/input/syntax/src/destructuring.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
interface Props {
a: number
}
const props: Props = { a: 42 }

export function objectDestructuring(): number[] {
olafurpg marked this conversation as resolved.
Show resolved Hide resolved
const { a: b } = props
return [props].map(({ a }) => a + b)
}

export function arrayDestructuring(): number[] {
const [b] = [props]
return [[b]].map(([a]) => a.a)
}

export function nestedDestructuring(): number[] {
const [[b]] = [[props]]
return [[props]].map(([{ a }]) => a + b.a)
}

export function forLoopObjectDestructuring(): number {
for (const { a } of [props]) {
return a
}
return 1
}

export function forLoopArrayDestructuring(): number {
for (const [{ a }] of [[props]]) {
return a
}
return 1
}

export function parameterDestructuring({ a }: Props): number {
return a
}
14 changes: 14 additions & 0 deletions snapshots/input/syntax/src/structural-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function foo(): Promise<{ member: number }> {
return Promise.resolve({ member: 42 })
}
Comment on lines +1 to +3
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens when there is a mapped type (https://www.typescriptlang.org/docs/handbook/2/mapped-types.html) involved as part of the definition? A mapped type can produce new object types, so the keys aren't visible in the source code, they're implicit as part of some logic.

E.g. if you have:

type OptionsFlags<Type> = {
  [Property in keyof Type]: boolean;
};

type FeatureFlags = {
  darkMode: () => void;
};
 
type FeatureOptions = OptionsFlags<FeatureFlags>;
// implicitly 
// type FeatureOptions = {
//    darkMode: boolean;
// }

const fo: FeatureOptions = { darkMode: true };
                          // ^ go to def

Will go-to-def on darkMode in the object literal take you to FeatureOptions? Will it take you to FeatureFlags? Nowhere?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice one. It doesn't work. I added a snapshot test and opened an issue to follow up on this #187

export function bar(): Promise<number> {
return foo().then(x => x.member)
}
export function bar2(): Promise<number> {
return foo().then(({ member }) => member)
}

type OptionsFlags<Type> = { [Property in keyof Type]: boolean }
type FeatureFlags = { darkMode: () => void }
export type FeatureOptions = OptionsFlags<FeatureFlags> // implicitly // type FeatureOptions = { // darkMode: boolean; // } const fo: FeatureOptions = { darkMode: true }; // ^ go to def
export const fo: FeatureOptions = { darkMode: true }
5 changes: 5 additions & 0 deletions snapshots/input/syntax/src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare var window: Window & typeof globalThis
interface Window {
process: any
require: any
}
3 changes: 3 additions & 0 deletions snapshots/input/syntax/src/typings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function process() {
return window.process
}
17 changes: 11 additions & 6 deletions snapshots/output/react/src/LoaderInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@
// ^^^^^^^^^^^^^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/FunctionComponent#
// ^^^^^ reference react-example 1.0.0 src/`LoaderInput.tsx`/Props#
loading,
// ^^^^^^^ reference local 3
// ^^^^^^^ definition local 3
// documentation ```ts\n(parameter) loading: boolean\n```
// ^^^^^^^ reference react-example 1.0.0 src/`LoaderInput.tsx`/Props#loading.
children,
// ^^^^^^^^ reference local 4
// ^^^^^^^^ definition local 4
// documentation ```ts\n(parameter) children: ReactNode\n```
// ^^^^^^^^ reference react-example 1.0.0 src/`LoaderInput.tsx`/Props#children.
// ^^^^^^^^ reference local 7
Copy link
Contributor

@varungandhi-src varungandhi-src Oct 19, 2022

Choose a reason for hiding this comment

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

Why is there an extra reference local here? The loading field doesn't have this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, opened #188

}) => (
<div className="hello">
// ^^^ reference @types/react 17.0.0 `index.d.ts`/global/JSX/IntrinsicElements#div.
Expand All @@ -49,15 +54,15 @@
// ^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/
// ^^^^^^^^^^^^^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/FunctionComponent#
// ^^^^^ reference react-example 1.0.0 src/`LoaderInput.tsx`/Props#
// ^^^^^ definition local 6
// ^^^^^ definition local 9
// documentation ```ts\n(parameter) props: PropsWithChildren<Props>\n```
return <LoaderInput loading={true} key="key" children={props.children} />
// ^^^^^^^^^^^ reference react-example 1.0.0 src/`LoaderInput.tsx`/LoaderInput.
// ^^^^^^^ reference react-example 1.0.0 src/`LoaderInput.tsx`/Props#loading.
// ^^^ reference local 10
// ^^^ reference local 13
// ^^^^^^^^ reference react-example 1.0.0 src/`LoaderInput.tsx`/Props#children.
// ^^^^^ reference local 6
// ^^^^^ reference local 9
// ^^^^^^^^ reference react-example 1.0.0 src/`LoaderInput.tsx`/Props#children.
// ^^^^^^^^ reference local 13
// ^^^^^^^^ reference local 7
}

108 changes: 108 additions & 0 deletions snapshots/output/syntax/src/destructuring.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
interface Props {
// definition syntax 1.0.0 src/`destructuring.ts`/
//documentation ```ts\nmodule "destructuring.ts"\n```
// ^^^^^ definition syntax 1.0.0 src/`destructuring.ts`/Props#
// documentation ```ts\ninterface Props\n```
a: number
// ^ definition syntax 1.0.0 src/`destructuring.ts`/Props#a.
// documentation ```ts\n(property) a: number\n```
}
const props: Props = { a: 42 }
// ^^^^^ definition syntax 1.0.0 src/`destructuring.ts`/props.
// documentation ```ts\nvar props: Props\n```
// ^^^^^ reference syntax 1.0.0 src/`destructuring.ts`/Props#
// ^ definition syntax 1.0.0 src/`destructuring.ts`/a0:
// documentation ```ts\n(property) a: number\n```
// relationship implementation reference scip-typescript npm syntax 1.0.0 src/`destructuring.ts`/Props#a.

export function objectDestructuring(): number[] {
// ^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`destructuring.ts`/objectDestructuring().
// documentation ```ts\nfunction objectDestructuring(): number[]\n```
const { a: b } = props
// ^ reference syntax 1.0.0 src/`destructuring.ts`/Props#a.
// ^ definition local 4
// documentation ```ts\nvar b: number\n```
// ^^^^^ reference syntax 1.0.0 src/`destructuring.ts`/props.
return [props].map(({ a }) => a + b)
// ^^^^^ reference syntax 1.0.0 src/`destructuring.ts`/props.
// ^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Array#map().
// ^ definition local 10
// documentation ```ts\n(parameter) a: number\n```
// ^ reference syntax 1.0.0 src/`destructuring.ts`/Props#a.
// ^ reference local 10
// ^ reference local 4
}

export function arrayDestructuring(): number[] {
// ^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`destructuring.ts`/arrayDestructuring().
// documentation ```ts\nfunction arrayDestructuring(): number[]\n```
const [b] = [props]
// ^ definition local 15
// documentation ```ts\nvar b: Props\n```
// ^^^^^ reference syntax 1.0.0 src/`destructuring.ts`/props.
return [[b]].map(([a]) => a.a)
// ^ reference local 15
// ^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Array#map().
// ^ definition local 21
// documentation ```ts\n(parameter) a: Props\n```
// ^ reference local 21
// ^ reference syntax 1.0.0 src/`destructuring.ts`/Props#a.
}

export function nestedDestructuring(): number[] {
// ^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`destructuring.ts`/nestedDestructuring().
// documentation ```ts\nfunction nestedDestructuring(): number[]\n```
const [[b]] = [[props]]
// ^ definition local 28
// documentation ```ts\nvar b: Props\n```
// ^^^^^ reference syntax 1.0.0 src/`destructuring.ts`/props.
return [[props]].map(([{ a }]) => a + b.a)
// ^^^^^ reference syntax 1.0.0 src/`destructuring.ts`/props.
// ^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Array#map().
// ^ definition local 36
// documentation ```ts\n(parameter) a: number\n```
// ^ reference syntax 1.0.0 src/`destructuring.ts`/Props#a.
// ^ reference local 36
// ^ reference local 28
// ^ reference syntax 1.0.0 src/`destructuring.ts`/Props#a.
}

export function forLoopObjectDestructuring(): number {
// ^^^^^^^^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`destructuring.ts`/forLoopObjectDestructuring().
// documentation ```ts\nfunction forLoopObjectDestructuring(): number\n```
for (const { a } of [props]) {
// ^ definition local 41
// documentation ```ts\nvar a: number\n```
Copy link
Contributor

Choose a reason for hiding this comment

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

This being a var and not a const/let is a bug, right? Filed #186

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like a bug indeed, thank you for opening that

// ^ reference syntax 1.0.0 src/`destructuring.ts`/Props#a.
// ^^^^^ reference syntax 1.0.0 src/`destructuring.ts`/props.
return a
// ^ reference local 41
}
return 1
}

export function forLoopArrayDestructuring(): number {
// ^^^^^^^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`destructuring.ts`/forLoopArrayDestructuring().
// documentation ```ts\nfunction forLoopArrayDestructuring(): number\n```
for (const [{ a }] of [[props]]) {
// ^ definition local 48
// documentation ```ts\nvar a: number\n```
// ^ reference syntax 1.0.0 src/`destructuring.ts`/Props#a.
// ^^^^^ reference syntax 1.0.0 src/`destructuring.ts`/props.
return a
// ^ reference local 48
}
return 1
}

export function parameterDestructuring({ a }: Props): number {
// ^^^^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`destructuring.ts`/parameterDestructuring().
// documentation ```ts\nfunction parameterDestructuring({ a }: Props): number\n```
// ^ definition local 50
// documentation ```ts\n(parameter) a: number\n```
// ^ reference syntax 1.0.0 src/`destructuring.ts`/Props#a.
// ^^^^^ reference syntax 1.0.0 src/`destructuring.ts`/Props#
return a
// ^ reference local 50
}

81 changes: 81 additions & 0 deletions snapshots/output/syntax/src/structural-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
export function foo(): Promise<{ member: number }> {
// definition syntax 1.0.0 src/`structural-type.ts`/
//documentation ```ts\nmodule "structural-type.ts"\n```
// ^^^ definition syntax 1.0.0 src/`structural-type.ts`/foo().
// documentation ```ts\nfunction foo(): Promise<{ member: number; }>\n```
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Promise#
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.iterable.d.ts`/Promise#
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.promise.d.ts`/Promise.
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.symbol.wellknown.d.ts`/Promise#
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2018.promise.d.ts`/Promise#
// ^^^^^^ definition syntax 1.0.0 src/`structural-type.ts`/foo().Promise:typeLiteral0:member.
// documentation ```ts\n(property) member: number\n```
return Promise.resolve({ member: 42 })
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Promise#
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.iterable.d.ts`/Promise#
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.promise.d.ts`/Promise.
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.symbol.wellknown.d.ts`/Promise#
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2018.promise.d.ts`/Promise#
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.promise.d.ts`/PromiseConstructor#resolve().
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.promise.d.ts`/PromiseConstructor#resolve().
// ^^^^^^ definition syntax 1.0.0 src/`structural-type.ts`/member0:
Comment on lines +11 to +21
Copy link
Contributor

Choose a reason for hiding this comment

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

Are these two symbols deliberately different/should they be the same or have a reference relationship?

Copy link
Member Author

Choose a reason for hiding this comment

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

Go to definition doesn't with tsserver for this case. I opened #189 because I think we can make it work anyways, and it would be cool to do better than tsserver

// documentation ```ts\n(property) member: number\n```
}
export function bar(): Promise<number> {
// ^^^ definition syntax 1.0.0 src/`structural-type.ts`/bar().
// documentation ```ts\nfunction bar(): Promise<number>\n```
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Promise#
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.iterable.d.ts`/Promise#
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.promise.d.ts`/Promise.
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.symbol.wellknown.d.ts`/Promise#
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2018.promise.d.ts`/Promise#
return foo().then(x => x.member)
// ^^^ reference syntax 1.0.0 src/`structural-type.ts`/foo().
// ^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Promise#then().
// ^ definition local 4
// documentation ```ts\n(parameter) x: { member: number; }\n```
// ^ reference local 4
// ^^^^^^ reference syntax 1.0.0 src/`structural-type.ts`/foo().Promise:typeLiteral0:member.
}
export function bar2(): Promise<number> {
// ^^^^ definition syntax 1.0.0 src/`structural-type.ts`/bar2().
// documentation ```ts\nfunction bar2(): Promise<number>\n```
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Promise#
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.iterable.d.ts`/Promise#
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.promise.d.ts`/Promise.
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.symbol.wellknown.d.ts`/Promise#
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2018.promise.d.ts`/Promise#
return foo().then(({ member }) => member)
// ^^^ reference syntax 1.0.0 src/`structural-type.ts`/foo().
// ^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Promise#then().
// ^^^^^^ definition local 10
// documentation ```ts\n(parameter) member: number\n```
// ^^^^^^ reference syntax 1.0.0 src/`structural-type.ts`/foo().Promise:typeLiteral0:member.
// ^^^^^^ reference local 10
}

type OptionsFlags<Type> = { [Property in keyof Type]: boolean }
// ^^^^^^^^^^^^ definition syntax 1.0.0 src/`structural-type.ts`/OptionsFlags#
// documentation ```ts\ntype OptionsFlags\n```
// ^^^^ definition syntax 1.0.0 src/`structural-type.ts`/OptionsFlags#[Type]
// documentation ```ts\nType: Type\n```
// ^^^^^^^^ definition local 12
// documentation ```ts\nProperty: Property\n```
// ^^^^ reference syntax 1.0.0 src/`structural-type.ts`/OptionsFlags#[Type]
type FeatureFlags = { darkMode: () => void }
// ^^^^^^^^^^^^ definition syntax 1.0.0 src/`structural-type.ts`/FeatureFlags#
// documentation ```ts\ntype FeatureFlags\n```
// ^^^^^^^^ definition syntax 1.0.0 src/`structural-type.ts`/FeatureFlags#typeLiteral13:darkMode.
// documentation ```ts\n(property) darkMode: () => void\n```
export type FeatureOptions = OptionsFlags<FeatureFlags> // implicitly // type FeatureOptions = { // darkMode: boolean; // } const fo: FeatureOptions = { darkMode: true }; // ^ go to def
// ^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`structural-type.ts`/FeatureOptions#
// documentation ```ts\ntype FeatureOptions\n```
// ^^^^^^^^^^^^ reference syntax 1.0.0 src/`structural-type.ts`/OptionsFlags#
// ^^^^^^^^^^^^ reference syntax 1.0.0 src/`structural-type.ts`/FeatureFlags#
export const fo: FeatureOptions = { darkMode: true }
// ^^ definition syntax 1.0.0 src/`structural-type.ts`/fo.
// documentation ```ts\nvar fo: OptionsFlags<FeatureFlags>\n```
// ^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`structural-type.ts`/FeatureOptions#
// ^^^^^^^^ definition syntax 1.0.0 src/`structural-type.ts`/darkMode0:
// documentation ```ts\n(property) darkMode: true\n```

11 changes: 11 additions & 0 deletions snapshots/output/syntax/src/typings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function process() {
// definition syntax 1.0.0 src/`typings.ts`/
//documentation ```ts\nmodule "typings.ts"\n```
// ^^^^^^^ definition syntax 1.0.0 src/`typings.ts`/process().
// documentation ```ts\nfunction process(): Process\n```
return window.process
// ^^^^^^ reference typescript 4.8.4 lib/`lib.dom.d.ts`/window.
// ^^^^^^^ reference @types/node 17.0.14 `globals.d.ts`/process.
// ^^^^^^^ reference @types/node 17.0.14 `process.d.ts`/`'process'`/global/process.
}

Loading