Skip to content

Commit 0290e20

Browse files
author
Kanchalai Tanglertsampan
committed
Get completion of JSX attributes type when tag name is a property access expression
1 parent ef25b25 commit 0290e20

File tree

2 files changed

+59
-6
lines changed

2 files changed

+59
-6
lines changed

src/services/completions.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -898,8 +898,8 @@ namespace ts.Completions {
898898
return undefined;
899899
}
900900

901-
const { parent, kind } = contextToken;
902-
if (kind === SyntaxKind.DotToken) {
901+
let parent = contextToken.parent;
902+
if (contextToken.kind === SyntaxKind.DotToken) {
903903
if (parent.kind === SyntaxKind.PropertyAccessExpression) {
904904
node = (<PropertyAccessExpression>contextToken.parent).expression;
905905
isRightOfDot = true;
@@ -915,16 +915,24 @@ namespace ts.Completions {
915915
}
916916
}
917917
else if (sourceFile.languageVariant === LanguageVariant.JSX) {
918-
switch (contextToken.parent.kind) {
918+
// <UI.Test /* completion position */ />
919+
// If the tagname is a property access expression, we will then walk up to the top most of property access expression.
920+
// Then, try to get a JSX container and its associated attributes type.
921+
if (parent && parent.kind === SyntaxKind.PropertyAccessExpression) {
922+
contextToken = parent;
923+
parent = parent.parent;
924+
}
925+
926+
switch (parent.kind) {
919927
case SyntaxKind.JsxClosingElement:
920-
if (kind === SyntaxKind.SlashToken) {
928+
if (contextToken.kind === SyntaxKind.SlashToken) {
921929
isStartingCloseTag = true;
922930
location = contextToken;
923931
}
924932
break;
925933

926934
case SyntaxKind.BinaryExpression:
927-
if (!((contextToken.parent as BinaryExpression).left.flags & NodeFlags.ThisNodeHasError)) {
935+
if (!((parent as BinaryExpression).left.flags & NodeFlags.ThisNodeHasError)) {
928936
// It has a left-hand side, so we're not in an opening JSX tag.
929937
break;
930938
}
@@ -933,7 +941,7 @@ namespace ts.Completions {
933941
case SyntaxKind.JsxSelfClosingElement:
934942
case SyntaxKind.JsxElement:
935943
case SyntaxKind.JsxOpeningElement:
936-
if (kind === SyntaxKind.LessThanToken) {
944+
if (contextToken.kind === SyntaxKind.LessThanToken) {
937945
isRightOfOpenTag = true;
938946
location = contextToken;
939947
}
@@ -1401,6 +1409,7 @@ namespace ts.Completions {
14011409
case SyntaxKind.LessThanSlashToken:
14021410
case SyntaxKind.SlashToken:
14031411
case SyntaxKind.Identifier:
1412+
case SyntaxKind.PropertyAccessExpression:
14041413
case SyntaxKind.JsxAttributes:
14051414
case SyntaxKind.JsxAttribute:
14061415
case SyntaxKind.JsxSpreadAttribute:
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/// <reference path='fourslash.ts' />
2+
//@module: commonjs
3+
//@jsx: preserve
4+
5+
//// declare module JSX {
6+
//// interface Element { }
7+
//// interface IntrinsicElements {
8+
//// }
9+
//// interface ElementAttributesProperty { props; }
10+
//// }
11+
12+
//@Filename: exporter.tsx
13+
//// export class Thing { props: { ONE: string; TWO: number } }
14+
//// export module M {
15+
//// export declare function SFCComp(props: { Three: number; Four: string }): JSX.Element;
16+
//// }
17+
18+
//@Filename: file.tsx
19+
//// import * as Exp from './exporter';
20+
//// var x1 = <Exp.Thing /*1*/ />;
21+
//// var x2 = <Exp.M.SFCComp /*2*/ />;
22+
//// var x3 = <Exp.Thing /*3*/ ></Exp.Thing>;
23+
//// var x4 = <Exp.M.SFCComp /*4*/ ></Exp.M.SFCComp>;
24+
25+
26+
goTo.marker("1");
27+
verify.completionListCount(2);
28+
verify.completionListContains('ONE');
29+
verify.completionListContains('TWO');
30+
31+
goTo.marker("2");
32+
verify.completionListCount(2);
33+
verify.completionListContains("Three");
34+
verify.completionListContains("Four");
35+
36+
goTo.marker("3");
37+
verify.completionListCount(2);
38+
verify.completionListContains('ONE');
39+
verify.completionListContains('TWO');
40+
41+
goTo.marker("4");
42+
verify.completionListCount(2);
43+
verify.completionListContains("Three");
44+
verify.completionListContains("Four");

0 commit comments

Comments
 (0)