Skip to content

Commit e6e58cc

Browse files
authored
Emit correct occurrences for destructured objects (#185)
* Emit correct occurrenes for destructured objects Previously, scip-typescript didn't emit appropriate occurrences for `property` in the destructuring pattern below: ```ts interface Props { property: number } const prop = { property: 42 } const { property } = prop // ^^^^^^^^ before: was a local reference // now: local definition + global reference ``` The solution works similarly to how to handle shorthand property definitions. * Address review feedback
1 parent db1ca27 commit e6e58cc

File tree

9 files changed

+331
-24
lines changed

9 files changed

+331
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
interface Props {
2+
a: number
3+
}
4+
const props: Props = { a: 42 }
5+
6+
export function objectDestructuring(): number[] {
7+
const { a: b } = props
8+
return [props].map(({ a }) => a + b)
9+
}
10+
11+
export function arrayDestructuring(): number[] {
12+
const [b] = [props]
13+
return [[b]].map(([a]) => a.a)
14+
}
15+
16+
export function nestedDestructuring(): number[] {
17+
const [[b]] = [[props]]
18+
return [[props]].map(([{ a }]) => a + b.a)
19+
}
20+
21+
export function forLoopObjectDestructuring(): number {
22+
for (const { a } of [props]) {
23+
return a
24+
}
25+
return 1
26+
}
27+
28+
export function forLoopArrayDestructuring(): number {
29+
for (const [{ a }] of [[props]]) {
30+
return a
31+
}
32+
return 1
33+
}
34+
35+
export function parameterDestructuring({ a }: Props): number {
36+
return a
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function foo(): Promise<{ member: number }> {
2+
return Promise.resolve({ member: 42 })
3+
}
4+
export function bar(): Promise<number> {
5+
return foo().then(x => x.member)
6+
}
7+
export function bar2(): Promise<number> {
8+
return foo().then(({ member }) => member)
9+
}
10+
11+
type OptionsFlags<Type> = { [Property in keyof Type]: boolean }
12+
type FeatureFlags = { darkMode: () => void }
13+
export type FeatureOptions = OptionsFlags<FeatureFlags> // implicitly // type FeatureOptions = { // darkMode: boolean; // } const fo: FeatureOptions = { darkMode: true }; // ^ go to def
14+
export const fo: FeatureOptions = { darkMode: true }
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare var window: Window & typeof globalThis
2+
interface Window {
3+
process: any
4+
require: any
5+
}

snapshots/input/syntax/src/typings.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function process() {
2+
return window.process
3+
}

snapshots/output/react/src/LoaderInput.tsx

+11-6
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@
2626
// ^^^^^^^^^^^^^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/FunctionComponent#
2727
// ^^^^^ reference react-example 1.0.0 src/`LoaderInput.tsx`/Props#
2828
loading,
29-
// ^^^^^^^ reference local 3
29+
// ^^^^^^^ definition local 3
30+
// documentation ```ts\n(parameter) loading: boolean\n```
31+
// ^^^^^^^ reference react-example 1.0.0 src/`LoaderInput.tsx`/Props#loading.
3032
children,
31-
// ^^^^^^^^ reference local 4
33+
// ^^^^^^^^ definition local 4
34+
// documentation ```ts\n(parameter) children: ReactNode\n```
35+
// ^^^^^^^^ reference react-example 1.0.0 src/`LoaderInput.tsx`/Props#children.
36+
// ^^^^^^^^ reference local 7
3237
}) => (
3338
<div className="hello">
3439
// ^^^ reference @types/react 17.0.0 `index.d.ts`/global/JSX/IntrinsicElements#div.
@@ -49,15 +54,15 @@
4954
// ^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/
5055
// ^^^^^^^^^^^^^^^^^ reference @types/react 17.0.0 `index.d.ts`/React/FunctionComponent#
5156
// ^^^^^ reference react-example 1.0.0 src/`LoaderInput.tsx`/Props#
52-
// ^^^^^ definition local 6
57+
// ^^^^^ definition local 9
5358
// documentation ```ts\n(parameter) props: PropsWithChildren<Props>\n```
5459
return <LoaderInput loading={true} key="key" children={props.children} />
5560
// ^^^^^^^^^^^ reference react-example 1.0.0 src/`LoaderInput.tsx`/LoaderInput.
5661
// ^^^^^^^ reference react-example 1.0.0 src/`LoaderInput.tsx`/Props#loading.
57-
// ^^^ reference local 10
62+
// ^^^ reference local 13
5863
// ^^^^^^^^ reference react-example 1.0.0 src/`LoaderInput.tsx`/Props#children.
59-
// ^^^^^ reference local 6
64+
// ^^^^^ reference local 9
6065
// ^^^^^^^^ reference react-example 1.0.0 src/`LoaderInput.tsx`/Props#children.
61-
// ^^^^^^^^ reference local 13
66+
// ^^^^^^^^ reference local 7
6267
}
6368

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
interface Props {
2+
// definition syntax 1.0.0 src/`destructuring.ts`/
3+
//documentation ```ts\nmodule "destructuring.ts"\n```
4+
// ^^^^^ definition syntax 1.0.0 src/`destructuring.ts`/Props#
5+
// documentation ```ts\ninterface Props\n```
6+
a: number
7+
// ^ definition syntax 1.0.0 src/`destructuring.ts`/Props#a.
8+
// documentation ```ts\n(property) a: number\n```
9+
}
10+
const props: Props = { a: 42 }
11+
// ^^^^^ definition syntax 1.0.0 src/`destructuring.ts`/props.
12+
// documentation ```ts\nvar props: Props\n```
13+
// ^^^^^ reference syntax 1.0.0 src/`destructuring.ts`/Props#
14+
// ^ definition syntax 1.0.0 src/`destructuring.ts`/a0:
15+
// documentation ```ts\n(property) a: number\n```
16+
// relationship implementation reference scip-typescript npm syntax 1.0.0 src/`destructuring.ts`/Props#a.
17+
18+
export function objectDestructuring(): number[] {
19+
// ^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`destructuring.ts`/objectDestructuring().
20+
// documentation ```ts\nfunction objectDestructuring(): number[]\n```
21+
const { a: b } = props
22+
// ^ reference syntax 1.0.0 src/`destructuring.ts`/Props#a.
23+
// ^ definition local 4
24+
// documentation ```ts\nvar b: number\n```
25+
// ^^^^^ reference syntax 1.0.0 src/`destructuring.ts`/props.
26+
return [props].map(({ a }) => a + b)
27+
// ^^^^^ reference syntax 1.0.0 src/`destructuring.ts`/props.
28+
// ^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Array#map().
29+
// ^ definition local 10
30+
// documentation ```ts\n(parameter) a: number\n```
31+
// ^ reference syntax 1.0.0 src/`destructuring.ts`/Props#a.
32+
// ^ reference local 10
33+
// ^ reference local 4
34+
}
35+
36+
export function arrayDestructuring(): number[] {
37+
// ^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`destructuring.ts`/arrayDestructuring().
38+
// documentation ```ts\nfunction arrayDestructuring(): number[]\n```
39+
const [b] = [props]
40+
// ^ definition local 15
41+
// documentation ```ts\nvar b: Props\n```
42+
// ^^^^^ reference syntax 1.0.0 src/`destructuring.ts`/props.
43+
return [[b]].map(([a]) => a.a)
44+
// ^ reference local 15
45+
// ^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Array#map().
46+
// ^ definition local 21
47+
// documentation ```ts\n(parameter) a: Props\n```
48+
// ^ reference local 21
49+
// ^ reference syntax 1.0.0 src/`destructuring.ts`/Props#a.
50+
}
51+
52+
export function nestedDestructuring(): number[] {
53+
// ^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`destructuring.ts`/nestedDestructuring().
54+
// documentation ```ts\nfunction nestedDestructuring(): number[]\n```
55+
const [[b]] = [[props]]
56+
// ^ definition local 28
57+
// documentation ```ts\nvar b: Props\n```
58+
// ^^^^^ reference syntax 1.0.0 src/`destructuring.ts`/props.
59+
return [[props]].map(([{ a }]) => a + b.a)
60+
// ^^^^^ reference syntax 1.0.0 src/`destructuring.ts`/props.
61+
// ^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Array#map().
62+
// ^ definition local 36
63+
// documentation ```ts\n(parameter) a: number\n```
64+
// ^ reference syntax 1.0.0 src/`destructuring.ts`/Props#a.
65+
// ^ reference local 36
66+
// ^ reference local 28
67+
// ^ reference syntax 1.0.0 src/`destructuring.ts`/Props#a.
68+
}
69+
70+
export function forLoopObjectDestructuring(): number {
71+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`destructuring.ts`/forLoopObjectDestructuring().
72+
// documentation ```ts\nfunction forLoopObjectDestructuring(): number\n```
73+
for (const { a } of [props]) {
74+
// ^ definition local 41
75+
// documentation ```ts\nvar a: number\n```
76+
// ^ reference syntax 1.0.0 src/`destructuring.ts`/Props#a.
77+
// ^^^^^ reference syntax 1.0.0 src/`destructuring.ts`/props.
78+
return a
79+
// ^ reference local 41
80+
}
81+
return 1
82+
}
83+
84+
export function forLoopArrayDestructuring(): number {
85+
// ^^^^^^^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`destructuring.ts`/forLoopArrayDestructuring().
86+
// documentation ```ts\nfunction forLoopArrayDestructuring(): number\n```
87+
for (const [{ a }] of [[props]]) {
88+
// ^ definition local 48
89+
// documentation ```ts\nvar a: number\n```
90+
// ^ reference syntax 1.0.0 src/`destructuring.ts`/Props#a.
91+
// ^^^^^ reference syntax 1.0.0 src/`destructuring.ts`/props.
92+
return a
93+
// ^ reference local 48
94+
}
95+
return 1
96+
}
97+
98+
export function parameterDestructuring({ a }: Props): number {
99+
// ^^^^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`destructuring.ts`/parameterDestructuring().
100+
// documentation ```ts\nfunction parameterDestructuring({ a }: Props): number\n```
101+
// ^ definition local 50
102+
// documentation ```ts\n(parameter) a: number\n```
103+
// ^ reference syntax 1.0.0 src/`destructuring.ts`/Props#a.
104+
// ^^^^^ reference syntax 1.0.0 src/`destructuring.ts`/Props#
105+
return a
106+
// ^ reference local 50
107+
}
108+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
export function foo(): Promise<{ member: number }> {
2+
// definition syntax 1.0.0 src/`structural-type.ts`/
3+
//documentation ```ts\nmodule "structural-type.ts"\n```
4+
// ^^^ definition syntax 1.0.0 src/`structural-type.ts`/foo().
5+
// documentation ```ts\nfunction foo(): Promise<{ member: number; }>\n```
6+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Promise#
7+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.iterable.d.ts`/Promise#
8+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.promise.d.ts`/Promise.
9+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.symbol.wellknown.d.ts`/Promise#
10+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2018.promise.d.ts`/Promise#
11+
// ^^^^^^ definition syntax 1.0.0 src/`structural-type.ts`/foo().Promise:typeLiteral0:member.
12+
// documentation ```ts\n(property) member: number\n```
13+
return Promise.resolve({ member: 42 })
14+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Promise#
15+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.iterable.d.ts`/Promise#
16+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.promise.d.ts`/Promise.
17+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.symbol.wellknown.d.ts`/Promise#
18+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2018.promise.d.ts`/Promise#
19+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.promise.d.ts`/PromiseConstructor#resolve().
20+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.promise.d.ts`/PromiseConstructor#resolve().
21+
// ^^^^^^ definition syntax 1.0.0 src/`structural-type.ts`/member0:
22+
// documentation ```ts\n(property) member: number\n```
23+
}
24+
export function bar(): Promise<number> {
25+
// ^^^ definition syntax 1.0.0 src/`structural-type.ts`/bar().
26+
// documentation ```ts\nfunction bar(): Promise<number>\n```
27+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Promise#
28+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.iterable.d.ts`/Promise#
29+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.promise.d.ts`/Promise.
30+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.symbol.wellknown.d.ts`/Promise#
31+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2018.promise.d.ts`/Promise#
32+
return foo().then(x => x.member)
33+
// ^^^ reference syntax 1.0.0 src/`structural-type.ts`/foo().
34+
// ^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Promise#then().
35+
// ^ definition local 4
36+
// documentation ```ts\n(parameter) x: { member: number; }\n```
37+
// ^ reference local 4
38+
// ^^^^^^ reference syntax 1.0.0 src/`structural-type.ts`/foo().Promise:typeLiteral0:member.
39+
}
40+
export function bar2(): Promise<number> {
41+
// ^^^^ definition syntax 1.0.0 src/`structural-type.ts`/bar2().
42+
// documentation ```ts\nfunction bar2(): Promise<number>\n```
43+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Promise#
44+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.iterable.d.ts`/Promise#
45+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.promise.d.ts`/Promise.
46+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2015.symbol.wellknown.d.ts`/Promise#
47+
// ^^^^^^^ reference typescript 4.8.4 lib/`lib.es2018.promise.d.ts`/Promise#
48+
return foo().then(({ member }) => member)
49+
// ^^^ reference syntax 1.0.0 src/`structural-type.ts`/foo().
50+
// ^^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Promise#then().
51+
// ^^^^^^ definition local 10
52+
// documentation ```ts\n(parameter) member: number\n```
53+
// ^^^^^^ reference syntax 1.0.0 src/`structural-type.ts`/foo().Promise:typeLiteral0:member.
54+
// ^^^^^^ reference local 10
55+
}
56+
57+
type OptionsFlags<Type> = { [Property in keyof Type]: boolean }
58+
// ^^^^^^^^^^^^ definition syntax 1.0.0 src/`structural-type.ts`/OptionsFlags#
59+
// documentation ```ts\ntype OptionsFlags\n```
60+
// ^^^^ definition syntax 1.0.0 src/`structural-type.ts`/OptionsFlags#[Type]
61+
// documentation ```ts\nType: Type\n```
62+
// ^^^^^^^^ definition local 12
63+
// documentation ```ts\nProperty: Property\n```
64+
// ^^^^ reference syntax 1.0.0 src/`structural-type.ts`/OptionsFlags#[Type]
65+
type FeatureFlags = { darkMode: () => void }
66+
// ^^^^^^^^^^^^ definition syntax 1.0.0 src/`structural-type.ts`/FeatureFlags#
67+
// documentation ```ts\ntype FeatureFlags\n```
68+
// ^^^^^^^^ definition syntax 1.0.0 src/`structural-type.ts`/FeatureFlags#typeLiteral13:darkMode.
69+
// documentation ```ts\n(property) darkMode: () => void\n```
70+
export type FeatureOptions = OptionsFlags<FeatureFlags> // implicitly // type FeatureOptions = { // darkMode: boolean; // } const fo: FeatureOptions = { darkMode: true }; // ^ go to def
71+
// ^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`structural-type.ts`/FeatureOptions#
72+
// documentation ```ts\ntype FeatureOptions\n```
73+
// ^^^^^^^^^^^^ reference syntax 1.0.0 src/`structural-type.ts`/OptionsFlags#
74+
// ^^^^^^^^^^^^ reference syntax 1.0.0 src/`structural-type.ts`/FeatureFlags#
75+
export const fo: FeatureOptions = { darkMode: true }
76+
// ^^ definition syntax 1.0.0 src/`structural-type.ts`/fo.
77+
// documentation ```ts\nvar fo: OptionsFlags<FeatureFlags>\n```
78+
// ^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`structural-type.ts`/FeatureOptions#
79+
// ^^^^^^^^ definition syntax 1.0.0 src/`structural-type.ts`/darkMode0:
80+
// documentation ```ts\n(property) darkMode: true\n```
81+
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function process() {
2+
// definition syntax 1.0.0 src/`typings.ts`/
3+
//documentation ```ts\nmodule "typings.ts"\n```
4+
// ^^^^^^^ definition syntax 1.0.0 src/`typings.ts`/process().
5+
// documentation ```ts\nfunction process(): Process\n```
6+
return window.process
7+
// ^^^^^^ reference typescript 4.8.4 lib/`lib.dom.d.ts`/window.
8+
// ^^^^^^^ reference @types/node 17.0.14 `globals.d.ts`/process.
9+
// ^^^^^^^ reference @types/node 17.0.14 `process.d.ts`/`'process'`/global/process.
10+
}
11+

0 commit comments

Comments
 (0)