Skip to content

Commit 29a550e

Browse files
committed
add check for invalid completion position
1 parent f4022d3 commit 29a550e

File tree

9 files changed

+316
-20
lines changed

9 files changed

+316
-20
lines changed

internal/ast/ast.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5495,6 +5495,10 @@ func (node *RegularExpressionLiteral) Clone(f NodeFactoryCoercible) *Node {
54955495
return cloneNode(f.AsNodeFactory().NewRegularExpressionLiteral(node.Text), node.AsNode(), f.AsNodeFactory().hooks)
54965496
}
54975497

5498+
func IsRegularExpressionLiteral(node *Node) bool {
5499+
return node.Kind == KindRegularExpressionLiteral
5500+
}
5501+
54985502
// NoSubstitutionTemplateLiteral
54995503

55005504
type NoSubstitutionTemplateLiteral struct {

internal/ast/utilities.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2964,3 +2964,17 @@ func IsTemplateLiteralKind(kind Kind) bool {
29642964
func IsTemplateLiteralToken(node *Node) bool {
29652965
return IsTemplateLiteralKind(node.Kind)
29662966
}
2967+
2968+
func IsUnterminatedNode(node *Node) bool {
2969+
return IsLiteralKind(node.Kind) && IsUnterminatedLiteral(node)
2970+
}
2971+
2972+
// Gets a value indicating whether a class element is either a static or an instance property declaration with an initializer.
2973+
func IsInitializedProperty(member *ClassElement) bool {
2974+
return member.Kind == KindPropertyDeclaration &&
2975+
member.Initializer() != nil
2976+
}
2977+
2978+
func IsJsxOpeningLikeElement(node *Node) bool {
2979+
return IsJsxOpeningElement(node) || IsJsxSelfClosingElement(node)
2980+
}

internal/checker/checker.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8409,7 +8409,7 @@ type CallState struct {
84098409
func (c *Checker) resolveCall(node *ast.Node, signatures []*Signature, candidatesOutArray *[]*Signature, checkMode CheckMode, callChainFlags SignatureFlags, headMessage *diagnostics.Message) *Signature {
84108410
isTaggedTemplate := node.Kind == ast.KindTaggedTemplateExpression
84118411
isDecorator := node.Kind == ast.KindDecorator
8412-
isJsxOpeningOrSelfClosingElement := isJsxOpeningLikeElement(node)
8412+
isJsxOpeningOrSelfClosingElement := ast.IsJsxOpeningLikeElement(node)
84138413
isInstanceof := node.Kind == ast.KindBinaryExpression
84148414
reportErrors := !c.isInferencePartiallyBlocked && candidatesOutArray == nil
84158415
var s CallState
@@ -8715,7 +8715,7 @@ func (c *Checker) hasCorrectArity(node *ast.Node, args []*ast.Node, signature *S
87158715
argCount = c.getDecoratorArgumentCount(node, signature)
87168716
case ast.IsBinaryExpression(node):
87178717
argCount = 1
8718-
case isJsxOpeningLikeElement(node):
8718+
case ast.IsJsxOpeningLikeElement(node):
87198719
callIsIncomplete = node.Attributes().End() == node.End()
87208720
if callIsIncomplete {
87218721
return true
@@ -8835,7 +8835,7 @@ func (c *Checker) checkTypeArguments(signature *Signature, typeArgumentNodes []*
88358835
}
88368836

88378837
func (c *Checker) isSignatureApplicable(node *ast.Node, args []*ast.Node, signature *Signature, relation *Relation, checkMode CheckMode, reportErrors bool, inferenceContext *InferenceContext, diagnosticOutput *[]*ast.Diagnostic) bool {
8838-
if isJsxOpeningLikeElement(node) {
8838+
if ast.IsJsxOpeningLikeElement(node) {
88398839
return c.checkApplicableSignatureForJsxOpeningLikeElement(node, signature, relation, checkMode, reportErrors, diagnosticOutput)
88408840
}
88418841
thisType := c.getThisTypeOfSignature(signature)
@@ -8976,7 +8976,7 @@ func (c *Checker) getEffectiveCheckNode(argument *ast.Node) *ast.Node {
89768976
}
89778977

89788978
func (c *Checker) inferTypeArguments(node *ast.Node, signature *Signature, args []*ast.Node, checkMode CheckMode, context *InferenceContext) []*Type {
8979-
if isJsxOpeningLikeElement(node) {
8979+
if ast.IsJsxOpeningLikeElement(node) {
89808980
return c.inferJsxTypeArguments(node, signature, checkMode, context)
89818981
}
89828982
// If a contextual type is available, infer from that type to the return type of the call expression. For
@@ -26493,7 +26493,7 @@ func (c *Checker) markLinkedReferences(location *ast.Node, hint ReferenceHint, p
2649326493
c.markExportAssignmentAliasReferenced(location)
2649426494
return
2649526495
}
26496-
if isJsxOpeningLikeElement(location) || ast.IsJsxOpeningFragment(location) {
26496+
if ast.IsJsxOpeningLikeElement(location) || ast.IsJsxOpeningFragment(location) {
2649726497
c.markJsxAliasReferenced(location)
2649826498
return
2649926499
}
@@ -26658,7 +26658,7 @@ func (c *Checker) markJsxAliasReferenced(node *ast.Node /*JsxOpeningLikeElement
2665826658
jsxFactoryRefErr := core.IfElse(c.compilerOptions.Jsx == core.JsxEmitReact, diagnostics.This_JSX_tag_requires_0_to_be_in_scope_but_it_could_not_be_found, nil)
2665926659
jsxFactoryNamespace := c.getJsxNamespace(node)
2666026660
jsxFactoryLocation := node
26661-
if isJsxOpeningLikeElement(node) {
26661+
if ast.IsJsxOpeningLikeElement(node) {
2666226662
jsxFactoryLocation = node.TagName()
2666326663
}
2666426664
// allow null as jsxFragmentFactory
@@ -27703,7 +27703,7 @@ func (c *Checker) getContextualTypeForArgumentAtIndex(callTarget *ast.Node, argI
2770327703
} else {
2770427704
signature = c.getResolvedSignature(callTarget, nil, CheckModeNormal)
2770527705
}
27706-
if isJsxOpeningLikeElement(callTarget) && argIndex == 0 {
27706+
if ast.IsJsxOpeningLikeElement(callTarget) && argIndex == 0 {
2770727707
return c.getEffectiveFirstArgumentForJsxSignature(signature, callTarget)
2770827708
}
2770927709
restIndex := len(signature.parameters) - 1
@@ -27957,7 +27957,7 @@ func (c *Checker) getEffectiveCallArguments(node *ast.Node) []*ast.Node {
2795727957
case ast.IsBinaryExpression(node):
2795827958
// Handles instanceof operator
2795927959
return []*ast.Node{node.AsBinaryExpression().Left}
27960-
case isJsxOpeningLikeElement(node):
27960+
case ast.IsJsxOpeningLikeElement(node):
2796127961
if len(node.Attributes().AsJsxAttributes().Properties.Nodes) != 0 || (ast.IsJsxOpeningElement(node) && len(node.Parent.Children().Nodes) != 0) {
2796227962
return []*ast.Node{node.Attributes()}
2796327963
}

internal/checker/jsx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (c *Checker) checkJsxAttributes(node *ast.Node, checkMode CheckMode) *Type
119119
}
120120

121121
func (c *Checker) checkJsxOpeningLikeElementOrOpeningFragment(node *ast.Node) {
122-
isNodeOpeningLikeElement := isJsxOpeningLikeElement(node)
122+
isNodeOpeningLikeElement := ast.IsJsxOpeningLikeElement(node)
123123
if isNodeOpeningLikeElement {
124124
c.checkGrammarJsxElement(node)
125125
}

internal/checker/relater.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2693,7 +2693,7 @@ func (r *Relater) hasExcessProperties(source *Type, target *Type, reportErrors b
26932693
if r.errorNode == nil {
26942694
panic("No errorNode in hasExcessProperties")
26952695
}
2696-
if ast.IsJsxAttributes(r.errorNode) || isJsxOpeningLikeElement(r.errorNode) || isJsxOpeningLikeElement(r.errorNode.Parent) {
2696+
if ast.IsJsxAttributes(r.errorNode) || ast.IsJsxOpeningLikeElement(r.errorNode) || ast.IsJsxOpeningLikeElement(r.errorNode.Parent) {
26972697
// JsxAttributes has an object-literal flag and undergo same type-assignablity check as normal object-literal.
26982698
// However, using an object-literal error message will be very confusing to the users so we give different a message.
26992699
if prop.ValueDeclaration != nil && ast.IsJsxAttribute(prop.ValueDeclaration) && ast.GetSourceFileOfNode(r.errorNode) == ast.GetSourceFileOfNode(prop.ValueDeclaration.Name()) {

internal/checker/utilities.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1238,15 +1238,6 @@ func reverseAccessKind(a AccessKind) AccessKind {
12381238
panic("Unhandled case in reverseAccessKind")
12391239
}
12401240

1241-
func isJsxOpeningLikeElement(node *ast.Node) bool {
1242-
return ast.IsJsxOpeningElement(node) || ast.IsJsxSelfClosingElement(node)
1243-
}
1244-
1245-
// Deprecated in favor of `ast.IsObjectLiteralElement`
1246-
func isObjectLiteralElementLike(node *ast.Node) bool {
1247-
return ast.IsObjectLiteralElement(node)
1248-
}
1249-
12501241
func isInfinityOrNaNString(name string) bool {
12511242
return name == "Infinity" || name == "-Infinity" || name == "NaN"
12521243
}
@@ -1355,7 +1346,7 @@ func isCallChain(node *ast.Node) bool {
13551346
}
13561347

13571348
func (c *Checker) callLikeExpressionMayHaveTypeArguments(node *ast.Node) bool {
1358-
return isCallOrNewExpression(node) || ast.IsTaggedTemplateExpression(node) || isJsxOpeningLikeElement(node)
1349+
return isCallOrNewExpression(node) || ast.IsTaggedTemplateExpression(node) || ast.IsJsxOpeningLikeElement(node)
13591350
}
13601351

13611352
func isSuperCall(n *ast.Node) bool {

internal/core/text.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ func (t TextRange) ContainsInclusive(pos int) bool {
4343
return pos >= int(t.pos) && pos <= int(t.end)
4444
}
4545

46+
func (t TextRange) ContainsExclusive(pos int) bool {
47+
return int(t.pos) < pos && pos < int(t.end)
48+
}
49+
4650
func (t TextRange) WithPos(pos int) TextRange {
4751
return TextRange{pos: TextPos(pos), end: t.end}
4852
}

0 commit comments

Comments
 (0)