77 "github.com/microsoft/typescript-go/internal/core"
88 "github.com/microsoft/typescript-go/internal/nodebuilder"
99 "github.com/microsoft/typescript-go/internal/printer"
10- "github.com/microsoft/typescript-go/internal/scanner"
1110)
1211
1312func (b * NodeBuilderImpl ) reuseNode (node * ast.Node ) * ast.Node {
@@ -22,35 +21,36 @@ func (b *NodeBuilderImpl) tryJSTypeNodeToTypeNode(node *ast.Node) *ast.Node {
2221 return b .reuseNode (node )
2322}
2423
25- // a wrapper around `reuseNode` for property names. It handles renaming `new` to `"new"` so we don't
26- // accidentally emit constructor signatures when we don't mean to, and normalizes string-literal
27- // property names whose text is a valid identifier into identifiers, matching the behavior of
28- // `createPropertyNameNodeForIdentifierOrLiteral` used when constructing fresh property name nodes
29- // (so that reused names emit consistently regardless of whether their containing type was reused
30- // from source or rebuilt from a type).
31- func (b * NodeBuilderImpl ) reuseName (node * ast.Node ) * ast.Node {
24+ func (b * NodeBuilderImpl ) reuseName (node * ast.Node , isMethod bool ) * ast.Node {
3225 res := b .reuseNode (node )
3326 if res == nil {
3427 return res
3528 }
36- if res . Kind == ast . KindIdentifier && node . AsIdentifier (). Text == "new" {
37- str := b . f . NewStringLiteral ( "new" , ast .TokenFlagsNone )
38- b . e . SetOriginal ( str , res )
39- return b . setTextRange ( str , res )
29+
30+ text , ok := ast .TryGetTextOfPropertyName ( res )
31+ if ! ok {
32+ return res
4033 }
41- if res .Kind == ast .KindStringLiteral {
42- text := res .AsStringLiteral ().Text
43- // Skip normalization for "new" so that reused names like `"new"(): void` on a
44- // method signature are not converted to an identifier (which would become a
45- // construct signature). This mirrors the `isMethodNamedNew` guard in
46- // createPropertyNameNodeForIdentifierOrLiteral.
47- if text != "new" && scanner .IsIdentifierText (text , core .LanguageVariantStandard ) {
48- ident := b .newIdentifier (text , nil )
49- b .e .SetOriginal (ident , res )
50- return b .setTextRange (ident , res )
51- }
34+
35+ kind := classifyPropertyName (text , ast .IsStringLiteral (res ), isMethod )
36+ if ast .IsIdentifier (res ) && kind == propertyNameNodeKindIdentifier {
37+ return res
5238 }
53- return res
39+ if ast .IsStringLiteral (res ) && kind == propertyNameNodeKindStringLiteral {
40+ return res
41+ }
42+
43+ var renamed * ast.Node
44+ switch kind {
45+ case propertyNameNodeKindIdentifier :
46+ renamed = b .newIdentifier (text , nil )
47+ case propertyNameNodeKindStringLiteral :
48+ renamed = b .f .NewStringLiteral (text , ast .TokenFlagsNone )
49+ default :
50+ return res
51+ }
52+ b .e .SetOriginal (renamed , res )
53+ return b .setTextRange (renamed , res )
5454}
5555
5656func (b * NodeBuilderImpl ) reuseTypeNode (node * ast.Node ) * ast.Node {
0 commit comments