Skip to content

Commit 4b69e13

Browse files
zardoyandrewbranch
andauthored
Basic JSX attribute snippet fixes (#51855)
Co-authored-by: Andrew Branch <[email protected]>
1 parent fdb5dc5 commit 4b69e13

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/services/completions.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,8 +2875,10 @@ function getCompletionData(
28752875

28762876
case SyntaxKind.JsxExpression:
28772877
case SyntaxKind.JsxSpreadAttribute:
2878-
// For `<div foo={true} [||] ></div>`, `parent` will be `{true}` and `previousToken` will be `}`
2879-
if (previousToken.kind === SyntaxKind.CloseBraceToken && currentToken.kind === SyntaxKind.GreaterThanToken) {
2878+
// First case is for `<div foo={true} [||] />` or `<div foo={true} [||] ></div>`,
2879+
// `parent` will be `{true}` and `previousToken` will be `}`
2880+
// Second case is for `<div foo={true} t[||] ></div>`
2881+
if (previousToken.kind === SyntaxKind.CloseBraceToken || (previousToken.kind === SyntaxKind.Identifier || previousToken.parent.kind === SyntaxKind.JsxAttribute)) {
28802882
isJsxIdentifierExpected = true;
28812883
}
28822884
break;
@@ -5215,4 +5217,3 @@ function toUpperCharCode(charCode: number) {
52155217
}
52165218
return charCode;
52175219
}
5218-

tests/cases/fourslash/jsxAttributeSnippetCompletionClosed.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//@Filename: file.tsx
33
////interface NestedInterface {
44
//// Foo: NestedInterface;
5-
//// (props: {className?: string}): any;
5+
//// (props: {className?: string, onClick?: () => void}): any;
66
////}
77
////
88
////declare const Foo: NestedInterface;
@@ -52,12 +52,27 @@
5252
////function fn11() {
5353
//// return <Foo something cla/*11*/ />
5454
////}
55+
////function fn12() {
56+
//// return <Foo something={false} cla/*12*/ />
57+
////}
58+
////function fn13() {
59+
//// return <Foo something={false} /*13*/ foo />
60+
////}
61+
////function fn14() {
62+
//// return <Foo something={false} cla/*14*/ foo />
63+
////}
64+
////function fn15() {
65+
//// return <Foo onC/*15*/="" />
66+
////}
67+
////function fn16() {
68+
//// return <Foo something={false} onC/*16*/="" foo />
69+
////}
5570

5671
var preferences: FourSlashInterface.UserPreferences = {
5772
jsxAttributeCompletionStyle: "braces",
5873
includeCompletionsWithSnippetText: true,
5974
includeCompletionsWithInsertText: true,
60-
};
75+
};
6176

6277
verify.completions(
6378
{ marker: "1", preferences, includes: { name: "className", insertText: "className={$1}", text: "(property) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } },
@@ -71,4 +86,9 @@ verify.completions(
7186
{ marker: "9", preferences, includes: { name: "className", insertText: "className={$1}", text: "(property) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } },
7287
{ marker: "10", preferences, includes: { name: "className", insertText: "className={$1}", text: "(property) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } },
7388
{ marker: "11", preferences, includes: { name: "className", insertText: "className={$1}", text: "(property) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } },
89+
{ marker: "12", preferences, includes: { name: "className", insertText: "className={$1}", text: "(property) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } },
90+
{ marker: "13", preferences, includes: { name: "className", insertText: "className={$1}", text: "(property) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } },
91+
{ marker: "14", preferences, includes: { name: "className", insertText: "className={$1}", text: "(property) className?: string", isSnippet: true, sortText: completion.SortText.OptionalMember } },
92+
{ marker: "15", preferences, includes: { name: "onClick", insertText: undefined, text: "(property) onClick?: () => void", isSnippet: undefined, sortText: completion.SortText.OptionalMember } },
93+
{ marker: "16", preferences, includes: { name: "onClick", insertText: undefined, text: "(property) onClick?: () => void", isSnippet: undefined, sortText: completion.SortText.OptionalMember } },
7494
)

0 commit comments

Comments
 (0)