Skip to content

Commit

Permalink
🤖 Pick PR #44219 (Properly remove generic types that ...) into releas…
Browse files Browse the repository at this point in the history
…e-4.3 (#44243)

* Cherry-pick PR #44219 into release-4.3

Component commits:
4475012 Improve getNonNullableType function

86f09ae Add tests

d45806a More closely match previous behavior

634b01a Add non-strict mode test

* Update LKG

Co-authored-by: Anders Hejlsberg <andersh@microsoft.com>
Co-authored-by: typescript-bot <typescript@microsoft.com>
  • Loading branch information
3 people committed May 25, 2021
1 parent d6e6fa7 commit 28e3e6f
Show file tree
Hide file tree
Showing 15 changed files with 460 additions and 77 deletions.
29 changes: 18 additions & 11 deletions lib/tsc.js
Expand Up @@ -46281,6 +46281,10 @@ var ts;
var indexSymbol = symbol && getIndexSymbol(symbol);
return indexSymbol && getIndexDeclarationOfIndexSymbol(indexSymbol, kind);
}
function getIndexDeclarationOfSymbolTable(symbolTable, kind) {
var indexSymbol = symbolTable && getIndexSymbolFromSymbolTable(symbolTable);
return indexSymbol && getIndexDeclarationOfIndexSymbol(indexSymbol, kind);
}
function getIndexDeclarationOfIndexSymbol(indexSymbol, kind) {
var syntaxKind = kind === 1 ? 144 : 147;
if (indexSymbol === null || indexSymbol === void 0 ? void 0 : indexSymbol.declarations) {
Expand Down Expand Up @@ -52614,13 +52618,13 @@ var ts;
return type.flags & 32768 ? type : getUnionType([type, undefinedType]);
}
function getGlobalNonNullableTypeInstantiation(type) {
var reducedType = getTypeWithFacts(type, 2097152);
if (!deferredGlobalNonNullableTypeAlias) {
deferredGlobalNonNullableTypeAlias = getGlobalSymbol("NonNullable", 524288, undefined) || unknownSymbol;
}
if (deferredGlobalNonNullableTypeAlias !== unknownSymbol) {
return getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [type]);
}
return getTypeWithFacts(type, 2097152);
return deferredGlobalNonNullableTypeAlias !== unknownSymbol ?
getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [reducedType]) :
reducedType;
}
function getNonNullableType(type) {
return strictNullChecks ? getGlobalNonNullableTypeInstantiation(type) : type;
Expand Down Expand Up @@ -55757,7 +55761,7 @@ var ts;
!isGenericIndexType(getTypeOfExpression(parent.argumentExpression));
}
function isGenericTypeWithUnionConstraint(type) {
return !!(type.flags & 465829888 && getBaseConstraintOrType(type).flags & 1048576);
return !!(type.flags & 465829888 && getBaseConstraintOrType(type).flags & (98304 | 1048576));
}
function containsGenericType(type) {
return !!(type.flags & 465829888 || type.flags & 3145728 && ts.some(type.types, containsGenericType));
Expand Down Expand Up @@ -65459,21 +65463,24 @@ var ts;
checkBlock(node.finallyBlock);
}
}
function checkIndexConstraints(type) {
var declaredNumberIndexer = getIndexDeclarationOfSymbol(type.symbol, 1);
var declaredStringIndexer = getIndexDeclarationOfSymbol(type.symbol, 0);
function checkIndexConstraints(type, isStatic) {
var _a, _b, _c, _d;
var declaredNumberIndexer = getIndexDeclarationOfSymbolTable(isStatic ? (_a = type.symbol) === null || _a === void 0 ? void 0 : _a.exports : (_b = type.symbol) === null || _b === void 0 ? void 0 : _b.members, 1);
var declaredStringIndexer = getIndexDeclarationOfSymbolTable(isStatic ? (_c = type.symbol) === null || _c === void 0 ? void 0 : _c.exports : (_d = type.symbol) === null || _d === void 0 ? void 0 : _d.members, 0);
var stringIndexType = getIndexTypeOfType(type, 0);
var numberIndexType = getIndexTypeOfType(type, 1);
if (stringIndexType || numberIndexType) {
ts.forEach(getPropertiesOfObjectType(type), function (prop) {
if (isStatic && prop.flags & 4194304)
return;
var propType = getTypeOfSymbol(prop);
checkIndexConstraintForProperty(prop, propType, type, declaredStringIndexer, stringIndexType, 0);
checkIndexConstraintForProperty(prop, propType, type, declaredNumberIndexer, numberIndexType, 1);
});
var classDeclaration = type.symbol.valueDeclaration;
if (ts.getObjectFlags(type) & 1 && classDeclaration && ts.isClassLike(classDeclaration)) {
for (var _i = 0, _a = classDeclaration.members; _i < _a.length; _i++) {
var member = _a[_i];
for (var _i = 0, _e = classDeclaration.members; _i < _e.length; _i++) {
var member = _e[_i];
if (!ts.hasSyntacticModifier(member, 32) && !hasBindableName(member)) {
var symbol = getSymbolOfNode(member);
var propType = getTypeOfSymbol(symbol);
Expand Down Expand Up @@ -65767,7 +65774,7 @@ var ts;
}
if (produceDiagnostics) {
checkIndexConstraints(type);
checkIndexConstraints(staticType);
checkIndexConstraints(staticType, true);
checkTypeForDuplicateIndexSignatures(node);
checkPropertyInitialization(node);
}
Expand Down
34 changes: 22 additions & 12 deletions lib/tsserver.js
Expand Up @@ -56010,6 +56010,10 @@ var ts;
var indexSymbol = symbol && getIndexSymbol(symbol);
return indexSymbol && getIndexDeclarationOfIndexSymbol(indexSymbol, kind);
}
function getIndexDeclarationOfSymbolTable(symbolTable, kind) {
var indexSymbol = symbolTable && getIndexSymbolFromSymbolTable(symbolTable);
return indexSymbol && getIndexDeclarationOfIndexSymbol(indexSymbol, kind);
}
function getIndexDeclarationOfIndexSymbol(indexSymbol, kind) {
var syntaxKind = kind === 1 /* Number */ ? 144 /* NumberKeyword */ : 147 /* StringKeyword */;
if (indexSymbol === null || indexSymbol === void 0 ? void 0 : indexSymbol.declarations) {
Expand Down Expand Up @@ -63218,14 +63222,17 @@ var ts;
return type.flags & 32768 /* Undefined */ ? type : getUnionType([type, undefinedType]);
}
function getGlobalNonNullableTypeInstantiation(type) {
// First reduce away any constituents that are assignable to 'undefined' or 'null'. This not only eliminates
// 'undefined' and 'null', but also higher-order types such as a type parameter 'U extends undefined | null'
// that isn't eliminated by a NonNullable<T> instantiation.
var reducedType = getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */);
if (!deferredGlobalNonNullableTypeAlias) {
deferredGlobalNonNullableTypeAlias = getGlobalSymbol("NonNullable", 524288 /* TypeAlias */, /*diagnostic*/ undefined) || unknownSymbol;
}
// Use NonNullable global type alias if available to improve quick info/declaration emit
if (deferredGlobalNonNullableTypeAlias !== unknownSymbol) {
return getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [type]);
}
return getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */); // Type alias unavailable, fall back to non-higher-order behavior
// If the NonNullable<T> type is available, return an instantiation. Otherwise just return the reduced type.
return deferredGlobalNonNullableTypeAlias !== unknownSymbol ?
getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [reducedType]) :
reducedType;
}
function getNonNullableType(type) {
return strictNullChecks ? getGlobalNonNullableTypeInstantiation(type) : type;
Expand Down Expand Up @@ -66813,7 +66820,7 @@ var ts;
!isGenericIndexType(getTypeOfExpression(parent.argumentExpression));
}
function isGenericTypeWithUnionConstraint(type) {
return !!(type.flags & 465829888 /* Instantiable */ && getBaseConstraintOrType(type).flags & 1048576 /* Union */);
return !!(type.flags & 465829888 /* Instantiable */ && getBaseConstraintOrType(type).flags & (98304 /* Nullable */ | 1048576 /* Union */));
}
function containsGenericType(type) {
return !!(type.flags & 465829888 /* Instantiable */ || type.flags & 3145728 /* UnionOrIntersection */ && ts.some(type.types, containsGenericType));
Expand Down Expand Up @@ -78287,21 +78294,24 @@ var ts;
checkBlock(node.finallyBlock);
}
}
function checkIndexConstraints(type) {
var declaredNumberIndexer = getIndexDeclarationOfSymbol(type.symbol, 1 /* Number */);
var declaredStringIndexer = getIndexDeclarationOfSymbol(type.symbol, 0 /* String */);
function checkIndexConstraints(type, isStatic) {
var _a, _b, _c, _d;
var declaredNumberIndexer = getIndexDeclarationOfSymbolTable(isStatic ? (_a = type.symbol) === null || _a === void 0 ? void 0 : _a.exports : (_b = type.symbol) === null || _b === void 0 ? void 0 : _b.members, 1 /* Number */);
var declaredStringIndexer = getIndexDeclarationOfSymbolTable(isStatic ? (_c = type.symbol) === null || _c === void 0 ? void 0 : _c.exports : (_d = type.symbol) === null || _d === void 0 ? void 0 : _d.members, 0 /* String */);
var stringIndexType = getIndexTypeOfType(type, 0 /* String */);
var numberIndexType = getIndexTypeOfType(type, 1 /* Number */);
if (stringIndexType || numberIndexType) {
ts.forEach(getPropertiesOfObjectType(type), function (prop) {
if (isStatic && prop.flags & 4194304 /* Prototype */)
return;
var propType = getTypeOfSymbol(prop);
checkIndexConstraintForProperty(prop, propType, type, declaredStringIndexer, stringIndexType, 0 /* String */);
checkIndexConstraintForProperty(prop, propType, type, declaredNumberIndexer, numberIndexType, 1 /* Number */);
});
var classDeclaration = type.symbol.valueDeclaration;
if (ts.getObjectFlags(type) & 1 /* Class */ && classDeclaration && ts.isClassLike(classDeclaration)) {
for (var _i = 0, _a = classDeclaration.members; _i < _a.length; _i++) {
var member = _a[_i];
for (var _i = 0, _e = classDeclaration.members; _i < _e.length; _i++) {
var member = _e[_i];
// Only process instance properties with computed names here.
// Static properties cannot be in conflict with indexers,
// and properties with literal names were already checked.
Expand Down Expand Up @@ -78631,7 +78641,7 @@ var ts;
}
if (produceDiagnostics) {
checkIndexConstraints(type);
checkIndexConstraints(staticType);
checkIndexConstraints(staticType, /*isStatic*/ true);
checkTypeForDuplicateIndexSignatures(node);
checkPropertyInitialization(node);
}
Expand Down
34 changes: 22 additions & 12 deletions lib/tsserverlibrary.js
Expand Up @@ -56204,6 +56204,10 @@ var ts;
var indexSymbol = symbol && getIndexSymbol(symbol);
return indexSymbol && getIndexDeclarationOfIndexSymbol(indexSymbol, kind);
}
function getIndexDeclarationOfSymbolTable(symbolTable, kind) {
var indexSymbol = symbolTable && getIndexSymbolFromSymbolTable(symbolTable);
return indexSymbol && getIndexDeclarationOfIndexSymbol(indexSymbol, kind);
}
function getIndexDeclarationOfIndexSymbol(indexSymbol, kind) {
var syntaxKind = kind === 1 /* Number */ ? 144 /* NumberKeyword */ : 147 /* StringKeyword */;
if (indexSymbol === null || indexSymbol === void 0 ? void 0 : indexSymbol.declarations) {
Expand Down Expand Up @@ -63412,14 +63416,17 @@ var ts;
return type.flags & 32768 /* Undefined */ ? type : getUnionType([type, undefinedType]);
}
function getGlobalNonNullableTypeInstantiation(type) {
// First reduce away any constituents that are assignable to 'undefined' or 'null'. This not only eliminates
// 'undefined' and 'null', but also higher-order types such as a type parameter 'U extends undefined | null'
// that isn't eliminated by a NonNullable<T> instantiation.
var reducedType = getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */);
if (!deferredGlobalNonNullableTypeAlias) {
deferredGlobalNonNullableTypeAlias = getGlobalSymbol("NonNullable", 524288 /* TypeAlias */, /*diagnostic*/ undefined) || unknownSymbol;
}
// Use NonNullable global type alias if available to improve quick info/declaration emit
if (deferredGlobalNonNullableTypeAlias !== unknownSymbol) {
return getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [type]);
}
return getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */); // Type alias unavailable, fall back to non-higher-order behavior
// If the NonNullable<T> type is available, return an instantiation. Otherwise just return the reduced type.
return deferredGlobalNonNullableTypeAlias !== unknownSymbol ?
getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [reducedType]) :
reducedType;
}
function getNonNullableType(type) {
return strictNullChecks ? getGlobalNonNullableTypeInstantiation(type) : type;
Expand Down Expand Up @@ -67007,7 +67014,7 @@ var ts;
!isGenericIndexType(getTypeOfExpression(parent.argumentExpression));
}
function isGenericTypeWithUnionConstraint(type) {
return !!(type.flags & 465829888 /* Instantiable */ && getBaseConstraintOrType(type).flags & 1048576 /* Union */);
return !!(type.flags & 465829888 /* Instantiable */ && getBaseConstraintOrType(type).flags & (98304 /* Nullable */ | 1048576 /* Union */));
}
function containsGenericType(type) {
return !!(type.flags & 465829888 /* Instantiable */ || type.flags & 3145728 /* UnionOrIntersection */ && ts.some(type.types, containsGenericType));
Expand Down Expand Up @@ -78481,21 +78488,24 @@ var ts;
checkBlock(node.finallyBlock);
}
}
function checkIndexConstraints(type) {
var declaredNumberIndexer = getIndexDeclarationOfSymbol(type.symbol, 1 /* Number */);
var declaredStringIndexer = getIndexDeclarationOfSymbol(type.symbol, 0 /* String */);
function checkIndexConstraints(type, isStatic) {
var _a, _b, _c, _d;
var declaredNumberIndexer = getIndexDeclarationOfSymbolTable(isStatic ? (_a = type.symbol) === null || _a === void 0 ? void 0 : _a.exports : (_b = type.symbol) === null || _b === void 0 ? void 0 : _b.members, 1 /* Number */);
var declaredStringIndexer = getIndexDeclarationOfSymbolTable(isStatic ? (_c = type.symbol) === null || _c === void 0 ? void 0 : _c.exports : (_d = type.symbol) === null || _d === void 0 ? void 0 : _d.members, 0 /* String */);
var stringIndexType = getIndexTypeOfType(type, 0 /* String */);
var numberIndexType = getIndexTypeOfType(type, 1 /* Number */);
if (stringIndexType || numberIndexType) {
ts.forEach(getPropertiesOfObjectType(type), function (prop) {
if (isStatic && prop.flags & 4194304 /* Prototype */)
return;
var propType = getTypeOfSymbol(prop);
checkIndexConstraintForProperty(prop, propType, type, declaredStringIndexer, stringIndexType, 0 /* String */);
checkIndexConstraintForProperty(prop, propType, type, declaredNumberIndexer, numberIndexType, 1 /* Number */);
});
var classDeclaration = type.symbol.valueDeclaration;
if (ts.getObjectFlags(type) & 1 /* Class */ && classDeclaration && ts.isClassLike(classDeclaration)) {
for (var _i = 0, _a = classDeclaration.members; _i < _a.length; _i++) {
var member = _a[_i];
for (var _i = 0, _e = classDeclaration.members; _i < _e.length; _i++) {
var member = _e[_i];
// Only process instance properties with computed names here.
// Static properties cannot be in conflict with indexers,
// and properties with literal names were already checked.
Expand Down Expand Up @@ -78825,7 +78835,7 @@ var ts;
}
if (produceDiagnostics) {
checkIndexConstraints(type);
checkIndexConstraints(staticType);
checkIndexConstraints(staticType, /*isStatic*/ true);
checkTypeForDuplicateIndexSignatures(node);
checkPropertyInitialization(node);
}
Expand Down

0 comments on commit 28e3e6f

Please sign in to comment.