Specified diagnostic for iterating known array type without --downlevelIteration (#40070)

* Specified error message for iterating known array types without --downlevelIteration

* Added extra target info to diagnostic

* NodeList too, a classic

* PR feedback: invert to allowsStrings; required param

Co-authored-by: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com>
This commit is contained in:
Josh Goldberg
2021-02-17 20:12:32 -05:00
committed by GitHub
parent 7fca9267e6
commit c3d7a56e90
11 changed files with 653 additions and 12 deletions

View File

@@ -34905,18 +34905,8 @@ namespace ts {
// want to say that number is not an array type. But if the input was just
// number and string input is allowed, we want to say that number is not an
// array type or a string type.
const yieldType = getIterationTypeOfIterable(use, IterationTypeKind.Yield, inputType, /*errorNode*/ undefined);
const [defaultDiagnostic, maybeMissingAwait]: [DiagnosticMessage, boolean] = !(use & IterationUse.AllowsStringInputFlag) || hasStringConstituent
? downlevelIteration
? [Diagnostics.Type_0_is_not_an_array_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator, true]
: yieldType
? [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Use_compiler_option_downlevelIteration_to_allow_iterating_of_iterators, false]
: [Diagnostics.Type_0_is_not_an_array_type, true]
: downlevelIteration
? [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]
: yieldType
? [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Use_compiler_option_downlevelIteration_to_allow_iterating_of_iterators, false]
: [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type, true];
const allowsStrings = !!(use & IterationUse.AllowsStringInputFlag) && !hasStringConstituent;
const [defaultDiagnostic, maybeMissingAwait] = getIterationDiagnosticDetails(allowsStrings, downlevelIteration);
errorAndMaybeSuggestAwait(
errorNode,
maybeMissingAwait && !!getAwaitedTypeOfPromise(arrayType),
@@ -34937,6 +34927,45 @@ namespace ts {
}
return (use & IterationUse.PossiblyOutOfBounds) ? includeUndefinedInIndexSignature(arrayElementType) : arrayElementType;
function getIterationDiagnosticDetails(allowsStrings: boolean, downlevelIteration: boolean | undefined): [DiagnosticMessage, boolean] {
if (downlevelIteration) {
return allowsStrings
? [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]
: [Diagnostics.Type_0_is_not_an_array_type_or_does_not_have_a_Symbol_iterator_method_that_returns_an_iterator, true];
}
const yieldType = getIterationTypeOfIterable(use, IterationTypeKind.Yield, inputType, /*errorNode*/ undefined);
if (yieldType) {
return [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type_Use_compiler_option_downlevelIteration_to_allow_iterating_of_iterators, false];
}
if (isES2015OrLaterIterable(inputType.symbol?.escapedName)) {
return [Diagnostics.Type_0_can_only_be_iterated_through_when_using_the_downlevelIteration_flag_or_with_a_target_of_es2015_or_higher, true];
}
return allowsStrings
? [Diagnostics.Type_0_is_not_an_array_type_or_a_string_type, true]
: [Diagnostics.Type_0_is_not_an_array_type, true];
}
}
function isES2015OrLaterIterable(n: __String) {
switch (n) {
case "Float32Array":
case "Float64Array":
case "Int16Array":
case "Int32Array":
case "Int8Array":
case "NodeList":
case "Uint16Array":
case "Uint32Array":
case "Uint8Array":
case "Uint8ClampedArray":
return true;
}
return false;
}
/**

View File

@@ -3260,6 +3260,10 @@
"category": "Error",
"code": 2801
},
"Type '{0}' can only be iterated through when using the '--downlevelIteration' flag or with a '--target' of 'es2015' or higher.": {
"category": "Error",
"code": 2802
},
"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",