@@ -299,7 +299,7 @@ namespace ts {
299
299
resolveName(name, location, meaning, excludeGlobals) {
300
300
return resolveName(location, escapeLeadingUnderscores(name), meaning, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined, /*isUse*/ false, excludeGlobals);
301
301
},
302
- getJsxNamespace: () => unescapeLeadingUnderscores(getJsxNamespace()),
302
+ getJsxNamespace: n => unescapeLeadingUnderscores(getJsxNamespace(n )),
303
303
getAccessibleSymbolChain,
304
304
getTypePredicateOfSignature: getTypePredicateOfSignature as (signature: Signature) => TypePredicate, // TODO: GH#18217
305
305
resolveExternalModuleSymbol,
@@ -766,7 +766,23 @@ namespace ts {
766
766
}
767
767
}
768
768
769
- function getJsxNamespace(): __String {
769
+ function getJsxNamespace(location: Node | undefined): __String {
770
+ if (location) {
771
+ const file = getSourceFileOfNode(location);
772
+ if (file) {
773
+ if (file.localJsxNamespace) {
774
+ return file.localJsxNamespace;
775
+ }
776
+ const jsxPragma = file.pragmas.get("jsx");
777
+ if (jsxPragma) {
778
+ const chosenpragma: any = isArray(jsxPragma) ? jsxPragma[0] : jsxPragma; // TODO: GH#18217
779
+ file.localJsxFactory = parseIsolatedEntityName(chosenpragma.arguments.factory, languageVersion);
780
+ if (file.localJsxFactory) {
781
+ return file.localJsxNamespace = getFirstIdentifier(file.localJsxFactory).escapedText;
782
+ }
783
+ }
784
+ }
785
+ }
770
786
if (!_jsxNamespace) {
771
787
_jsxNamespace = "React" as __String;
772
788
if (compilerOptions.jsxFactory) {
@@ -2099,7 +2115,7 @@ namespace ts {
2099
2115
else if (noImplicitAny && moduleNotFoundError) {
2100
2116
let errorInfo = resolvedModule!.packageId && chainDiagnosticMessages(/*details*/ undefined,
2101
2117
Diagnostics.Try_npm_install_types_Slash_0_if_it_exists_or_add_a_new_declaration_d_ts_file_containing_declare_module_0,
2102
- resolvedModule!.packageId!.name); // TODO: GH#18217
2118
+ getMangledNameForScopedPackage( resolvedModule!.packageId!.name) ); // TODO: GH#18217
2103
2119
errorInfo = chainDiagnosticMessages(errorInfo,
2104
2120
Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type,
2105
2121
moduleReference,
@@ -4031,7 +4047,7 @@ namespace ts {
4031
4047
parentType = getNonNullableType(parentType);
4032
4048
}
4033
4049
const propType = getTypeOfPropertyOfType(parentType, text);
4034
- const declaredType = propType && getApparentTypeForLocation (propType, declaration.name);
4050
+ const declaredType = propType && getConstraintForLocation (propType, declaration.name);
4035
4051
type = declaredType && getFlowTypeOfReference(declaration, declaredType) ||
4036
4052
isNumericLiteralName(text) && getIndexTypeOfType(parentType, IndexKind.Number) ||
4037
4053
getIndexTypeOfType(parentType, IndexKind.String);
@@ -6159,17 +6175,29 @@ namespace ts {
6159
6175
return getConstraintOfDistributiveConditionalType(type) || getDefaultConstraintOfConditionalType(type);
6160
6176
}
6161
6177
6162
- function getBaseConstraintOfType (type: Type): Type | undefined {
6178
+ function getBaseConstraintOfInstantiableNonPrimitiveUnionOrIntersection (type: Type) {
6163
6179
if (type.flags & (TypeFlags.InstantiableNonPrimitive | TypeFlags.UnionOrIntersection)) {
6164
6180
const constraint = getResolvedBaseConstraint(<InstantiableType | UnionOrIntersectionType>type);
6165
6181
if (constraint !== noConstraintType && constraint !== circularConstraintType) {
6166
6182
return constraint;
6167
6183
}
6168
6184
}
6169
- else if (type.flags & TypeFlags.Index) {
6185
+ }
6186
+
6187
+ function getBaseConstraintOfType(type: Type): Type | undefined {
6188
+ const constraint = getBaseConstraintOfInstantiableNonPrimitiveUnionOrIntersection(type);
6189
+ if (!constraint && type.flags & TypeFlags.Index) {
6170
6190
return stringType;
6171
6191
}
6172
- return undefined;
6192
+ return constraint;
6193
+ }
6194
+
6195
+ /**
6196
+ * This is similar to `getBaseConstraintOfType` except it returns the input type if there's no base constraint, instead of `undefined`
6197
+ * It also doesn't map indexes to `string`, as where this is used this would be unneeded (and likely undesirable)
6198
+ */
6199
+ function getBaseConstraintOrType(type: Type) {
6200
+ return getBaseConstraintOfType(type) || type;
6173
6201
}
6174
6202
6175
6203
function hasNonCircularBaseConstraint(type: InstantiableType): boolean {
@@ -11971,7 +11999,7 @@ namespace ts {
11971
11999
function getFlowCacheKey(node: Node): string | undefined {
11972
12000
if (node.kind === SyntaxKind.Identifier) {
11973
12001
const symbol = getResolvedSymbol(<Identifier>node);
11974
- return symbol !== unknownSymbol ? (isApparentTypePosition (node) ? "@" : "") + getSymbolId(symbol) : undefined;
12002
+ return symbol !== unknownSymbol ? (isConstraintPosition (node) ? "@" : "") + getSymbolId(symbol) : undefined;
11975
12003
}
11976
12004
if (node.kind === SyntaxKind.ThisKeyword) {
11977
12005
return "0";
@@ -13336,8 +13364,8 @@ namespace ts {
13336
13364
return annotationIncludesUndefined ? getTypeWithFacts(declaredType, TypeFacts.NEUndefined) : declaredType;
13337
13365
}
13338
13366
13339
- function isApparentTypePosition (node: Node) {
13340
- const { parent } = node;
13367
+ function isConstraintPosition (node: Node) {
13368
+ const parent = node.parent ;
13341
13369
return parent.kind === SyntaxKind.PropertyAccessExpression ||
13342
13370
parent.kind === SyntaxKind.CallExpression && (<CallExpression>parent).expression === node ||
13343
13371
parent.kind === SyntaxKind.ElementAccessExpression && (<ElementAccessExpression>parent).expression === node ||
@@ -13349,13 +13377,13 @@ namespace ts {
13349
13377
return type.flags & TypeFlags.InstantiableNonPrimitive && maybeTypeOfKind(getBaseConstraintOfType(type) || emptyObjectType, TypeFlags.Nullable);
13350
13378
}
13351
13379
13352
- function getApparentTypeForLocation (type: Type, node: Node) {
13380
+ function getConstraintForLocation (type: Type, node: Node) {
13353
13381
// When a node is the left hand expression of a property access, element access, or call expression,
13354
13382
// and the type of the node includes type variables with constraints that are nullable, we fetch the
13355
13383
// apparent type of the node *before* performing control flow analysis such that narrowings apply to
13356
13384
// the constraint type.
13357
- if (isApparentTypePosition (node) && forEachType(type, typeHasNullableConstraint)) {
13358
- return mapType(getWidenedType(type), getApparentType );
13385
+ if (isConstraintPosition (node) && forEachType(type, typeHasNullableConstraint)) {
13386
+ return mapType(getWidenedType(type), getBaseConstraintOrType );
13359
13387
}
13360
13388
return type;
13361
13389
}
@@ -13443,7 +13471,7 @@ namespace ts {
13443
13471
checkCollisionWithCapturedNewTargetVariable(node, node);
13444
13472
checkNestedBlockScopedBinding(node, symbol);
13445
13473
13446
- const type = getApparentTypeForLocation (getTypeOfSymbol(localOrExportSymbol), node);
13474
+ const type = getConstraintForLocation (getTypeOfSymbol(localOrExportSymbol), node);
13447
13475
const assignmentKind = getAssignmentTargetKind(node);
13448
13476
13449
13477
if (assignmentKind) {
@@ -15109,8 +15137,10 @@ namespace ts {
15109
15137
function checkJsxFragment(node: JsxFragment, checkMode: CheckMode | undefined): Type {
15110
15138
checkJsxOpeningLikeElementOrOpeningFragment(node.openingFragment, checkMode);
15111
15139
15112
- if (compilerOptions.jsx === JsxEmit.React && compilerOptions.jsxFactory) {
15113
- error(node, Diagnostics.JSX_fragment_is_not_supported_when_using_jsxFactory);
15140
+ if (compilerOptions.jsx === JsxEmit.React && (compilerOptions.jsxFactory || getSourceFileOfNode(node).pragmas.has("jsx"))) {
15141
+ error(node, compilerOptions.jsxFactory
15142
+ ? Diagnostics.JSX_fragment_is_not_supported_when_using_jsxFactory
15143
+ : Diagnostics.JSX_fragment_is_not_supported_when_using_an_inline_JSX_factory_pragma);
15114
15144
}
15115
15145
15116
15146
return getJsxGlobalElementType() || anyType;
@@ -15737,7 +15767,7 @@ namespace ts {
15737
15767
// The reactNamespace/jsxFactory's root symbol should be marked as 'used' so we don't incorrectly elide its import.
15738
15768
// And if there is no reactNamespace/jsxFactory's symbol in scope when targeting React emit, we should issue an error.
15739
15769
const reactRefErr = diagnostics && compilerOptions.jsx === JsxEmit.React ? Diagnostics.Cannot_find_name_0 : undefined;
15740
- const reactNamespace = getJsxNamespace();
15770
+ const reactNamespace = getJsxNamespace(node );
15741
15771
const reactLocation = isNodeOpeningLikeElement ? (<JsxOpeningLikeElement>node).tagName : node;
15742
15772
const reactSym = resolveName(reactLocation, reactNamespace, SymbolFlags.Value, reactRefErr, reactNamespace, /*isUse*/ true);
15743
15773
if (reactSym) {
@@ -15917,7 +15947,7 @@ namespace ts {
15917
15947
15918
15948
// Referencing abstract properties within their own constructors is not allowed
15919
15949
if ((flags & ModifierFlags.Abstract) && isThisProperty(node) && symbolHasNonMethodDeclaration(prop)) {
15920
- const declaringClassDeclaration = <ClassLikeDeclaration> getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop)!);
15950
+ const declaringClassDeclaration = getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop)!);
15921
15951
if (declaringClassDeclaration && isNodeWithinConstructorOfClass(node, declaringClassDeclaration)) {
15922
15952
error(errorNode, Diagnostics.Abstract_property_0_in_class_1_cannot_be_accessed_in_the_constructor, symbolToString(prop), getTextOfIdentifierOrLiteral(declaringClassDeclaration.name!)); // TODO: GH#18217
15923
15953
return false;
@@ -15933,7 +15963,7 @@ namespace ts {
15933
15963
15934
15964
// Private property is accessible if the property is within the declaring class
15935
15965
if (flags & ModifierFlags.Private) {
15936
- const declaringClassDeclaration = <ClassLikeDeclaration> getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop)!);
15966
+ const declaringClassDeclaration = getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop)!)! ;
15937
15967
if (!isNodeWithinClass(node, declaringClassDeclaration)) {
15938
15968
error(errorNode, Diagnostics.Property_0_is_private_and_only_accessible_within_class_1, symbolToString(prop), typeToString(getDeclaringClass(prop)!));
15939
15969
return false;
@@ -16064,7 +16094,7 @@ namespace ts {
16064
16094
return unknownType;
16065
16095
}
16066
16096
}
16067
- propType = getApparentTypeForLocation (getTypeOfSymbol(prop), node);
16097
+ propType = getConstraintForLocation (getTypeOfSymbol(prop), node);
16068
16098
}
16069
16099
// Only compute control flow type if this is a property access expression that isn't an
16070
16100
// assignment target, and the referenced property was declared as a variable, property,
@@ -17657,7 +17687,7 @@ namespace ts {
17657
17687
return true;
17658
17688
}
17659
17689
17660
- const declaringClassDeclaration = <ClassLikeDeclaration> getClassLikeDeclarationOfSymbol(declaration.parent.symbol!);
17690
+ const declaringClassDeclaration = getClassLikeDeclarationOfSymbol(declaration.parent.symbol!)! ;
17661
17691
const declaringClass = <InterfaceType>getDeclaredTypeOfSymbol(declaration.parent.symbol!);
17662
17692
17663
17693
// A private or protected constructor can only be instantiated within its own class (or a subclass, for protected)
@@ -23138,7 +23168,7 @@ namespace ts {
23138
23168
const rootChain = () => chainDiagnosticMessages(
23139
23169
/*details*/ undefined,
23140
23170
Diagnostics.Property_0_in_type_1_is_not_assignable_to_the_same_property_in_base_type_2,
23141
- unescapeLeadingUnderscores (declaredProp.escapedName ),
23171
+ symbolToString (declaredProp),
23142
23172
typeToString(typeWithThis),
23143
23173
typeToString(baseWithThis)
23144
23174
);
@@ -23159,7 +23189,7 @@ namespace ts {
23159
23189
if (signatures.length) {
23160
23190
const declaration = signatures[0].declaration;
23161
23191
if (declaration && hasModifier(declaration, ModifierFlags.Private)) {
23162
- const typeClassDeclaration = <ClassLikeDeclaration> getClassLikeDeclarationOfSymbol(type.symbol!);
23192
+ const typeClassDeclaration = getClassLikeDeclarationOfSymbol(type.symbol!)! ;
23163
23193
if (!isNodeWithinClass(node, typeClassDeclaration)) {
23164
23194
error(node, Diagnostics.Cannot_extend_a_class_0_Class_constructor_is_marked_as_private, getFullyQualifiedName(type.symbol!));
23165
23195
}
@@ -25597,7 +25627,7 @@ namespace ts {
25597
25627
return !!(symbol && getCheckFlags(symbol) & CheckFlags.Late);
25598
25628
},
25599
25629
writeLiteralConstValue,
25600
- getJsxFactoryEntity: () => _jsxFactoryEntity!, // TODO: GH#18217
25630
+ getJsxFactoryEntity: location => location ? (getJsxNamespace(location), (getSourceFileOfNode(location).localJsxFactory || _jsxFactoryEntity!)) : _jsxFactoryEntity!, // TODO: GH#18217
25601
25631
};
25602
25632
25603
25633
// defined here to avoid outer scope pollution
0 commit comments