Add base constraint completions for JSX attributes (#35803)

* Add base constraint completions for JSX attributes

* Add test for class components
This commit is contained in:
Andrew Branch
2019-12-23 14:06:46 -06:00
committed by GitHub
parent d66e959a74
commit bed7c89354
3 changed files with 48 additions and 11 deletions

View File

@@ -21213,14 +21213,14 @@ namespace ts {
function getContextualTypeForArgumentAtIndex(callTarget: CallLikeExpression, argIndex: number, contextFlags?: ContextFlags): Type {
// If we're already in the process of resolving the given signature, don't resolve again as
// that could cause infinite recursion. Instead, return anySignature.
const signature = getNodeLinks(callTarget).resolvedSignature === resolvingSignature ? resolvingSignature : getResolvedSignature(callTarget);
let signature = getNodeLinks(callTarget).resolvedSignature === resolvingSignature ? resolvingSignature : getResolvedSignature(callTarget);
if (contextFlags && contextFlags & ContextFlags.BaseConstraint && signature.target && !hasTypeArguments(callTarget)) {
signature = getBaseSignature(signature.target);
}
if (isJsxOpeningLikeElement(callTarget) && argIndex === 0) {
return getEffectiveFirstArgumentForJsxSignature(signature, callTarget);
}
if (contextFlags && contextFlags & ContextFlags.BaseConstraint && signature.target && !hasTypeArguments(callTarget)) {
const baseSignature = getBaseSignature(signature.target);
return getTypeAtPosition(baseSignature, argIndex);
}
return getTypeAtPosition(signature, argIndex);
}
@@ -21645,7 +21645,7 @@ namespace ts {
return getContextualTypeForJsxAttribute(<JsxAttribute | JsxSpreadAttribute>parent);
case SyntaxKind.JsxOpeningElement:
case SyntaxKind.JsxSelfClosingElement:
return getContextualJsxElementAttributesType(<JsxOpeningLikeElement>parent);
return getContextualJsxElementAttributesType(<JsxOpeningLikeElement>parent, contextFlags);
}
return undefined;
}
@@ -21655,18 +21655,20 @@ namespace ts {
return ancestor && ancestor.inferenceContext!;
}
function getContextualJsxElementAttributesType(node: JsxOpeningLikeElement) {
if (isJsxOpeningElement(node) && node.parent.contextualType) {
function getContextualJsxElementAttributesType(node: JsxOpeningLikeElement, contextFlags?: ContextFlags) {
if (isJsxOpeningElement(node) && node.parent.contextualType && contextFlags !== ContextFlags.BaseConstraint) {
// Contextually applied type is moved from attributes up to the outer jsx attributes so when walking up from the children they get hit
// _However_ to hit them from the _attributes_ we must look for them here; otherwise we'll used the declared type
// (as below) instead!
return node.parent.contextualType;
}
return getContextualTypeForArgumentAtIndex(node, 0);
return getContextualTypeForArgumentAtIndex(node, 0, contextFlags);
}
function getEffectiveFirstArgumentForJsxSignature(signature: Signature, node: JsxOpeningLikeElement) {
return getJsxReferenceKind(node) !== JsxReferenceKind.Component ? getJsxPropsTypeFromCallSignature(signature, node) : getJsxPropsTypeFromClassType(signature, node);
return getJsxReferenceKind(node) !== JsxReferenceKind.Component
? getJsxPropsTypeFromCallSignature(signature, node)
: getJsxPropsTypeFromClassType(signature, node);
}
function getJsxPropsTypeFromCallSignature(sig: Signature, context: JsxOpeningLikeElement) {