-
Notifications
You must be signed in to change notification settings - Fork 645
Deep clone descendants of reparsed nodes #966
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
16d70d1
Deep clone reparsed nodes
andrewbranch 54e2b4e
Accept baselines
andrewbranch 1e0c4d9
Fix type parameter constraints
andrewbranch 6063160
Delete JSDocTypeExpression.Host
andrewbranch c6b7403
Delete reparse/original map
andrewbranch 354f351
Clone reparsed CommonJS constructs
andrewbranch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,14 +17,18 @@ func (p *Parser) reparseCommonJS(node *ast.Node) { | |
var export *ast.Node | ||
switch kind { | ||
case ast.JSDeclarationKindModuleExports: | ||
export = p.factory.NewJSExportAssignment(bin.Right) | ||
export = p.factory.NewJSExportAssignment(p.factory.DeepCloneNode(bin.Right)) | ||
case ast.JSDeclarationKindExportsProperty: | ||
nodes := p.nodeSlicePool.NewSlice(1) | ||
nodes[0] = p.factory.NewModifier(ast.KindExportKeyword) | ||
nodes[0].Flags = ast.NodeFlagsReparsed | ||
nodes[0].Loc = bin.Loc | ||
// TODO: Name can sometimes be a string literal, so downstream code needs to handle this | ||
export = p.factory.NewCommonJSExport(p.newModifierList(bin.Loc, nodes), ast.GetElementOrPropertyAccessName(bin.Left), bin.Right) | ||
export = p.factory.NewCommonJSExport( | ||
p.newModifierList(bin.Loc, nodes), | ||
p.factory.DeepCloneNode(ast.GetElementOrPropertyAccessName(bin.Left)), | ||
p.factory.DeepCloneNode(bin.Right), | ||
) | ||
} | ||
if export != nil { | ||
export.Flags = ast.NodeFlagsReparsed | ||
|
@@ -63,7 +67,7 @@ func (p *Parser) reparseTags(parent *ast.Node, jsDoc []*ast.Node) { | |
var t *ast.Node | ||
switch typeExpression.Kind { | ||
case ast.KindJSDocTypeExpression: | ||
t = typeExpression.Type() | ||
t = p.factory.DeepCloneNode(typeExpression.Type()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you need to clone every child added to a synthetic node? There are a few up in reparseCommonJS too (eg bin.Left) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, good catch. |
||
case ast.KindJSDocTypeLiteral: | ||
members := p.nodeSlicePool.NewSlice(0) | ||
for _, member := range typeExpression.AsJSDocTypeLiteral().JSDocPropertyTags { | ||
|
@@ -74,7 +78,7 @@ func (p *Parser) reparseTags(parent *ast.Node, jsDoc []*ast.Node) { | |
questionToken.Loc = core.NewTextRange(member.Pos(), member.End()) | ||
questionToken.Flags = p.contextFlags | ast.NodeFlagsReparsed | ||
} | ||
prop := p.factory.NewPropertySignatureDeclaration(nil, member.Name(), questionToken, member.Type(), nil /*initializer*/) | ||
prop := p.factory.NewPropertySignatureDeclaration(nil, p.factory.DeepCloneNode(member.Name()), questionToken, p.factory.DeepCloneNode(member.Type()), nil /*initializer*/) | ||
prop.Loc = member.Loc | ||
prop.Flags = p.contextFlags | ast.NodeFlagsReparsed | ||
members = append(members, prop) | ||
|
@@ -85,16 +89,25 @@ func (p *Parser) reparseTags(parent *ast.Node, jsDoc []*ast.Node) { | |
default: | ||
panic("typedef tag type expression should be a name reference or a type expression" + typeExpression.Kind.String()) | ||
} | ||
typeAlias := p.factory.NewJSTypeAliasDeclaration(modifiers, tag.AsJSDocTypedefTag().Name(), typeParameters, t) | ||
typeAlias := p.factory.NewJSTypeAliasDeclaration(modifiers, p.factory.DeepCloneNode(tag.AsJSDocTypedefTag().Name()), typeParameters, t) | ||
typeAlias.Loc = core.NewTextRange(tag.Pos(), tag.End()) | ||
typeAlias.Flags = p.contextFlags | ast.NodeFlagsReparsed | ||
ast.SetParentInChildren(typeAlias) | ||
p.reparseList = append(p.reparseList, typeAlias) | ||
case ast.KindJSDocImportTag: | ||
importTag := tag.AsJSDocImportTag() | ||
importClause := importTag.ImportClause.Clone(&p.factory) | ||
importClause := p.factory.DeepCloneNode(importTag.ImportClause) | ||
importClause.Flags |= ast.NodeFlagsReparsed | ||
var modifiers *ast.ModifierList | ||
if importTag.Modifiers() != nil { | ||
modifiers = importTag.Modifiers().Clone(&p.factory) | ||
for i, m := range modifiers.Nodes { | ||
modifiers.Nodes[i] = p.factory.DeepCloneNode(m) | ||
modifiers.Nodes[i].Flags |= ast.NodeFlagsReparsed | ||
} | ||
} | ||
importClause.AsImportClause().IsTypeOnly = true | ||
importDeclaration := p.factory.NewJSImportDeclaration(importTag.Modifiers(), importClause, importTag.ModuleSpecifier, importTag.Attributes) | ||
importDeclaration := p.factory.NewJSImportDeclaration(modifiers, importClause, p.factory.DeepCloneNode(importTag.ModuleSpecifier), p.factory.DeepCloneNode(importTag.Attributes)) | ||
importDeclaration.Loc = core.NewTextRange(tag.Pos(), tag.End()) | ||
importDeclaration.Flags = p.contextFlags | ast.NodeFlagsReparsed | ||
p.reparseList = append(p.reparseList, importDeclaration) | ||
|
@@ -108,36 +121,36 @@ func (p *Parser) reparseTags(parent *ast.Node, jsDoc []*ast.Node) { | |
if parent.Kind == ast.KindVariableStatement && parent.AsVariableStatement().DeclarationList != nil { | ||
for _, declaration := range parent.AsVariableStatement().DeclarationList.AsVariableDeclarationList().Declarations.Nodes { | ||
if declaration.AsVariableDeclaration().Type == nil { | ||
declaration.AsVariableDeclaration().Type = p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, declaration) | ||
declaration.AsVariableDeclaration().Type = p.makeNewType(tag.AsJSDocTypeTag().TypeExpression) | ||
break | ||
} | ||
} | ||
} else if parent.Kind == ast.KindVariableDeclaration { | ||
if parent.AsVariableDeclaration().Type == nil { | ||
parent.AsVariableDeclaration().Type = p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, parent) | ||
parent.AsVariableDeclaration().Type = p.makeNewType(tag.AsJSDocTypeTag().TypeExpression) | ||
} | ||
} else if parent.Kind == ast.KindPropertyDeclaration { | ||
declaration := parent.AsPropertyDeclaration() | ||
if declaration.Type == nil { | ||
declaration.Type = p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, parent) | ||
declaration.Type = p.makeNewType(tag.AsJSDocTypeTag().TypeExpression) | ||
} | ||
} else if parent.Kind == ast.KindPropertyAssignment { | ||
prop := parent.AsPropertyAssignment() | ||
prop.Initializer = p.makeNewTypeAssertion(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, nil), prop.Initializer) | ||
prop.Initializer = p.makeNewTypeAssertion(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression), prop.Initializer) | ||
} else if parent.Kind == ast.KindExportAssignment { | ||
export := parent.AsExportAssignment() | ||
export.Expression = p.makeNewTypeAssertion(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, nil), export.Expression) | ||
export.Expression = p.makeNewTypeAssertion(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression), export.Expression) | ||
} else if parent.Kind == ast.KindReturnStatement { | ||
ret := parent.AsReturnStatement() | ||
ret.Expression = p.makeNewTypeAssertion(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, nil), ret.Expression) | ||
ret.Expression = p.makeNewTypeAssertion(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression), ret.Expression) | ||
} else if parent.Kind == ast.KindParenthesizedExpression { | ||
paren := parent.AsParenthesizedExpression() | ||
paren.Expression = p.makeNewTypeAssertion(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, nil), paren.Expression) | ||
paren.Expression = p.makeNewTypeAssertion(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression), paren.Expression) | ||
} else if parent.Kind == ast.KindExpressionStatement && | ||
parent.AsExpressionStatement().Expression.Kind == ast.KindBinaryExpression { | ||
bin := parent.AsExpressionStatement().Expression.AsBinaryExpression() | ||
if ast.GetAssignmentDeclarationKind(bin) != ast.JSDeclarationKindNone { | ||
bin.Right = p.makeNewTypeAssertion(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, nil), bin.Right) | ||
bin.Right = p.makeNewTypeAssertion(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression), bin.Right) | ||
} | ||
} | ||
case ast.KindJSDocTemplateTag: | ||
|
@@ -161,7 +174,7 @@ func (p *Parser) reparseTags(parent *ast.Node, jsDoc []*ast.Node) { | |
jsparam := tag.AsJSDocParameterTag() | ||
if param, ok := findMatchingParameter(fun, jsparam); ok { | ||
if param.Type() == nil { | ||
param.AsParameterDeclaration().Type = p.makeNewType(jsparam.TypeExpression, param) | ||
param.AsParameterDeclaration().Type = p.makeNewType(jsparam.TypeExpression) | ||
if param.AsParameterDeclaration().QuestionToken == nil && | ||
param.AsParameterDeclaration().Initializer == nil && | ||
(jsparam.IsBracketed || jsparam.TypeExpression != nil && jsparam.TypeExpression.Type().Kind == ast.KindJSDocOptionalType) { | ||
|
@@ -175,7 +188,7 @@ func (p *Parser) reparseTags(parent *ast.Node, jsDoc []*ast.Node) { | |
case ast.KindJSDocReturnTag: | ||
if fun, ok := getFunctionLikeHost(parent); ok { | ||
if fun.Type() == nil { | ||
fun.FunctionLikeData().Type = p.makeNewType(tag.AsJSDocReturnTag().TypeExpression, fun) | ||
fun.FunctionLikeData().Type = p.makeNewType(tag.AsJSDocReturnTag().TypeExpression) | ||
} | ||
} | ||
} | ||
|
@@ -209,14 +222,12 @@ func (p *Parser) gatherTypeParameters(j *ast.Node) *ast.NodeList { | |
constraint := tag.AsJSDocTemplateTag().Constraint | ||
for _, tp := range tag.AsJSDocTemplateTag().TypeParameters().Nodes { | ||
typeParameter := tp.AsTypeParameter() | ||
var reparse *ast.Node | ||
if constraint == nil { | ||
reparse = typeParameter.Clone(&p.factory) | ||
} else { | ||
clone := constraint.Type().Clone(&p.factory) | ||
clone.Flags |= ast.NodeFlagsReparsed | ||
reparse = p.factory.NewTypeParameterDeclaration(typeParameter.Modifiers(), typeParameter.Name(), clone, typeParameter.DefaultType) | ||
reparse.Loc = typeParameter.Loc | ||
reparse := p.factory.DeepCloneNode(tp) | ||
reparse.Loc = typeParameter.Loc | ||
if constraint != nil { | ||
constraintClone := p.factory.DeepCloneNode(constraint.Type()) | ||
constraintClone.Flags |= ast.NodeFlagsReparsed | ||
reparse.AsTypeParameter().Constraint = constraintClone | ||
} | ||
reparse.Flags |= ast.NodeFlagsReparsed | ||
typeParameters = append(typeParameters, reparse) | ||
|
@@ -255,25 +266,20 @@ func getFunctionLikeHost(host *ast.Node) (*ast.Node, bool) { | |
} | ||
|
||
func (p *Parser) makeNewTypeAssertion(t *ast.TypeNode, e *ast.Node) *ast.Node { | ||
assert := p.factory.NewTypeAssertion(t, e) | ||
if t.Flags&ast.NodeFlagsReparsed == 0 { | ||
panic("should only pass reparsed type nodes to makeNewTypeAssertion") | ||
} | ||
assert := p.factory.NewTypeAssertion(t, p.factory.DeepCloneNode(e)) | ||
assert.Flags = p.contextFlags | ast.NodeFlagsReparsed | ||
assert.Loc = core.NewTextRange(e.Pos(), e.End()) | ||
return assert | ||
} | ||
|
||
func (p *Parser) makeNewType(typeExpression *ast.TypeNode, host *ast.Node) *ast.Node { | ||
func (p *Parser) makeNewType(typeExpression *ast.TypeNode) *ast.Node { | ||
if typeExpression == nil || typeExpression.Type() == nil { | ||
return nil | ||
} | ||
if typeExpression.AsJSDocTypeExpression().Host == nil { | ||
typeExpression.AsJSDocTypeExpression().Host = host | ||
} else { | ||
panic("JSDoc type expression already has a host: " + typeExpression.AsJSDocTypeExpression().Host.Kind.String()) | ||
} | ||
t := typeExpression.Type().Clone(&p.factory) | ||
t := p.factory.DeepCloneNode(typeExpression.Type()) | ||
t.Flags |= ast.NodeFlagsReparsed | ||
if host != nil { | ||
t.Parent = host | ||
} | ||
return t | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 1 addition & 4 deletions
5
testdata/baselines/reference/submodule/compiler/expandoFunctionContextualTypesJs.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.