mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-27 22:39:59 -05:00
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:
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user