-
Notifications
You must be signed in to change notification settings - Fork 648
Add this/satisfies tag #1157
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
Add this/satisfies tag #1157
Changes from all commits
67d8c27
f79f58d
937bf58
31301e7
e910051
f888513
ffa002e
241d2d8
6f85df0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -224,10 +224,10 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node) | |
} | ||
case ast.KindReturnStatement: | ||
ret := parent.AsReturnStatement() | ||
ret.Expression = p.makeNewTypeAssertion(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, nil), ret.Expression) | ||
ret.Expression = p.makeNewCast(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, nil), ret.Expression, true /*isAssertion*/) | ||
case ast.KindParenthesizedExpression: | ||
paren := parent.AsParenthesizedExpression() | ||
paren.Expression = p.makeNewTypeAssertion(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, nil), paren.Expression) | ||
paren.Expression = p.makeNewCast(p.makeNewType(tag.AsJSDocTypeTag().TypeExpression, nil), paren.Expression, true /*isAssertion*/) | ||
case ast.KindExpressionStatement: | ||
if parent.AsExpressionStatement().Expression.Kind == ast.KindBinaryExpression { | ||
bin := parent.AsExpressionStatement().Expression.AsBinaryExpression() | ||
|
@@ -236,6 +236,11 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node) | |
} | ||
} | ||
} | ||
case ast.KindJSDocSatisfiesTag: | ||
if parent.Kind == ast.KindParenthesizedExpression { | ||
paren := parent.AsParenthesizedExpression() | ||
paren.Expression = p.makeNewCast(p.makeNewType(tag.AsJSDocSatisfiesTag().TypeExpression, nil), paren.Expression, false /*isAssertion*/) | ||
} | ||
case ast.KindJSDocTemplateTag: | ||
if fun, ok := getFunctionLikeHost(parent); ok { | ||
if fun.TypeParameters() == nil { | ||
|
@@ -266,6 +271,31 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node) | |
} | ||
} | ||
} | ||
case ast.KindJSDocThisTag: | ||
if fun, ok := getFunctionLikeHost(parent); ok { | ||
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. What's this supposed to do for function-like-expressions that don't have a 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. Copilot already noticed this and I replied that there's already an error logged so it's OK with me to have wonky semantics in a file with errors |
||
params := fun.Parameters() | ||
if len(params) == 0 || params[0].Name().Kind != ast.KindThisKeyword { | ||
thisParam := p.factory.NewParameterDeclaration( | ||
nil, /* decorators */ | ||
nil, /* modifiers */ | ||
p.factory.NewIdentifier("this"), | ||
nil, /* questionToken */ | ||
nil, /* type */ | ||
nil, /* initializer */ | ||
) | ||
thisParam.AsParameterDeclaration().Type = p.makeNewType(tag.AsJSDocThisTag().TypeExpression, thisParam) | ||
thisParam.Loc = tag.AsJSDocThisTag().TagName.Loc | ||
thisParam.Flags = p.contextFlags | ast.NodeFlagsReparsed | ||
|
||
newParams := p.nodeSlicePool.NewSlice(len(params) + 1) | ||
newParams[0] = thisParam | ||
for i, param := range params { | ||
newParams[i+1] = param | ||
} | ||
|
||
fun.FunctionLikeData().Parameters = p.newNodeList(thisParam.Loc, newParams) | ||
} | ||
} | ||
case ast.KindJSDocReturnTag: | ||
if fun, ok := getFunctionLikeHost(parent); ok { | ||
if fun.Type() == nil { | ||
|
@@ -351,7 +381,6 @@ func (p *Parser) reparseHosted(tag *ast.Node, parent *ast.Node, jsDoc *ast.Node) | |
} | ||
} | ||
} | ||
// !!! other attached tags (@this, @satisfies) support goes here | ||
} | ||
|
||
func (p *Parser) makeQuestionIfOptional(parameter *ast.JSDocParameterTag) *ast.Node { | ||
|
@@ -398,8 +427,13 @@ func getFunctionLikeHost(host *ast.Node) (*ast.Node, bool) { | |
return nil, false | ||
} | ||
|
||
func (p *Parser) makeNewTypeAssertion(t *ast.TypeNode, e *ast.Node) *ast.Node { | ||
assert := p.factory.NewTypeAssertion(t, e) | ||
func (p *Parser) makeNewCast(t *ast.TypeNode, e *ast.Node, isAssertion bool) *ast.Node { | ||
var assert *ast.Node | ||
if isAssertion { | ||
assert = p.factory.NewAsExpression(e, t) | ||
} else { | ||
assert = p.factory.NewSatisfiesExpression(e, t) | ||
} | ||
assert.Flags = p.contextFlags | ast.NodeFlagsReparsed | ||
assert.Loc = core.NewTextRange(e.Pos(), e.End()) | ||
return assert | ||
|
Uh oh!
There was an error while loading. Please reload this page.