-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
noImplicitAny as suggestion #27693
noImplicitAny as suggestion #27693
Changes from all commits
e6a57f7
c835e0c
d2fad6a
c59c4eb
eea5553
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4915,9 +4915,7 @@ namespace ts { | |
} | ||
const widened = getWidenedType(addOptionality(type, definedInMethod && !definedInConstructor)); | ||
if (filterType(widened, t => !!(t.flags & ~TypeFlags.Nullable)) === neverType) { | ||
if (noImplicitAny) { | ||
reportImplicitAnyError(symbol.valueDeclaration, anyType); | ||
} | ||
reportImplicitAny(symbol.valueDeclaration, anyType); | ||
return anyType; | ||
} | ||
return widened; | ||
|
@@ -4992,9 +4990,7 @@ namespace ts { | |
return result; | ||
} | ||
if (isEmptyArrayLiteralType(type)) { | ||
if (noImplicitAny) { | ||
reportImplicitAnyError(expression, anyArrayType); | ||
} | ||
reportImplicitAny(expression, anyArrayType); | ||
return anyArrayType; | ||
} | ||
return type; | ||
|
@@ -5044,8 +5040,8 @@ namespace ts { | |
if (isBindingPattern(element.name)) { | ||
return getTypeFromBindingPattern(element.name, includePatternInType, reportErrors); | ||
} | ||
if (reportErrors && noImplicitAny && !declarationBelongsToPrivateAmbientMember(element)) { | ||
reportImplicitAnyError(element, anyType); | ||
if (reportErrors && !declarationBelongsToPrivateAmbientMember(element)) { | ||
reportImplicitAny(element, anyType); | ||
} | ||
return anyType; | ||
} | ||
|
@@ -5145,9 +5141,9 @@ namespace ts { | |
type = isParameter(declaration) && declaration.dotDotDotToken ? anyArrayType : anyType; | ||
|
||
// Report implicit any errors unless this is a private property within an ambient declaration | ||
if (reportErrors && noImplicitAny) { | ||
if (reportErrors) { | ||
if (!declarationBelongsToPrivateAmbientMember(declaration)) { | ||
reportImplicitAnyError(declaration, type); | ||
reportImplicitAny(declaration, type); | ||
} | ||
} | ||
return type; | ||
|
@@ -5328,14 +5324,12 @@ namespace ts { | |
} | ||
// Otherwise, fall back to 'any'. | ||
else { | ||
if (noImplicitAny) { | ||
if (setter) { | ||
error(setter, Diagnostics.Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation, symbolToString(symbol)); | ||
} | ||
else { | ||
Debug.assert(!!getter, "there must existed getter as we are current checking either setter or getter in this function"); | ||
error(getter, Diagnostics.Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation, symbolToString(symbol)); | ||
} | ||
if (setter) { | ||
errorOrSuggestion(noImplicitAny, setter, Diagnostics.Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation, symbolToString(symbol)); | ||
} | ||
else { | ||
Debug.assert(!!getter, "there must existed getter as we are current checking either setter or getter in this function"); | ||
errorOrSuggestion(noImplicitAny, getter!, Diagnostics.Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation, symbolToString(symbol)); | ||
} | ||
type = anyType; | ||
} | ||
|
@@ -13270,8 +13264,12 @@ namespace ts { | |
return errorReported; | ||
} | ||
|
||
function reportImplicitAnyError(declaration: Declaration, type: Type) { | ||
function reportImplicitAny(declaration: Declaration, type: Type) { | ||
const typeAsString = typeToString(getWidenedType(type)); | ||
if (isInJSFile(declaration) && !isCheckJsEnabledForFile(getSourceFileOfNode(declaration), compilerOptions)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we even get here without checkJS? Also, this must be duplicate code of something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, why go out of our way to not report this error, if no errors will be shown anyway? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestions will still be shown, since they're stored in a separate array, and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
// Only report implicit any errors/suggestions in TS and ts-check JS files | ||
return; | ||
} | ||
let diagnostic: DiagnosticMessage; | ||
switch (declaration.kind) { | ||
case SyntaxKind.BinaryExpression: | ||
|
@@ -13294,26 +13292,28 @@ namespace ts { | |
case SyntaxKind.SetAccessor: | ||
case SyntaxKind.FunctionExpression: | ||
case SyntaxKind.ArrowFunction: | ||
if (!(declaration as NamedDeclaration).name) { | ||
if (noImplicitAny && !(declaration as NamedDeclaration).name) { | ||
error(declaration, Diagnostics.Function_expression_which_lacks_return_type_annotation_implicitly_has_an_0_return_type, typeAsString); | ||
return; | ||
} | ||
diagnostic = Diagnostics._0_which_lacks_return_type_annotation_implicitly_has_an_1_return_type; | ||
break; | ||
case SyntaxKind.MappedType: | ||
error(declaration, Diagnostics.Mapped_object_type_implicitly_has_an_any_template_type); | ||
if (noImplicitAny) { | ||
error(declaration, Diagnostics.Mapped_object_type_implicitly_has_an_any_template_type); | ||
} | ||
return; | ||
default: | ||
diagnostic = Diagnostics.Variable_0_implicitly_has_an_1_type; | ||
} | ||
error(declaration, diagnostic, declarationNameToString(getNameOfDeclaration(declaration)), typeAsString); | ||
errorOrSuggestion(noImplicitAny, declaration, diagnostic, declarationNameToString(getNameOfDeclaration(declaration)), typeAsString); | ||
} | ||
|
||
function reportErrorsFromWidening(declaration: Declaration, type: Type) { | ||
if (produceDiagnostics && noImplicitAny && type.flags & TypeFlags.ContainsWideningType) { | ||
// Report implicit any error within type if possible, otherwise report error on declaration | ||
if (!reportWideningErrorsInType(type)) { | ||
reportImplicitAnyError(declaration, type); | ||
reportImplicitAny(declaration, type); | ||
} | ||
} | ||
} | ||
|
@@ -22314,15 +22314,11 @@ namespace ts { | |
isTypeAssertion(initializer) ? type : getWidenedLiteralType(type); | ||
if (isInJSFile(declaration)) { | ||
if (widened.flags & TypeFlags.Nullable) { | ||
if (noImplicitAny) { | ||
reportImplicitAnyError(declaration, anyType); | ||
} | ||
reportImplicitAny(declaration, anyType); | ||
return anyType; | ||
} | ||
else if (isEmptyArrayLiteralType(widened)) { | ||
if (noImplicitAny) { | ||
reportImplicitAnyError(declaration, anyArrayType); | ||
} | ||
reportImplicitAny(declaration, anyArrayType); | ||
return anyArrayType; | ||
} | ||
} | ||
|
@@ -23313,8 +23309,8 @@ namespace ts { | |
checkSourceElement(node.typeParameter); | ||
checkSourceElement(node.type); | ||
|
||
if (noImplicitAny && !node.type) { | ||
reportImplicitAnyError(node, anyType); | ||
if (!node.type) { | ||
reportImplicitAny(node, anyType); | ||
} | ||
|
||
const type = <MappedType>getTypeFromMappedTypeNode(node); | ||
|
@@ -24338,8 +24334,8 @@ namespace ts { | |
if (produceDiagnostics && !getEffectiveReturnTypeNode(node)) { | ||
// Report an implicit any error if there is no body, no explicit return type, and node is not a private method | ||
// in an ambient context | ||
if (noImplicitAny && nodeIsMissing(body) && !isPrivateWithinAmbient(node)) { | ||
reportImplicitAnyError(node, anyType); | ||
if (nodeIsMissing(body) && !isPrivateWithinAmbient(node)) { | ||
reportImplicitAny(node, anyType); | ||
} | ||
|
||
if (functionFlags & FunctionFlags.Generator && nodeIsPresent(body)) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,4 @@ | |
//// x+1; | ||
////} | ||
|
||
verify.rangeAfterCodeFix("var x;"); | ||
verify.rangeAfterCodeFix("var x;", undefined, undefined, 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May be rename this to
reportImplicitAnyErrorOrSuggestion
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not necessary to take
noImplicitAny
as a parameter since that's already in scope. In every instance of calling this the argument isnoImplicitAny
; in one case the argument istrue
but it's withinif (noImplicitAny)
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I chose to shorten it to
reportImplicitAny
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andy-ms good catch. I'll get rid of the parameter.