Skip to content

Commit a088cf4

Browse files
author
Kanchalai Tanglertsampan
committed
Address code review: fix up comment
1 parent 9c33395 commit a088cf4

File tree

1 file changed

+36
-45
lines changed

1 file changed

+36
-45
lines changed

src/compiler/checker.ts

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3359,8 +3359,8 @@ namespace ts {
33593359
return addOptionality(type, /*optional*/ declaration.questionToken && includeOptionality);
33603360
}
33613361
else if (isJsxAttribute(declaration)) {
3362-
// For JSX Attribute, if it doesn't have initializer, by default the attribute gets true-type.
3363-
// <Elem attr /> is sugar for <Elem attr={true} />
3362+
// if JSX attribute doesn't have initializer, by default the attribute will have boolean value of true.
3363+
// I.e <Elem attr /> is sugar for <Elem attr={true} />
33643364
return trueType;
33653365
}
33663366

@@ -6931,8 +6931,7 @@ namespace ts {
69316931
case SyntaxKind.JsxAttributes:
69326932
return forEach((<JsxAttributes>node).properties, isContextSensitive);
69336933
case SyntaxKind.JsxAttribute:
6934-
// If written as a shorthand (e.g <... attr /> then there is no explicit initializer as it has implicit boolean value of true
6935-
// which is not context sensitvie.
6934+
// If there is no initializer, JSX attribute has a boolean value of true which is not context sensitive.
69366935
return (<JsxAttribute>node).initializer && isContextSensitive((<JsxAttribute>node).initializer);
69376936
case SyntaxKind.JsxExpression:
69386937
return isContextSensitive((<JsxExpression>node).expression);
@@ -7669,7 +7668,7 @@ namespace ts {
76697668
// reasoning about what went wrong.
76707669
Debug.assert(!!errorNode);
76717670
if (isJsxAttributes(errorNode)) {
7672-
// JsxAttributes has an object-literal flag and is underwent same type-assignablity check as normal object-literal.
7671+
// JsxAttributes has an object-literal flag and undergo same type-assignablity check as normal object-literal.
76737672
// However, using an object-literal error message will be very confusing to the users so we give different a message.
76747673
reportError(Diagnostics.Property_0_does_not_exist_on_type_1, symbolToString(prop), typeToString(target));
76757674
}
@@ -11316,9 +11315,9 @@ namespace ts {
1131611315
}
1131711316

1131811317
function getContextualTypeForJsxAttribute(attribute: JsxAttribute | JsxSpreadAttribute) {
11319-
// When we trying to resolve JsxOpeningLikeElement as a stateless function element, we will already give JSXAttributes a contextual type
11320-
// which is a type of the parameter of the signature we are trying out. This is not the case if it is a stateful JSX (i.e ReactComponenet class)
11321-
// So if that is the case, just return the type of the JsxAttribute in such contextual type with out going into resolving of the JsxOpeningLikeElement again
11318+
// When we trying to resolve JsxOpeningLikeElement as a stateless function element, we will already give its attributes a contextual type
11319+
// which is a type of the parameter of the signature we are trying out.
11320+
// If there is no contextual type (e.g. we are trying to resolve stateful component), get attributes type from resolving element's tagName
1132211321
const attributesType = getContextualType(<Expression>attribute.parent) || getAttributesTypeFromJsxOpeningLikeElement(<JsxOpeningLikeElement>attribute.parent.parent);
1132311322

1132411323
if (isJsxAttribute(attribute)) {
@@ -11902,8 +11901,10 @@ namespace ts {
1190211901
}
1190311902

1190411903
/**
11905-
* Get attributes type of the given Jsx opening-like element. The result is from resolving "attributes" property of the opening-like element.
11906-
* @param openingLikeElement a Jsx opening-like element
11904+
* Get attributes type of the JSX opening-like element. The result is from resolving "attributes" property of the opening-like element.
11905+
*
11906+
* @param openingLikeElement a JSX opening-like element
11907+
* @param filter a function to remove attributes that will not participate in checking whether attributes are assignable
1190711908
* @return an anonymous type (similar to the one returned by checkObjectLiteral) in which its properties are attributes property.
1190811909
*/
1190911910
function createJsxAttributesTypeFromAttributesProperty(openingLikeElement: JsxOpeningLikeElement, filter?: (symbol: Symbol) => boolean, contextualMapper?: TypeMapper) {
@@ -11982,36 +11983,35 @@ namespace ts {
1198211983
}
1198311984

1198411985
/**
11985-
* Check JSXAttributes from "attributes" property. This function is used when we are trying to figure out call signature for JSX opening-like element during chooseOverload
11986-
* In "checkApplicableSignatureForJsxOpeningLikeElement", we get type of arguments for potential stateless function by checking
11987-
* the JSX opening-like element attributes property with contextual type.
11986+
* Check attributes property of opening-like element. This function is called during chooseOverload to get call signature of a JSX opening-like element.
11987+
* (See "checkApplicableSignatureForJsxOpeningLikeElement" for how the function is used)
1198811988
* @param node a JSXAttributes to be resolved of its type
1198911989
*/
1199011990
function checkJsxAttributes(node: JsxAttributes, contextualMapper?: TypeMapper) {
1199111991
return createJsxAttributesTypeFromAttributesProperty(node.parent as JsxOpeningLikeElement, /*filter*/ undefined, contextualMapper);
1199211992
}
1199311993

1199411994
/**
11995-
* Check whether the given attributes of JsxOpeningLikeElement is assignable to the tag-name attributes type.
11996-
* Resolve the type of attributes of the openingLikeElement through checking type of tag-name
11997-
* Check assignablity between given attributes property, "attributes" and the target attributes resulted from resolving tag-name
11995+
* Check whether the given attributes of JSX opening-like element is assignable to the tagName attributes.
11996+
* Get the attributes type of the opening-like element through resolving the tagName, "target attributes"
11997+
* Check assignablity between given attributes property, "source attributes", and the "target attributes"
1199811998
* @param openingLikeElement an opening-like JSX element to check its JSXAttributes
1199911999
*/
12000-
function checkJsxAttributesAssignableToTagnameAttributes(openingLikeElement: JsxOpeningLikeElement) {
12000+
function checkJsxAttributesAssignableToTagNameAttributes(openingLikeElement: JsxOpeningLikeElement) {
1200112001
// The function involves following steps:
12002-
// 1. Figure out expected attributes type expected by resolving tag-name of the JSX opening-like element, targetAttributesType.
12003-
// During these steps, we will try to resolve the tag-name as intrinsic name, stateless function, stateful component (in the order)
12002+
// 1. Figure out expected attributes type by resolving tagName of the JSX opening-like element, targetAttributesType.
12003+
// During these steps, we will try to resolve the tagName as intrinsic name, stateless function, stateful component (in the order)
1200412004
// 2. Solved JSX attributes type given by users, sourceAttributesType, which is by resolving "attributes" property of the JSX opening-like element.
1200512005
// 3. Check if the two are assignable to each other
1200612006

12007-
// targetAttributesType is a type of an attributes from resolving tag-name of an opening-like JSX element.
12007+
// targetAttributesType is a type of an attributes from resolving tagName of an opening-like JSX element.
1200812008
const targetAttributesType = isJsxIntrinsicIdentifier(openingLikeElement.tagName) ?
1200912009
getIntrinsicAttributesTypeFromJsxOpeningLikeElement(openingLikeElement) :
1201012010
getCustomJsxElementAttributesType(openingLikeElement, /*shouldIncludeAllStatelessAttributesType*/ false);
1201112011

1201212012
// sourceAttributesType is a type of an attributes properties.
1201312013
// i.e <div attr1={10} attr2="string" />
12014-
// attr1 and attr2 are treated as JSXAttributes attached in the JsxOpeningLikeElement as "attributes". They resolved to be sourceAttributesType.
12014+
// attr1 and attr2 are treated as JSXAttributes attached in the JsxOpeningLikeElement as "attributes".
1201512015
const sourceAttributesType = createJsxAttributesTypeFromAttributesProperty(openingLikeElement,
1201612016
attribute => {
1201712017
return isUnhyphenatedJsxName(attribute.name) || !!(getPropertyOfType(targetAttributesType, attribute.name));
@@ -12141,7 +12141,7 @@ namespace ts {
1214112141
/**
1214212142
* Get JSX attributes type by trying to resolve openingLikeElement as a stateless function component.
1214312143
* Return only attributes type of successfully resolved call signature.
12144-
* This function assumes that the caller handled other possible element type of the JSX element.
12144+
* This function assumes that the caller handled other possible element type of the JSX element (e.g. stateful component)
1214512145
* Unlike tryGetAllJsxStatelessFunctionAttributesType, this function is a default behavior of type-checkers.
1214612146
* @param openingLikeElement a JSX opening-like element to find attributes type
1214712147
* @param elementType a type of the opening-like element. This elementType can't be an union type
@@ -12151,9 +12151,8 @@ namespace ts {
1215112151
function defaultTryGetJsxStatelessFunctionAttributesType(openingLikeElement: JsxOpeningLikeElement, elementType: Type, elemInstanceType: Type, elementClassType?: Type): Type {
1215212152
Debug.assert(!(elementType.flags & TypeFlags.Union));
1215312153
if (!elementClassType || !isTypeAssignableTo(elemInstanceType, elementClassType)) {
12154-
// Is this is a stateless function component? See if its single signature's return type is assignable to the JSX Element Type
1215512154
if (jsxElementType) {
12156-
// We don't call getResolvedSignature because here we have already resolve the type of JSX Element.
12155+
// We don't call getResolvedSignature here because we have already resolve the type of JSX Element.
1215712156
const callSignature = getResolvedJsxStatelessFunctionSignature(openingLikeElement, elementType, /*candidatesOutArray*/ undefined);
1215812157
const callReturnType = callSignature && getReturnTypeOfSignature(callSignature);
1215912158
let paramType = callReturnType && (callSignature.parameters.length === 0 ? emptyObjectType : getTypeOfSymbol(callSignature.parameters[0]));
@@ -12175,7 +12174,6 @@ namespace ts {
1217512174
* Return all attributes type of resolved call signature including candidate signatures.
1217612175
* This function assumes that the caller handled other possible element type of the JSX element.
1217712176
* This function is a behavior used by language service when looking up completion in JSX element.
12178-
*
1217912177
* @param openingLikeElement a JSX opening-like element to find attributes type
1218012178
* @param elementType a type of the opening-like element. This elementType can't be an union type
1218112179
* @param elemInstanceType an element instance type (the result of newing or invoking this tag)
@@ -12382,8 +12380,7 @@ namespace ts {
1238212380

1238312381
/**
1238412382
* Get attributes type of the given custom opening-like JSX element.
12385-
* The function is intended to be called from a function which has handle intrinsic JSX element already.
12386-
*
12383+
* This function is intended to be called from a caller that handles intrinsic JSX element already.
1238712384
* @param node a custom JSX opening-like element
1238812385
*/
1238912386
function getCustomJsxElementAttributesType(node: JsxOpeningLikeElement, shouldIncludeAllStatelessAttributesType: boolean): Type {
@@ -12396,26 +12393,23 @@ namespace ts {
1239612393
}
1239712394

1239812395
/**
12399-
* Get all possible attributes type, especially for an overload stateless function component, of the given JSX opening-like element.
12400-
* This function is called by language service (see: completions-tryGetGlobalSymbols)
12401-
*
12402-
* Because in language service, the given JSX opening-like element may be incomplete and therefore, we can't resolve to exact signature if the element
12403-
* is a stateless function component so the best thing to do is return all attributes type from all overloads.
12404-
*
12396+
* Get all possible attributes type, especially from an overload stateless function component, of the given JSX opening-like element.
12397+
* This function is called by language service (see: completions-tryGetGlobalSymbols).
1240512398
* @param node a JSX opening-like element to get attributes type for
1240612399
*/
1240712400
function getAllAttributesTypeFromJsxOpeningLikeElement(node: JsxOpeningLikeElement): Type {
1240812401
if (isJsxIntrinsicIdentifier(node.tagName)) {
1240912402
return getIntrinsicAttributesTypeFromJsxOpeningLikeElement(node);
1241012403
}
1241112404
else {
12405+
// Because in language service, the given JSX opening-like element may be incomplete and therefore,
12406+
// we can't resolve to exact signature if the element is a stateless function component so the best thing to do is return all attributes type from all overloads.
1241212407
return getCustomJsxElementAttributesType(node, /*shouldIncludeAllStatelessAttributesType*/ true);
1241312408
}
1241412409
}
1241512410

1241612411
/**
1241712412
* Get the attributes type which is the type that indicate which attributes are valid on the given JSXOpeningLikeElement.
12418-
*
1241912413
* @param node a JSXOpeningLikeElement node
1242012414
* @return an attributes type of the given node
1242112415
*/
@@ -12446,7 +12440,7 @@ namespace ts {
1244612440
return jsxElementClassType;
1244712441
}
1244812442

12449-
/// Returns all the properties of the Jsx.IntrinsicElements interface
12443+
// Returns all the properties of the Jsx.IntrinsicElements interface
1245012444
function getJsxIntrinsicTagNames(): Symbol[] {
1245112445
const intrinsics = getJsxType(JsxNames.IntrinsicElements);
1245212446
return intrinsics ? getPropertiesOfType(intrinsics) : emptyArray;
@@ -12484,7 +12478,7 @@ namespace ts {
1248412478
}
1248512479
}
1248612480

12487-
checkJsxAttributesAssignableToTagnameAttributes(node);
12481+
checkJsxAttributesAssignableToTagNameAttributes(node);
1248812482
}
1248912483

1249012484
function checkJsxExpression(node: JsxExpression, contextualMapper?: TypeMapper) {
@@ -13278,8 +13272,6 @@ namespace ts {
1327813272
return undefined;
1327913273
}
1328013274
else if (isJsxOpeningLikeElement(node)) {
13281-
// For a JSX opening-like element, even though we will recheck the attributes again in "checkApplicableSignatureForJsxOpeningLikeElement" to figure out correct arity.
13282-
// We still return it here because when using infer type-argument we still have to getEffectiveArgument in trying to infer type-argument.
1328313275
args = node.attributes.properties.length > 0 ? [node.attributes] : [];
1328413276
}
1328513277
else {
@@ -14087,11 +14079,10 @@ namespace ts {
1408714079
}
1408814080

1408914081
/**
14090-
* This function is similar to getResolvedSignature but exclusively for trying to resolve JSX stateless-function component.
14091-
* The main reason we have to use this function because, the caller of this function will already check the type of openingLikeElement's tagname
14092-
* pass the type as elementType. The elementType can not be a union (as such case should be handled by the caller of this function)
14093-
* At this point, it is still not sure whether the opening-like element is a stateless function component or not.
14094-
*
14082+
* This function is similar to getResolvedSignature but is exclusively for trying to resolve JSX stateless-function component.
14083+
* The main reason we have to use this function instead of getResolvedSignature because, the caller of this function will already check the type of openingLikeElement's tagName
14084+
* and pass the type as elementType. The elementType can not be a union (as such case should be handled by the caller of this function)
14085+
* Note: at this point, we are still not sure whether the opening-like element is a stateless function component or not.
1409514086
* @param openingLikeElement an opening-like JSX element to try to resolve as JSX stateless function
1409614087
* @param elementType an element type of the opneing-like element by checking opening-like element's tagname.
1409714088
* @param candidatesOutArray an array of signature to be filled in by the function. It is passed by signature help in the language service;
@@ -14120,8 +14111,8 @@ namespace ts {
1412014111
}
1412114112

1412214113
/**
14123-
* Try treating a given opening-like element as stateless function component and resolve a tag-name to a function signature.
14124-
* @param openingLikeElement an JsxOpeningLikeElement we want to try resolve its state-less function if possible
14114+
* Try treating a given opening-like element as stateless function component and resolve a tagName to a function signature.
14115+
* @param openingLikeElement an JSX opening-like element we want to try resolve its stateless function if possible
1412514116
* @param elementType a type of the opening-like JSX element, a result of resolving tagName in opening-like element.
1412614117
* @param candidatesOutArray an array of signature to be filled in by the function. It is passed by signature help in the language service;
1412714118
* the function will fill it up with appropriate candidate signatures

0 commit comments

Comments
 (0)