Skip to content

Ensure deep cloned AST nodes have synthetic positions #1174

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 2 commits into from
Jun 12, 2025
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
9 changes: 8 additions & 1 deletion internal/ast/deepclone.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ast

import "github.com/microsoft/typescript-go/internal/core"

// Ideally, this would get cached on the node factory so there's only ever one set of closures made per factory
func getDeepCloneVisitor(f *NodeFactory) *NodeVisitor {
var visitor *NodeVisitor
Expand All @@ -9,7 +11,12 @@ func getDeepCloneVisitor(f *NodeFactory) *NodeVisitor {
if visited != node {
return visited
}
return node.Clone(f) // forcibly clone leaf nodes, which will then cascade new nodes/arrays upwards via `update` calls
c := node.Clone(f) // forcibly clone leaf nodes, which will then cascade new nodes/arrays upwards via `update` calls
// In strada, `factory.cloneNode` was dynamic and did _not_ clone positions for any "special cases", meanwhile
// Node.Clone in corsa reliably uses `Update` calls for all nodes and so copies locations by default.
// Deep clones are done to copy a node across files, so here, we explicitly make the location range synthetic on all cloned nodes
c.Loc = core.NewTextRange(-1, -1)
return c
},
f,
NodeVisitorHooks{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
===================================================================
JsFile: export.d.ts
mapUrl: export.d.ts.map
sourceRoot:
sources: export.ts
===================================================================
-------------------------------------------------------------------
emittedFile:export.d.ts
sourceFile:export.ts
-------------------------------------------------------------------
>>>/**
1 >
2 >^^^^^^^^^^^^^^^^^^^^^^^->
1 >
1 >Emitted(1, 1) Source(1, 1) + SourceIndex(0)
---
>>> * blah blah blah blah
>>> * blah blah blah blah
>>> * blah blah blah blah
>>> * blah blah blah blah
>>> * blah blah blah blah
>>> */
1->^^^
2 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1->/**
> * blah blah blah blah
> * blah blah blah blah
> * blah blah blah blah
> * blah blah blah blah
> * blah blah blah blah
> */
1->Emitted(7, 4) Source(7, 4) + SourceIndex(0)
---
>>>export declare function foo(): (_item: unknown) => _item is boolean;
1->
2 >^^^^^^^^^^^^^^^^^^^^^^^^
3 > ^^^
4 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1->
>
>
2 >export function
3 > foo
4 > () {
> return (_item: unknown): _item is boolean => {
> return true;
> };
> }
1->Emitted(8, 1) Source(9, 1) + SourceIndex(0)
2 >Emitted(8, 25) Source(9, 17) + SourceIndex(0)
3 >Emitted(8, 28) Source(9, 20) + SourceIndex(0)
4 >Emitted(8, 69) Source(13, 2) + SourceIndex(0)
---
>>>//# sourceMappingURL=export.d.ts.map===================================================================
JsFile: import.d.ts
mapUrl: import.d.ts.map
sourceRoot:
sources: import.ts
===================================================================
-------------------------------------------------------------------
emittedFile:import.d.ts
sourceFile:import.ts
-------------------------------------------------------------------
>>>export declare const x: (_item: unknown) => _item is boolean;
1 >
2 >^^^^^^^^^^^^^^^
3 > ^^^^^^
4 > ^
5 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6 > ^
1 >import { foo } from './export';
>
2 >export
3 > const
4 > x
5 > = foo()
6 > ;
1 >Emitted(1, 1) Source(2, 1) + SourceIndex(0)
2 >Emitted(1, 16) Source(2, 8) + SourceIndex(0)
3 >Emitted(1, 22) Source(2, 14) + SourceIndex(0)
4 >Emitted(1, 23) Source(2, 15) + SourceIndex(0)
5 >Emitted(1, 61) Source(2, 23) + SourceIndex(0)
6 >Emitted(1, 62) Source(2, 24) + SourceIndex(0)
---
>>>//# sourceMappingURL=import.d.ts.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//// [tests/cases/compiler/declarationEmitNoCrashOnCommentCopiedFromOtherFile.ts] ////

=== export.ts ===
/**
* blah blah blah blah
* blah blah blah blah
* blah blah blah blah
* blah blah blah blah
* blah blah blah blah
*/

export function foo() {
>foo : Symbol(foo, Decl(export.ts, 0, 0))

return (_item: unknown): _item is boolean => {
>_item : Symbol(_item, Decl(export.ts, 9, 10))
>_item : Symbol(_item, Decl(export.ts, 9, 10))

return true;
};
}
=== import.ts ===
import { foo } from './export';
>foo : Symbol(foo, Decl(import.ts, 0, 8))

export const x = foo();
>x : Symbol(x, Decl(import.ts, 1, 12))
>foo : Symbol(foo, Decl(import.ts, 0, 8))

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//// [tests/cases/compiler/declarationEmitNoCrashOnCommentCopiedFromOtherFile.ts] ////

=== export.ts ===
/**
* blah blah blah blah
* blah blah blah blah
* blah blah blah blah
* blah blah blah blah
* blah blah blah blah
*/

export function foo() {
>foo : () => (_item: unknown) => _item is boolean

return (_item: unknown): _item is boolean => {
>(_item: unknown): _item is boolean => { return true; } : (_item: unknown) => _item is boolean
>_item : unknown

return true;
>true : true

};
}
=== import.ts ===
import { foo } from './export';
>foo : () => (_item: unknown) => _item is boolean

export const x = foo();
>x : (_item: unknown) => _item is boolean
>foo() : (_item: unknown) => _item is boolean
>foo : () => (_item: unknown) => _item is boolean

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var results: string[];


function f([, a, , b, , , , s, , , ] = results) {
>f : ([, a, , b, , , , s, , ,]?: string[]) => void
>f : ([, a, , b, , , , s, , ]?: string[]) => void
>a : string
>b : string
>s : string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--- old.arrayBindingPatternOmittedExpressions.types
+++ new.arrayBindingPatternOmittedExpressions.types
@@= skipped -23, +23 lines =@@


function f([, a, , b, , , , s, , , ] = results) {
->f : ([, a, , b, , , , s, , ,]?: string[]) => void
+>f : ([, a, , b, , , , s, , ]?: string[]) => void
Copy link
Member

Choose a reason for hiding this comment

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

This is odd, what's the deal?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, it's mainly just losing the trailing comma, then.

Copy link
Member Author

Choose a reason for hiding this comment

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

trailing comma preservation is done by looking at positions

>a : string
>b : string
>s : string
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

=== declarationEmitDestructuring5.ts ===
function baz([, z, , ]) { }
>baz : ([, z, ,]: [any, any, any?]) => void
>baz : ([, z, ]: [any, any, any?]) => void
>z : any

function foo([, b, ]: [any, any]): void { }
>foo : ([, b,]: [any, any]) => void
>b : any

function bar([z, , , ]) { }
>bar : ([z, , ,]: [any, any?, any?]) => void
>bar : ([z, , ]: [any, any?, any?]) => void
>z : any

function bar1([z, , , ] = [1, 3, 4, 6, 7]) { }
>bar1 : ([z, , ,]?: [number, number, number, number, number]) => void
>bar1 : ([z, , ]?: [number, number, number, number, number]) => void
>z : number
>[1, 3, 4, 6, 7] : [number, number, number, number, number]
>1 : 1
Expand All @@ -24,6 +24,6 @@ function bar1([z, , , ] = [1, 3, 4, 6, 7]) { }
>7 : 7

function bar2([,,z, , , ]) { }
>bar2 : ([, , z, , ,]: [any, any, any, any?, any?]) => void
>bar2 : ([, , z, , ]: [any, any, any, any?, any?]) => void
>z : any

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--- old.declarationEmitDestructuring5.types
+++ new.declarationEmitDestructuring5.types
@@= skipped -1, +1 lines =@@

=== declarationEmitDestructuring5.ts ===
function baz([, z, , ]) { }
->baz : ([, z, ,]: [any, any, any?]) => void
+>baz : ([, z, ]: [any, any, any?]) => void
>z : any

function foo([, b, ]: [any, any]): void { }
@@= skipped -8, +8 lines =@@
>b : any

function bar([z, , , ]) { }
->bar : ([z, , ,]: [any, any?, any?]) => void
+>bar : ([z, , ]: [any, any?, any?]) => void
>z : any

function bar1([z, , , ] = [1, 3, 4, 6, 7]) { }
->bar1 : ([z, , ,]?: [number, number, number, number, number]) => void
+>bar1 : ([z, , ]?: [number, number, number, number, number]) => void
>z : number
>[1, 3, 4, 6, 7] : [number, number, number, number, number]
>1 : 1
@@= skipped -14, +14 lines =@@
>7 : 7

function bar2([,,z, , , ]) { }
->bar2 : ([, , z, , ,]: [any, any, any, any?, any?]) => void
+>bar2 : ([, , z, , ]: [any, any, any, any?, any?]) => void
>z : any
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ export declare const m: {
};
export declare const x: {
p: {
o: (
/**
* leading doc for prop
*/
a?: string[]) => void;
o: (a?: string[]) => void;
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
export declare const x: {
p: {
- o: (a?: (import("./a").X | undefined)[]) => void;
+ o: (
+ /**
+ * leading doc for prop
+ */
+ a?: string[]) => void;
+ o: (a?: string[]) => void;
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// @target: es2017
// @declaration: true
// @declarationMap: true
// @strict: true
// @esModuleInterop: true
// @filename: export.ts
/**
* blah blah blah blah
* blah blah blah blah
* blah blah blah blah
* blah blah blah blah
* blah blah blah blah
*/

export function foo() {
return (_item: unknown): _item is boolean => {
return true;
};
}
//@filename: import.ts
import { foo } from './export';
export const x = foo();