Skip to content

Commit 14e44b1

Browse files
muglugjakebailey
andauthored
Skip defaults when printing BindingName (#1025)
Co-authored-by: Jake Bailey <[email protected]>
1 parent 951a76f commit 14e44b1

File tree

127 files changed

+392
-2312
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+392
-2312
lines changed

internal/checker/nodebuilder.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ func (b *NodeBuilder) enterContext(enclosingDeclaration *ast.Node, flags nodebui
4040
}
4141

4242
func (b *NodeBuilder) popContext() {
43-
b.impl.ctx = nil
44-
if len(b.ctxStack) > 1 {
45-
b.impl.ctx = b.ctxStack[len(b.ctxStack)-1]
43+
stackSize := len(b.ctxStack)
44+
if stackSize == 0 {
45+
b.impl.ctx = nil
46+
} else {
47+
b.impl.ctx = b.ctxStack[stackSize-1]
48+
b.ctxStack = b.ctxStack[:stackSize-1]
4649
}
47-
b.ctxStack = b.ctxStack[:len(b.ctxStack)-1]
4850
}
4951

5052
func (b *NodeBuilder) exitContext(result *ast.Node) *ast.Node {
@@ -165,7 +167,7 @@ func (b *NodeBuilder) TypeToTypeNode(typ *Type, enclosingDeclaration *ast.Node,
165167

166168
func NewNodeBuilder(ch *Checker, e *printer.EmitContext) *NodeBuilder {
167169
impl := newNodeBuilderImpl(ch, e)
168-
return &NodeBuilder{impl: &impl, ctxStack: make([]*NodeBuilderContext, 0, 1), host: ch.program}
170+
return &NodeBuilder{impl: impl, ctxStack: make([]*NodeBuilderContext, 0, 1), host: ch.program}
169171
}
170172

171173
func (c *Checker) GetDiagnosticNodeBuilder() *NodeBuilder {

internal/checker/nodebuilderimpl.go

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ type nodeBuilderImpl struct {
9393

9494
// state
9595
ctx *NodeBuilderContext
96+
97+
// reusable visitor
98+
cloneBindingNameVisitor *ast.NodeVisitor
9699
}
97100

98101
const (
@@ -102,9 +105,10 @@ const (
102105

103106
// Node builder utility functions
104107

105-
func newNodeBuilderImpl(ch *Checker, e *printer.EmitContext) nodeBuilderImpl {
106-
result := nodeBuilderImpl{f: e.Factory.AsNodeFactory(), ch: ch, e: e}
107-
return result
108+
func newNodeBuilderImpl(ch *Checker, e *printer.EmitContext) *nodeBuilderImpl {
109+
b := &nodeBuilderImpl{f: e.Factory.AsNodeFactory(), ch: ch, e: e}
110+
b.cloneBindingNameVisitor = ast.NewNodeVisitor(b.cloneBindingName, b.f, ast.NodeVisitorHooks{})
111+
return b
108112
}
109113

110114
func (b *nodeBuilderImpl) saveRestoreFlags() func() {
@@ -1468,8 +1472,46 @@ func (b *nodeBuilderImpl) parameterToParameterDeclarationName(parameterSymbol *a
14681472
if parameterDeclaration == nil || parameterDeclaration.Name() == nil {
14691473
return b.f.NewIdentifier(parameterSymbol.Name)
14701474
}
1471-
// !!! TODO - symbol tracking of computed names in cloned binding patterns, set singleline emit flags
1472-
return b.f.DeepCloneNode(parameterDeclaration.Name())
1475+
1476+
name := parameterDeclaration.Name()
1477+
switch name.Kind {
1478+
case ast.KindIdentifier:
1479+
cloned := b.f.DeepCloneNode(name)
1480+
b.e.SetEmitFlags(cloned, printer.EFNoAsciiEscaping)
1481+
return cloned
1482+
case ast.KindQualifiedName:
1483+
cloned := b.f.DeepCloneNode(name.AsQualifiedName().Right)
1484+
b.e.SetEmitFlags(cloned, printer.EFNoAsciiEscaping)
1485+
return cloned
1486+
default:
1487+
return b.cloneBindingName(name)
1488+
}
1489+
}
1490+
1491+
func (b *nodeBuilderImpl) cloneBindingName(node *ast.Node) *ast.Node {
1492+
if ast.IsComputedPropertyName(node) && b.ch.isLateBindableName(node) {
1493+
b.trackComputedName(node.Expression(), b.ctx.enclosingDeclaration)
1494+
}
1495+
1496+
visited := b.cloneBindingNameVisitor.VisitEachChild(node)
1497+
1498+
if ast.IsBindingElement(visited) {
1499+
bindingElement := visited.AsBindingElement()
1500+
visited = b.f.UpdateBindingElement(
1501+
bindingElement,
1502+
bindingElement.DotDotDotToken,
1503+
bindingElement.PropertyName,
1504+
bindingElement.Name(),
1505+
nil, // remove initializer
1506+
)
1507+
}
1508+
1509+
if !ast.NodeIsSynthesized(visited) {
1510+
visited = b.f.DeepCloneNode(visited)
1511+
}
1512+
1513+
b.e.SetEmitFlags(visited, printer.EFSingleLine|printer.EFNoAsciiEscaping)
1514+
return visited
14731515
}
14741516

14751517
func (b *nodeBuilderImpl) symbolTableToDeclarationStatements(symbolTable *ast.SymbolTable) []*ast.Node {

internal/scanner/scanner.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,13 +2086,14 @@ func SkipTriviaEx(text string, pos int, options *SkipTriviaOptions) int {
20862086
options = &SkipTriviaOptions{}
20872087
}
20882088

2089+
textLen := len(text)
20892090
canConsumeStar := false
20902091
// Keep in sync with couldStartTrivia
20912092
for {
20922093
ch, size := utf8.DecodeRuneInString(text[pos:])
20932094
switch ch {
20942095
case '\r':
2095-
if text[pos+1] == '\n' {
2096+
if pos+1 < textLen && text[pos+1] == '\n' {
20962097
pos++
20972098
}
20982099
fallthrough
@@ -2110,10 +2111,10 @@ func SkipTriviaEx(text string, pos int, options *SkipTriviaOptions) int {
21102111
if options.StopAtComments {
21112112
break
21122113
}
2113-
if pos+1 < len(text) {
2114+
if pos+1 < textLen {
21142115
if text[pos+1] == '/' {
21152116
pos += 2
2116-
for pos < len(text) {
2117+
for pos < textLen {
21172118
ch, size := utf8.DecodeRuneInString(text[pos:])
21182119
if stringutil.IsLineBreak(ch) {
21192120
break
@@ -2125,8 +2126,8 @@ func SkipTriviaEx(text string, pos int, options *SkipTriviaOptions) int {
21252126
}
21262127
if text[pos+1] == '*' {
21272128
pos += 2
2128-
for pos < len(text) {
2129-
if text[pos] == '*' && text[pos+1] == '/' {
2129+
for pos < textLen {
2130+
if text[pos] == '*' && (pos+1 < textLen) && text[pos+1] == '/' {
21302131
pos += 2
21312132
break
21322133
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
=== bug25434.js ===
44
// should not crash while checking
55
function Test({ b = '' } = {}) {}
6-
>Test : ({ b = "" }?: { b?: string; }) => void
6+
>Test : ({ b }?: { b?: string; }) => void
77
>b : string
88
>'' : ""
99
>{} : {}
1010

1111
Test(({ b = '5' } = {}));
1212
>Test(({ b = '5' } = {})) : void
13-
>Test : ({ b = "" }?: { b?: string; }) => void
13+
>Test : ({ b }?: { b?: string; }) => void
1414
>({ b = '5' } = {}) : {}
1515
>{ b = '5' } = {} : {}
1616
>{ b = '5' } : { b?: any; }

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
=== contextualTypeForInitalizedVariablesFiltersUndefined.ts ===
44
const fInferred = ({ a = 0 } = {}) => a;
5-
>fInferred : ({ a = 0 }?: { a?: number | undefined; }) => number
6-
>({ a = 0 } = {}) => a : ({ a = 0 }?: { a?: number | undefined; }) => number
5+
>fInferred : ({ a }?: { a?: number | undefined; }) => number
6+
>({ a = 0 } = {}) => a : ({ a }?: { a?: number | undefined; }) => number
77
>a : number
88
>0 : 0
99
>{} : {}
@@ -12,9 +12,9 @@ const fInferred = ({ a = 0 } = {}) => a;
1212
// const fInferred: ({ a }?: { a?: number; }) => number
1313

1414
const fAnnotated: typeof fInferred = ({ a = 0 } = {}) => a;
15-
>fAnnotated : ({ a = 0 }?: { a?: number | undefined; }) => number
16-
>fInferred : ({ a = 0 }?: { a?: number | undefined; }) => number
17-
>({ a = 0 } = {}) => a : ({ a = 0 }?: { a?: number | undefined; } | undefined) => number
15+
>fAnnotated : ({ a }?: { a?: number | undefined; }) => number
16+
>fInferred : ({ a }?: { a?: number | undefined; }) => number
17+
>({ a = 0 } = {}) => a : ({ a }?: { a?: number | undefined; } | undefined) => number
1818
>a : number
1919
>0 : 0
2020
>{} : {}

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

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

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,44 +28,44 @@ declare function id5<T extends (x?: number) => any>(input: T): T;
2828
>input : T
2929

3030
const f10 = function ({ foo = 42 }) { return foo };
31-
>f10 : ({ foo = 42 }: { foo?: number | undefined; }) => number
32-
>function ({ foo = 42 }) { return foo } : ({ foo = 42 }: { foo?: number | undefined; }) => number
31+
>f10 : ({ foo }: { foo?: number | undefined; }) => number
32+
>function ({ foo = 42 }) { return foo } : ({ foo }: { foo?: number | undefined; }) => number
3333
>foo : number
3434
>42 : 42
3535
>foo : number
3636

3737
const f11 = id1(function ({ foo = 42 }) { return foo });
38-
>f11 : ({ foo = 42 }: { foo?: number | undefined; }) => number
39-
>id1(function ({ foo = 42 }) { return foo }) : ({ foo = 42 }: { foo?: number | undefined; }) => number
38+
>f11 : ({ foo }: { foo?: number | undefined; }) => number
39+
>id1(function ({ foo = 42 }) { return foo }) : ({ foo }: { foo?: number | undefined; }) => number
4040
>id1 : <T>(input: T) => T
41-
>function ({ foo = 42 }) { return foo } : ({ foo = 42 }: { foo?: number | undefined; }) => number
41+
>function ({ foo = 42 }) { return foo } : ({ foo }: { foo?: number | undefined; }) => number
4242
>foo : number
4343
>42 : 42
4444
>foo : number
4545

4646
const f12 = id2(function ({ foo = 42 }) { return foo });
47-
>f12 : ({ foo = 42 }: any) => any
48-
>id2(function ({ foo = 42 }) { return foo }) : ({ foo = 42 }: any) => any
47+
>f12 : ({ foo }: any) => any
48+
>id2(function ({ foo = 42 }) { return foo }) : ({ foo }: any) => any
4949
>id2 : <T extends (x: any) => any>(input: T) => T
50-
>function ({ foo = 42 }) { return foo } : ({ foo = 42 }: any) => any
50+
>function ({ foo = 42 }) { return foo } : ({ foo }: any) => any
5151
>foo : any
5252
>42 : 42
5353
>foo : any
5454

5555
const f13 = id3(function ({ foo = 42 }) { return foo });
56-
>f13 : ({ foo = 42 }: { foo: any; }) => any
57-
>id3(function ({ foo = 42 }) { return foo }) : ({ foo = 42 }: { foo: any; }) => any
56+
>f13 : ({ foo }: { foo: any; }) => any
57+
>id3(function ({ foo = 42 }) { return foo }) : ({ foo }: { foo: any; }) => any
5858
>id3 : <T extends (x: { foo: any; }) => any>(input: T) => T
59-
>function ({ foo = 42 }) { return foo } : ({ foo = 42 }: { foo: any; }) => any
59+
>function ({ foo = 42 }) { return foo } : ({ foo }: { foo: any; }) => any
6060
>foo : any
6161
>42 : 42
6262
>foo : any
6363

6464
const f14 = id4(function ({ foo = 42 }) { return foo });
65-
>f14 : ({ foo = 42 }: { foo?: number | undefined; }) => number
66-
>id4(function ({ foo = 42 }) { return foo }) : ({ foo = 42 }: { foo?: number | undefined; }) => number
65+
>f14 : ({ foo }: { foo?: number | undefined; }) => number
66+
>id4(function ({ foo = 42 }) { return foo }) : ({ foo }: { foo?: number | undefined; }) => number
6767
>id4 : <T extends (x: { foo?: number | undefined; }) => any>(input: T) => T
68-
>function ({ foo = 42 }) { return foo } : ({ foo = 42 }: { foo?: number | undefined; }) => number
68+
>function ({ foo = 42 }) { return foo } : ({ foo }: { foo?: number | undefined; }) => number
6969
>foo : number
7070
>42 : 42
7171
>foo : number
@@ -254,7 +254,7 @@ function id<T>(input: T): T { return input }
254254
>input : T
255255

256256
function getFoo ({ foo = 42 }) {
257-
>getFoo : ({ foo = 42 }: { foo?: number | undefined; }) => number
257+
>getFoo : ({ foo }: { foo?: number | undefined; }) => number
258258
>foo : number
259259
>42 : 42
260260

@@ -263,17 +263,17 @@ function getFoo ({ foo = 42 }) {
263263
}
264264

265265
const newGetFoo = id(getFoo);
266-
>newGetFoo : ({ foo = 42 }: { foo?: number | undefined; }) => number
267-
>id(getFoo) : ({ foo = 42 }: { foo?: number | undefined; }) => number
266+
>newGetFoo : ({ foo }: { foo?: number | undefined; }) => number
267+
>id(getFoo) : ({ foo }: { foo?: number | undefined; }) => number
268268
>id : <T>(input: T) => T
269-
>getFoo : ({ foo = 42 }: { foo?: number | undefined; }) => number
269+
>getFoo : ({ foo }: { foo?: number | undefined; }) => number
270270

271271
const newGetFoo2 = id(function getFoo ({ foo = 42 }) {
272-
>newGetFoo2 : ({ foo = 42 }: { foo?: number | undefined; }) => number
273-
>id(function getFoo ({ foo = 42 }) { return foo;}) : ({ foo = 42 }: { foo?: number | undefined; }) => number
272+
>newGetFoo2 : ({ foo }: { foo?: number | undefined; }) => number
273+
>id(function getFoo ({ foo = 42 }) { return foo;}) : ({ foo }: { foo?: number | undefined; }) => number
274274
>id : <T>(input: T) => T
275-
>function getFoo ({ foo = 42 }) { return foo;} : ({ foo = 42 }: { foo?: number | undefined; }) => number
276-
>getFoo : ({ foo = 42 }: { foo?: number | undefined; }) => number
275+
>function getFoo ({ foo = 42 }) { return foo;} : ({ foo }: { foo?: number | undefined; }) => number
276+
>getFoo : ({ foo }: { foo?: number | undefined; }) => number
277277
>foo : number
278278
>42 : 42
279279

0 commit comments

Comments
 (0)