Skip to content
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

Merged
merged 5 commits into from Oct 11, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/compiler/checker.ts
Expand Up @@ -4915,7 +4915,7 @@ namespace ts {
}
const widened = getWidenedType(addOptionality(type, definedInMethod && !definedInConstructor));
if (filterType(widened, t => !!(t.flags & ~TypeFlags.Nullable)) === neverType) {
reportImplicitAnyError(noImplicitAny, symbol.valueDeclaration, anyType);
reportImplicitAny(noImplicitAny, symbol.valueDeclaration, anyType);
return anyType;
}
return widened;
Expand Down Expand Up @@ -4990,7 +4990,7 @@ namespace ts {
return result;
}
if (isEmptyArrayLiteralType(type)) {
reportImplicitAnyError(noImplicitAny, expression, anyArrayType);
reportImplicitAny(noImplicitAny, expression, anyArrayType);
return anyArrayType;
}
return type;
Expand Down Expand Up @@ -5041,7 +5041,7 @@ namespace ts {
return getTypeFromBindingPattern(element.name, includePatternInType, reportErrors);
}
if (reportErrors && !declarationBelongsToPrivateAmbientMember(element)) {
reportImplicitAnyError(noImplicitAny, element, anyType);
reportImplicitAny(noImplicitAny, element, anyType);
}
return anyType;
}
Expand Down Expand Up @@ -5143,7 +5143,7 @@ namespace ts {
// Report implicit any errors unless this is a private property within an ambient declaration
if (reportErrors) {
if (!declarationBelongsToPrivateAmbientMember(declaration)) {
reportImplicitAnyError(noImplicitAny, declaration, type);
reportImplicitAny(noImplicitAny, declaration, type);
}
}
return type;
Expand Down Expand Up @@ -13264,8 +13264,12 @@ namespace ts {
return errorReported;
}

function reportImplicitAnyError(noImplicitAny: boolean, declaration: Declaration, type: Type) {
function reportImplicitAny(noImplicitAny: boolean, declaration: Declaration, type: Type) {
Copy link
Member

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?

Copy link

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 is noImplicitAny; in one case the argument is true but it's within if (noImplicitAny).

Copy link
Member Author

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

Copy link
Member Author

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.

const typeAsString = typeToString(getWidenedType(type));
if (isInJSFile(declaration) && !getSourceFileOfNode(declaration).pragmas.has("checkJSDirective") && !compilerOptions.checkJs) {
Copy link

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Yes, allowJS reports errors as usual, but doesn't actually display them.
  2. Yes, isCheckJsEnabledForFile.

Copy link

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The 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 errorOrSuggestion at the bottom of the function will create a suggestion instead of an error in an allowJs file.

Copy link

Choose a reason for hiding this comment

The 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:
Expand Down Expand Up @@ -13309,7 +13313,7 @@ namespace ts {
if (produceDiagnostics && noImplicitAny && type.flags & TypeFlags.ContainsWideningType) {
// Report implicit any error within type if possible, otherwise report error on declaration
if (!reportWideningErrorsInType(type)) {
reportImplicitAnyError(/*noImplicitAny*/ true, declaration, type);
reportImplicitAny(/*noImplicitAny*/ true, declaration, type);
}
}
}
Expand Down Expand Up @@ -22310,11 +22314,11 @@ namespace ts {
isTypeAssertion(initializer) ? type : getWidenedLiteralType(type);
if (isInJSFile(declaration)) {
if (widened.flags & TypeFlags.Nullable) {
reportImplicitAnyError(noImplicitAny, declaration, anyType);
reportImplicitAny(noImplicitAny, declaration, anyType);
return anyType;
}
else if (isEmptyArrayLiteralType(widened)) {
reportImplicitAnyError(noImplicitAny, declaration, anyArrayType);
reportImplicitAny(noImplicitAny, declaration, anyArrayType);
return anyArrayType;
}
}
Expand Down Expand Up @@ -23306,7 +23310,7 @@ namespace ts {
checkSourceElement(node.type);

if (!node.type) {
reportImplicitAnyError(noImplicitAny, node, anyType);
reportImplicitAny(noImplicitAny, node, anyType);
}

const type = <MappedType>getTypeFromMappedTypeNode(node);
Expand Down Expand Up @@ -24331,7 +24335,7 @@ namespace ts {
// 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 (nodeIsMissing(body) && !isPrivateWithinAmbient(node)) {
reportImplicitAnyError(noImplicitAny, node, anyType);
reportImplicitAny(noImplicitAny, node, anyType);
}

if (functionFlags & FunctionFlags.Generator && nodeIsPresent(body)) {
Expand Down