Skip to content

Commit 3f675f1

Browse files
authored
Always set synthetic position when deep cloning (#1610)
1 parent 3a82d57 commit 3f675f1

12 files changed

+329
-58
lines changed

internal/ast/ast.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,11 @@ func (list *NodeList) Pos() int { return list.Loc.Pos() }
183183
func (list *NodeList) End() int { return list.Loc.End() }
184184

185185
func (list *NodeList) HasTrailingComma() bool {
186-
if len(list.Nodes) == 0 || PositionIsSynthesized(list.End()) {
186+
if len(list.Nodes) == 0 {
187187
return false
188188
}
189189
last := list.Nodes[len(list.Nodes)-1]
190-
return !PositionIsSynthesized(last.End()) && last.End() < list.End()
190+
return last.End() < list.End()
191191
}
192192

193193
func (list *NodeList) Clone(f NodeFactoryCoercible) *NodeList {

internal/ast/deepclone.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ func getDeepCloneVisitor(f *NodeFactory, syntheticLocation bool) *NodeVisitor {
99
func(node *Node) *Node {
1010
visited := visitor.VisitEachChild(node)
1111
if visited != node {
12+
if syntheticLocation {
13+
visited.Loc = core.NewTextRange(-1, -1)
14+
}
1215
return visited
1316
}
1417
c := node.Clone(f) // forcibly clone leaf nodes, which will then cascade new nodes/arrays upwards via `update` calls
@@ -26,21 +29,39 @@ func getDeepCloneVisitor(f *NodeFactory, syntheticLocation bool) *NodeVisitor {
2629
if nodes == nil {
2730
return nil
2831
}
29-
// force update empty lists
30-
if len(nodes.Nodes) == 0 {
31-
return nodes.Clone(v.Factory)
32+
visited := v.VisitNodes(nodes)
33+
var newList *NodeList
34+
if visited != nodes {
35+
newList = visited
36+
} else {
37+
newList = nodes.Clone(v.Factory)
38+
}
39+
if syntheticLocation {
40+
newList.Loc = core.NewTextRange(-1, -1)
41+
if nodes.HasTrailingComma() {
42+
newList.Nodes[len(newList.Nodes)-1].Loc = core.NewTextRange(-2, -2)
43+
}
3244
}
33-
return v.VisitNodes(nodes)
45+
return newList
3446
},
3547
VisitModifiers: func(nodes *ModifierList, v *NodeVisitor) *ModifierList {
3648
if nodes == nil {
3749
return nil
3850
}
39-
// force update empty lists
40-
if len(nodes.Nodes) == 0 {
41-
return nodes.Clone(v.Factory)
51+
visited := v.VisitModifiers(nodes)
52+
var newList *ModifierList
53+
if visited != nodes {
54+
newList = visited
55+
} else {
56+
newList = nodes.Clone(v.Factory)
57+
}
58+
if syntheticLocation {
59+
newList.Loc = core.NewTextRange(-1, -1)
60+
if nodes.HasTrailingComma() {
61+
newList.Nodes[len(newList.Nodes)-1].Loc = core.NewTextRange(-2, -2)
62+
}
4263
}
43-
return v.VisitModifiers(nodes)
64+
return newList
4465
},
4566
},
4667
)

testdata/baselines/reference/compiler/declarationEmitNoCrashOnCrossFileNode.js

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testdata/baselines/reference/compiler/declarationEmitNoCrashOnCrossFileNode.js.map

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
===================================================================
2+
JsFile: other.d.ts
3+
mapUrl: other.d.ts.map
4+
sourceRoot:
5+
sources: other.ts
6+
===================================================================
7+
-------------------------------------------------------------------
8+
emittedFile:/other.d.ts
9+
sourceFile:other.ts
10+
-------------------------------------------------------------------
11+
>>>/**
12+
1 >
13+
2 >^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
14+
1 >
15+
1 >Emitted(1, 1) Source(1, 1) + SourceIndex(0)
16+
---
17+
>>> * Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris eu aliquet lectus, nec rhoncus metus. Donec dapibus consectetur risus vitae porta. Aenean nisi neque, dignissim quis varius vel, volutpat vel tellus. Praesent lacinia molestie est, vel convallis odio ornare id. Pellentesque quis purus ante. Morbi a nisl justo. Etiam malesuada ipsum sem, fringilla rhoncus turpis ullamcorper et. Aenean laoreet, nisl id tempus pellentesque, elit elit congue felis, sit amet luctus nulla orci sit amet velit. Praesent non tincidunt nisi, at tempor eros. Quisque tincidunt euismod posuere. Ut blandit mauris elit, a porttitor orci aliquam ac. Duis imperdiet gravida ultrices. In.
18+
>>> */
19+
1->^^^
20+
2 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
21+
1->/**
22+
> * Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris eu aliquet lectus, nec rhoncus metus. Donec dapibus consectetur risus vitae porta. Aenean nisi neque, dignissim quis varius vel, volutpat vel tellus. Praesent lacinia molestie est, vel convallis odio ornare id. Pellentesque quis purus ante. Morbi a nisl justo. Etiam malesuada ipsum sem, fringilla rhoncus turpis ullamcorper et. Aenean laoreet, nisl id tempus pellentesque, elit elit congue felis, sit amet luctus nulla orci sit amet velit. Praesent non tincidunt nisi, at tempor eros. Quisque tincidunt euismod posuere. Ut blandit mauris elit, a porttitor orci aliquam ac. Duis imperdiet gravida ultrices. In.
23+
> */
24+
1->Emitted(3, 4) Source(3, 4) + SourceIndex(0)
25+
---
26+
>>>export declare function foo(): ({ a, b }: {
27+
1->
28+
2 >^^^^^^^^^^^^^^^^^^^^^^^^
29+
3 > ^^^
30+
4 > ^^^^
31+
5 > ^
32+
6 > ^^
33+
7 > ^
34+
8 > ^^
35+
9 > ^
36+
10> ^^
37+
11> ^^
38+
1->
39+
>
40+
>
41+
2 >export function
42+
3 > foo
43+
4 > ():
44+
5 > (
45+
6 > {
46+
7 > a
47+
8 > ,
48+
9 > b
49+
10> }
50+
11> :
51+
1->Emitted(4, 1) Source(5, 1) + SourceIndex(0)
52+
2 >Emitted(4, 25) Source(5, 17) + SourceIndex(0)
53+
3 >Emitted(4, 28) Source(5, 20) + SourceIndex(0)
54+
4 >Emitted(4, 32) Source(5, 24) + SourceIndex(0)
55+
5 >Emitted(4, 33) Source(5, 25) + SourceIndex(0)
56+
6 >Emitted(4, 35) Source(5, 27) + SourceIndex(0)
57+
7 >Emitted(4, 36) Source(5, 28) + SourceIndex(0)
58+
8 >Emitted(4, 38) Source(5, 30) + SourceIndex(0)
59+
9 >Emitted(4, 39) Source(5, 31) + SourceIndex(0)
60+
10>Emitted(4, 41) Source(5, 33) + SourceIndex(0)
61+
11>Emitted(4, 43) Source(5, 35) + SourceIndex(0)
62+
---
63+
>>> a: string;
64+
1 >^^^^
65+
2 > ^
66+
3 > ^^
67+
4 > ^^^^^^
68+
5 > ^
69+
6 > ^->
70+
1 >{
71+
2 > a
72+
3 > :
73+
4 > string
74+
5 > ,
75+
1 >Emitted(5, 5) Source(5, 37) + SourceIndex(0)
76+
2 >Emitted(5, 6) Source(5, 38) + SourceIndex(0)
77+
3 >Emitted(5, 8) Source(5, 40) + SourceIndex(0)
78+
4 >Emitted(5, 14) Source(5, 46) + SourceIndex(0)
79+
5 >Emitted(5, 15) Source(5, 47) + SourceIndex(0)
80+
---
81+
>>> b: string;
82+
1->^^^^
83+
2 > ^
84+
3 > ^^
85+
4 > ^^^^^^
86+
5 > ^
87+
1->
88+
2 > b
89+
3 > :
90+
4 > string
91+
5 >
92+
1->Emitted(6, 5) Source(5, 48) + SourceIndex(0)
93+
2 >Emitted(6, 6) Source(5, 49) + SourceIndex(0)
94+
3 >Emitted(6, 8) Source(5, 51) + SourceIndex(0)
95+
4 >Emitted(6, 14) Source(5, 57) + SourceIndex(0)
96+
5 >Emitted(6, 15) Source(5, 57) + SourceIndex(0)
97+
---
98+
>>>}) => void;
99+
1 >^
100+
2 > ^^^^^
101+
3 > ^^^^
102+
4 > ^
103+
5 > ^^^^^^^^^^^^^^^^^^^^^^^->
104+
1 > }
105+
2 > ) =>
106+
3 > void
107+
4 > {
108+
> return () => {}
109+
> }
110+
1 >Emitted(7, 2) Source(5, 59) + SourceIndex(0)
111+
2 >Emitted(7, 7) Source(5, 64) + SourceIndex(0)
112+
3 >Emitted(7, 11) Source(5, 68) + SourceIndex(0)
113+
4 >Emitted(7, 12) Source(7, 2) + SourceIndex(0)
114+
---
115+
>>>//# sourceMappingURL=other.d.ts.map===================================================================
116+
JsFile: index.d.ts
117+
mapUrl: index.d.ts.map
118+
sourceRoot:
119+
sources: index.ts
120+
===================================================================
121+
-------------------------------------------------------------------
122+
emittedFile:/index.d.ts
123+
sourceFile:index.ts
124+
-------------------------------------------------------------------
125+
>>>export declare class Foo {
126+
1 >
127+
2 >^^^^^^^^^^^^^^^^^^^^^
128+
3 > ^^^
129+
1 >import { foo } from './other';
130+
>
131+
2 >export class
132+
3 > Foo
133+
1 >Emitted(1, 1) Source(2, 1) + SourceIndex(0)
134+
2 >Emitted(1, 22) Source(2, 14) + SourceIndex(0)
135+
3 >Emitted(1, 25) Source(2, 17) + SourceIndex(0)
136+
---
137+
>>> bar: ({ a, b }: {
138+
1 >^^^^
139+
2 > ^^^
140+
3 > ^^^^^^^^^^^^->
141+
1 > {
142+
> public
143+
2 > bar
144+
1 >Emitted(2, 5) Source(3, 12) + SourceIndex(0)
145+
2 >Emitted(2, 8) Source(3, 15) + SourceIndex(0)
146+
---
147+
>>> a: string;
148+
>>> b: string;
149+
>>> }) => void;
150+
1->^^^^^^^^^^^^^^^
151+
1-> = foo();
152+
1->Emitted(5, 16) Source(3, 24) + SourceIndex(0)
153+
---
154+
>>>}
155+
1 >^
156+
2 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
157+
1 >
158+
>}
159+
1 >Emitted(6, 2) Source(4, 2) + SourceIndex(0)
160+
---
161+
>>>//# sourceMappingURL=index.d.ts.map
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [tests/cases/compiler/declarationEmitNoCrashOnCrossFileNode.ts] ////
2+
3+
=== /index.ts ===
4+
import { foo } from './other';
5+
>foo : Symbol(foo, Decl(index.ts, 0, 8))
6+
7+
export class Foo {
8+
>Foo : Symbol(Foo, Decl(index.ts, 0, 30))
9+
10+
public bar = foo();
11+
>bar : Symbol(Foo.bar, Decl(index.ts, 1, 18))
12+
>foo : Symbol(foo, Decl(index.ts, 0, 8))
13+
}
14+
=== /other.ts ===
15+
/**
16+
* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris eu aliquet lectus, nec rhoncus metus. Donec dapibus consectetur risus vitae porta. Aenean nisi neque, dignissim quis varius vel, volutpat vel tellus. Praesent lacinia molestie est, vel convallis odio ornare id. Pellentesque quis purus ante. Morbi a nisl justo. Etiam malesuada ipsum sem, fringilla rhoncus turpis ullamcorper et. Aenean laoreet, nisl id tempus pellentesque, elit elit congue felis, sit amet luctus nulla orci sit amet velit. Praesent non tincidunt nisi, at tempor eros. Quisque tincidunt euismod posuere. Ut blandit mauris elit, a porttitor orci aliquam ac. Duis imperdiet gravida ultrices. In.
17+
*/
18+
19+
export function foo(): ({ a, b }: { a: string, b: string }) => void {
20+
>foo : Symbol(foo, Decl(other.ts, 0, 0))
21+
>a : Symbol(a, Decl(other.ts, 4, 25))
22+
>b : Symbol(b, Decl(other.ts, 4, 28))
23+
>a : Symbol(a, Decl(other.ts, 4, 35))
24+
>b : Symbol(b, Decl(other.ts, 4, 46))
25+
26+
return () => {}
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//// [tests/cases/compiler/declarationEmitNoCrashOnCrossFileNode.ts] ////
2+
3+
=== /index.ts ===
4+
import { foo } from './other';
5+
>foo : () => ({ a, b }: { a: string; b: string; }) => void
6+
7+
export class Foo {
8+
>Foo : Foo
9+
10+
public bar = foo();
11+
>bar : ({ a, b }: { a: string; b: string; }) => void
12+
>foo() : ({ a, b }: { a: string; b: string; }) => void
13+
>foo : () => ({ a, b }: { a: string; b: string; }) => void
14+
}
15+
=== /other.ts ===
16+
/**
17+
* Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris eu aliquet lectus, nec rhoncus metus. Donec dapibus consectetur risus vitae porta. Aenean nisi neque, dignissim quis varius vel, volutpat vel tellus. Praesent lacinia molestie est, vel convallis odio ornare id. Pellentesque quis purus ante. Morbi a nisl justo. Etiam malesuada ipsum sem, fringilla rhoncus turpis ullamcorper et. Aenean laoreet, nisl id tempus pellentesque, elit elit congue felis, sit amet luctus nulla orci sit amet velit. Praesent non tincidunt nisi, at tempor eros. Quisque tincidunt euismod posuere. Ut blandit mauris elit, a porttitor orci aliquam ac. Duis imperdiet gravida ultrices. In.
18+
*/
19+
20+
export function foo(): ({ a, b }: { a: string, b: string }) => void {
21+
>foo : () => ({ a, b }: { a: string; b: string; }) => void
22+
>a : string
23+
>b : string
24+
>a : string
25+
>b : string
26+
27+
return () => {}
28+
>() => {} : () => void
29+
}

testdata/baselines/reference/submodule/compiler/arrayBindingPatternOmittedExpressions.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var results: string[];
2424

2525

2626
function f([, a, , b, , , , s, , , ] = results) {
27-
>f : ([, a, , b, , , , s, , ]?: string[]) => void
27+
>f : ([, a, , b, , , , s, , ,]?: string[]) => void
2828
>a : string
2929
>b : string
3030
>s : string

testdata/baselines/reference/submodule/compiler/arrayBindingPatternOmittedExpressions.types.diff

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)