FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix JSX linked editing for incomplete property tags with attributes by musatoktas · Pull Request #64643 · microsoft/TypeScript · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .go  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
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
@@ -0,0 +1,57 @@
package fourslash_test

import (
"fmt"
"testing"

"github.com/microsoft/TypeScript/tsc/internal/fourslash"
"github.com/microsoft/TypeScript/tsc/internal/lsp/lsproto"
"github.com/microsoft/TypeScript/tsc/internal/testutil"
)

func TestLinkedEditingJsxTagIncompleteProperty(t *testing.T) {
t.Parallel()
for _, test := range []struct {
name string
extension string
tagName string
closingTag string
attributes string
}{
{"string attribute", "tsx", "A.", "A.", `foo="bar"`},
{"JavaScript", "jsx", "A.", "A.", `foo="bar"`},
{"expression attribute", "tsx", "A.", "A.", `foo={bar}`},
{"hyphenated attribute", "tsx", "A.", "A.", `data-foo="bar"`},
{"namespaced attribute", "tsx", "A.", "A.", `foo:bar="baz"`},
{"keyword attribute", "tsx", "A.", "A.", `default="bar"`},
{"nested property", "tsx", "A.B.", "A.B.", `foo="bar"`},
{"multiline attribute", "tsx", "A.", "A.", "\nfoo=\"bar\""},
{"spread attribute", "tsx", "A.", "A.", `{...props}`},
{"complete property with whitespace", "tsx", "A. B", "A. B", `foo="bar"`},
{"mismatched names", "tsx", "A.", "B.", `foo="bar"`},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
defer testutil.RecoverAndFail(t, "Panic on fourslash test")
content := fmt.Sprintf(`// @allowJs: true
// @Filename: /incompleteProperty.%s
const a = () => </*openStart*/%s/*openEnd*/ /*attribute*/%s><//*closeStart*/%s/*closeEnd*/>;`, test.extension, test.tagName, test.attributes, test.closingTag)
f, done := fourslash.NewFourslash(t, nil /*capabilities*/, content)
defer done()
var ranges []lsproto.Range
if test.tagName == test.closingTag {
ranges = []lsproto.Range{
{Start: f.MarkerByName(t, "openStart").LSPosition, End: f.MarkerByName(t, "openEnd").LSPosition},
{Start: f.MarkerByName(t, "closeStart").LSPosition, End: f.MarkerByName(t, "closeEnd").LSPosition},
}
}
f.VerifyLinkedEditing(t, map[string][]lsproto.Range{
"openStart": ranges,
"openEnd": ranges,
"closeStart": ranges,
"closeEnd": ranges,
"attribute": nil,
})
})
}
}
22 changes: 21 additions & 1 deletion tsc/internal/parser/parser.go
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
Expand Up @@ -5051,11 +5051,31 @@ func (p *Parser) parseJsxElementName() *ast.Expression {
}
expression := initialExpression
for p.parseOptional(ast.KindDotToken) {
expression = p.finishNode(p.factory.NewPropertyAccessExpression(expression, nil, p.parseRightSideOfDot(true /*allowIdentifierNames*/, false /*allowPrivateIdentifiers*/, false /*allowUnicodeEscapeSequenceInIdentifierName*/), ast.NodeFlagsNone), pos)
var name *ast.Node
if p.scanner.TokenStart() > p.nodePos() && tokenIsIdentifierOrKeyword(p.token) && p.lookAhead((*Parser).nextTokenIsJsxAttributeValue) {
// Recover <A. prop="value"> as an incomplete tag name followed by an attribute.
// Consuming prop as the property name would also lose the matching closing tag.
p.parseErrorAt(p.nodePos(), p.nodePos(), diagnostics.Identifier_expected)
name = p.createMissingIdentifier()
} else {
name = p.parseRightSideOfDot(true /*allowIdentifierNames*/, false /*allowPrivateIdentifiers*/, false /*allowUnicodeEscapeSequenceInIdentifierName*/)
}
expression = p.finishNode(p.factory.NewPropertyAccessExpression(expression, nil, name, ast.NodeFlagsNone), pos)
}
return expression
}

func (p *Parser) nextTokenIsJsxAttributeValue() bool {
p.scanJsxIdentifier()
p.nextToken()
if p.token == ast.KindColonToken {
p.nextToken()
p.scanJsxIdentifier()
p.nextToken()
}
return p.token == ast.KindEqualsToken
}

func (p *Parser) parseJsxTagName() *ast.Expression {
pos := p.nodePos()
p.scanJsxIdentifier()
Expand Down

Back | FazBrowse Home | New Git URL