Skip to content

Commit d282c70

Browse files
committed
Merge branch 'master' into fix42522
# Conflicts: # src/compiler/checker.ts
2 parents 3d6dbf4 + c7fa6e0 commit d282c70

File tree

123 files changed

+3983
-2896
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+3983
-2896
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ tests/baselines/reference/dt
8585
.failed-tests
8686
TEST-results.xml
8787
package-lock.json
88+
tests/cases/user/npm/npm
8889
tests/cases/user/TypeScript-React-Starter/TypeScript-React-Starter
8990
tests/cases/user/TypeScript-Node-Starter/TypeScript-Node-Starter
9091
tests/cases/user/TypeScript-React-Native-Starter/TypeScript-React-Native-Starter

package-lock.json

Lines changed: 25 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/compiler/binder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3307,7 +3307,7 @@ namespace ts {
33073307
}
33083308

33093309
if (!isBindingPattern(node.name)) {
3310-
if (isInJSFile(node) && isRequireVariableDeclaration(node, /*requireStringLiteralLikeArgument*/ true) && !getJSDocTypeTag(node)) {
3310+
if (isInJSFile(node) && isRequireVariableDeclaration(node) && !getJSDocTypeTag(node)) {
33113311
declareSymbolAndAddToSymbolTable(node as Declaration, SymbolFlags.Alias, SymbolFlags.AliasExcludes);
33123312
}
33133313
else if (isBlockOrCatchScoped(node)) {

src/compiler/checker.ts

Lines changed: 65 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,7 +2496,7 @@ namespace ts {
24962496
&& isAliasableOrJsExpression(node.parent.right)
24972497
|| node.kind === SyntaxKind.ShorthandPropertyAssignment
24982498
|| node.kind === SyntaxKind.PropertyAssignment && isAliasableOrJsExpression((node as PropertyAssignment).initializer)
2499-
|| isRequireVariableDeclaration(node, /*requireStringLiteralLikeArgument*/ true);
2499+
|| isRequireVariableDeclaration(node);
25002500
}
25012501

25022502
function isAliasableOrJsExpression(e: Expression) {
@@ -9186,7 +9186,6 @@ namespace ts {
91869186
return undefined;
91879187
}
91889188
switch (node.kind) {
9189-
case SyntaxKind.VariableStatement:
91909189
case SyntaxKind.ClassDeclaration:
91919190
case SyntaxKind.ClassExpression:
91929191
case SyntaxKind.InterfaceDeclaration:
@@ -9206,28 +9205,32 @@ namespace ts {
92069205
case SyntaxKind.JSDocEnumTag:
92079206
case SyntaxKind.JSDocCallbackTag:
92089207
case SyntaxKind.MappedType:
9209-
case SyntaxKind.ConditionalType:
9208+
case SyntaxKind.ConditionalType: {
92109209
const outerTypeParameters = getOuterTypeParameters(node, includeThisTypes);
92119210
if (node.kind === SyntaxKind.MappedType) {
92129211
return append(outerTypeParameters, getDeclaredTypeOfTypeParameter(getSymbolOfNode((<MappedTypeNode>node).typeParameter)));
92139212
}
92149213
else if (node.kind === SyntaxKind.ConditionalType) {
92159214
return concatenate(outerTypeParameters, getInferTypeParameters(<ConditionalTypeNode>node));
92169215
}
9217-
else if (node.kind === SyntaxKind.VariableStatement && !isInJSFile(node)) {
9218-
break;
9219-
}
92209216
const outerAndOwnTypeParameters = appendTypeParameters(outerTypeParameters, getEffectiveTypeParameterDeclarations(<DeclarationWithTypeParameters>node));
92219217
const thisType = includeThisTypes &&
92229218
(node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.ClassExpression || node.kind === SyntaxKind.InterfaceDeclaration || isJSConstructor(node)) &&
92239219
getDeclaredTypeOfClassOrInterface(getSymbolOfNode(node as ClassLikeDeclaration | InterfaceDeclaration)).thisType;
92249220
return thisType ? append(outerAndOwnTypeParameters, thisType) : outerAndOwnTypeParameters;
9221+
}
92259222
case SyntaxKind.JSDocParameterTag:
92269223
const paramSymbol = getParameterSymbolFromJSDoc(node as JSDocParameterTag);
92279224
if (paramSymbol) {
92289225
node = paramSymbol.valueDeclaration;
92299226
}
92309227
break;
9228+
case SyntaxKind.JSDocComment: {
9229+
const outerTypeParameters = getOuterTypeParameters(node, includeThisTypes);
9230+
return (node as JSDoc).tags
9231+
? appendTypeParameters(outerTypeParameters, flatMap((node as JSDoc).tags, t => isJSDocTemplateTag(t) ? t.typeParameters : undefined))
9232+
: outerTypeParameters;
9233+
}
92319234
}
92329235
}
92339236
}
@@ -13381,7 +13384,7 @@ namespace ts {
1338113384
return types;
1338213385
}
1338313386

13384-
function removeRedundantLiteralTypes(types: Type[], includes: TypeFlags) {
13387+
function removeRedundantLiteralTypes(types: Type[], includes: TypeFlags, reduceVoidUndefined: boolean) {
1338513388
let i = types.length;
1338613389
while (i > 0) {
1338713390
i--;
@@ -13392,7 +13395,7 @@ namespace ts {
1339213395
flags & TypeFlags.NumberLiteral && includes & TypeFlags.Number ||
1339313396
flags & TypeFlags.BigIntLiteral && includes & TypeFlags.BigInt ||
1339413397
flags & TypeFlags.UniqueESSymbol && includes & TypeFlags.ESSymbol ||
13395-
flags & TypeFlags.Undefined && includes & TypeFlags.Void ||
13398+
reduceVoidUndefined && flags & TypeFlags.Undefined && includes & TypeFlags.Void ||
1339613399
isFreshLiteralType(t) && containsType(types, (<LiteralType>t).regularType);
1339713400
if (remove) {
1339813401
orderedRemoveItemAt(types, i);
@@ -13459,7 +13462,7 @@ namespace ts {
1345913462
return includes & TypeFlags.Any ? includes & TypeFlags.IncludesWildcard ? wildcardType : anyType : unknownType;
1346013463
}
1346113464
if (includes & (TypeFlags.Literal | TypeFlags.UniqueESSymbol) || includes & TypeFlags.Void && includes & TypeFlags.Undefined) {
13462-
removeRedundantLiteralTypes(typeSet, includes);
13465+
removeRedundantLiteralTypes(typeSet, includes, !!(unionReduction & UnionReduction.Subtype));
1346313466
}
1346413467
if (includes & TypeFlags.StringLiteral && includes & TypeFlags.TemplateLiteral) {
1346513468
removeStringLiteralsMatchedByTemplateLiterals(typeSet);
@@ -22079,7 +22082,16 @@ namespace ts {
2207922082
const type = getTypeOfDottedName((<PropertyAccessExpression>node).expression, diagnostic);
2208022083
if (type) {
2208122084
const name = (<PropertyAccessExpression>node).name;
22082-
const prop = getPropertyOfType(type, isPrivateIdentifier(name) ? getSymbolNameForPrivateIdentifier(type.symbol, name.escapedText) : name.escapedText);
22085+
let prop: Symbol | undefined;
22086+
if (isPrivateIdentifier(name)) {
22087+
if (!type.symbol) {
22088+
return undefined;
22089+
}
22090+
prop = getPropertyOfType(type, getSymbolNameForPrivateIdentifier(type.symbol, name.escapedText));
22091+
}
22092+
else {
22093+
prop = getPropertyOfType(type, name.escapedText);
22094+
}
2208322095
return prop && getExplicitTypeOfSymbol(prop, diagnostic);
2208422096
}
2208522097
return undefined;
@@ -34565,7 +34577,7 @@ namespace ts {
3456534577
}
3456634578
// For a commonjs `const x = require`, validate the alias and exit
3456734579
const symbol = getSymbolOfNode(node);
34568-
if (symbol.flags & SymbolFlags.Alias && isRequireVariableDeclaration(node, /*requireStringLiteralLikeArgument*/ true)) {
34580+
if (symbol.flags & SymbolFlags.Alias && isRequireVariableDeclaration(node)) {
3456934581
checkAliasSymbol(node);
3457034582
return;
3457134583
}
@@ -35070,18 +35082,8 @@ namespace ts {
3507035082
// want to say that number is not an array type. But if the input was just
3507135083
// number and string input is allowed, we want to say that number is not an
3507235084
// array type or a string type.
35073-
const yieldType = getIterationTypeOfIterable(use, IterationTypeKind.Yield, inputType, /*errorNode*/ undefined);
35074-
const [defaultDiagnostic, maybeMissingAwait]: [DiagnosticMessage, boolean] = !(use & IterationUse.AllowsStringInputFlag) || hasStringConstituent
35075-
? downlevelIteration
35076-
? [Diagnostics.Type_0_is_not_an_array_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator, true]
35077-
: yieldType
35078-
? [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Use_compiler_option_downlevelIteration_to_allow_iterating_of_iterators, false]
35079-
: [Diagnostics.Type_0_is_not_an_array_type, true]
35080-
: downlevelIteration
35081-
? [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator, true]
35082-
: yieldType
35083-
? [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Use_compiler_option_downlevelIteration_to_allow_iterating_of_iterators, false]
35084-
: [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type, true];
35085+
const allowsStrings = !!(use & IterationUse.AllowsStringInputFlag) && !hasStringConstituent;
35086+
const [defaultDiagnostic, maybeMissingAwait] = getIterationDiagnosticDetails(allowsStrings, downlevelIteration);
3508535087
errorAndMaybeSuggestAwait(
3508635088
errorNode,
3508735089
maybeMissingAwait && !!getAwaitedTypeOfPromise(arrayType),
@@ -35102,6 +35104,45 @@ namespace ts {
3510235104
}
3510335105

3510435106
return (use & IterationUse.PossiblyOutOfBounds) ? includeUndefinedInIndexSignature(arrayElementType) : arrayElementType;
35107+
35108+
function getIterationDiagnosticDetails(allowsStrings: boolean, downlevelIteration: boolean | undefined): [DiagnosticMessage, boolean] {
35109+
if (downlevelIteration) {
35110+
return allowsStrings
35111+
? [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator, true]
35112+
: [Diagnostics.Type_0_is_not_an_array_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator, true];
35113+
}
35114+
35115+
const yieldType = getIterationTypeOfIterable(use, IterationTypeKind.Yield, inputType, /*errorNode*/ undefined);
35116+
35117+
if (yieldType) {
35118+
return [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Use_compiler_option_downlevelIteration_to_allow_iterating_of_iterators, false];
35119+
}
35120+
35121+
if (isES2015OrLaterIterable(inputType.symbol?.escapedName)) {
35122+
return [Diagnostics.Type_0_can_only_be_iterated_through_when_using_the_downlevelIteration_flag_or_with_a_target_of_es2015_or_higher, true];
35123+
}
35124+
35125+
return allowsStrings
35126+
? [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type, true]
35127+
: [Diagnostics.Type_0_is_not_an_array_type, true];
35128+
}
35129+
}
35130+
35131+
function isES2015OrLaterIterable(n: __String) {
35132+
switch (n) {
35133+
case "Float32Array":
35134+
case "Float64Array":
35135+
case "Int16Array":
35136+
case "Int32Array":
35137+
case "Int8Array":
35138+
case "NodeList":
35139+
case "Uint16Array":
35140+
case "Uint32Array":
35141+
case "Uint8Array":
35142+
case "Uint8ClampedArray":
35143+
return true;
35144+
}
35145+
return false;
3510535146
}
3510635147

3510735148
/**
@@ -40906,7 +40947,7 @@ namespace ts {
4090640947
if (isStringLiteral(node.name) && node.name.text === "constructor") {
4090740948
return grammarErrorOnNode(node.name, Diagnostics.Classes_may_not_have_a_field_named_constructor);
4090840949
}
40909-
if (checkGrammarForInvalidDynamicName(node.name, Diagnostics.A_computed_property_name_in_a_class_property_declaration_must_refer_to_an_expression_whose_type_is_a_literal_type_or_a_unique_symbol_type)) {
40950+
if (checkGrammarForInvalidDynamicName(node.name, Diagnostics.A_computed_property_name_in_a_class_property_declaration_must_have_a_simple_literal_type_or_a_unique_symbol_type)) {
4091040951
return true;
4091140952
}
4091240953
if (languageVersion < ScriptTarget.ES2015 && isPrivateIdentifier(node.name)) {

src/compiler/diagnosticMessages.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@
503503
"category": "Error",
504504
"code": 1165
505505
},
506-
"A computed property name in a class property declaration must refer to an expression whose type is a literal type or a 'unique symbol' type.": {
506+
"A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type.": {
507507
"category": "Error",
508508
"code": 1166
509509
},
@@ -3260,6 +3260,10 @@
32603260
"category": "Error",
32613261
"code": 2801
32623262
},
3263+
"Type '{0}' can only be iterated through when using the '--downlevelIteration' flag or with a '--target' of 'es2015' or higher.": {
3264+
"category": "Error",
3265+
"code": 2802
3266+
},
32633267

32643268
"Import declaration '{0}' is using private name '{1}'.": {
32653269
"category": "Error",

src/compiler/parser.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8043,6 +8043,9 @@ namespace ts {
80438043
function parseTemplateTagTypeParameter() {
80448044
const typeParameterPos = getNodePos();
80458045
const name = parseJSDocIdentifierName(Diagnostics.Unexpected_token_A_type_parameter_name_was_expected_without_curly_braces);
8046+
if (nodeIsMissing(name)) {
8047+
return undefined;
8048+
}
80468049
return finishNode(factory.createTypeParameterDeclaration(name, /*constraint*/ undefined, /*defaultType*/ undefined), typeParameterPos);
80478050
}
80488051

@@ -8051,7 +8054,10 @@ namespace ts {
80518054
const typeParameters = [];
80528055
do {
80538056
skipWhitespace();
8054-
typeParameters.push(parseTemplateTagTypeParameter());
8057+
const node = parseTemplateTagTypeParameter();
8058+
if (node !== undefined) {
8059+
typeParameters.push(node);
8060+
}
80558061
skipWhitespaceOrAsterisk();
80568062
} while (parseOptionalJsdoc(SyntaxKind.CommaToken));
80578063
return createNodeArray(typeParameters, pos);

0 commit comments

Comments
 (0)