Skip to content

chore(eslint-plugin): add a debug log when a type is an "error" any to help with investigations #2226

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

Merged
merged 1 commit into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ module.exports = {
// our plugin :D
//

'@typescript-eslint/ban-ts-comment': [
'error',
{
'ts-expect-error': 'allow-with-description',
'ts-ignore': true,
'ts-nocheck': true,
'ts-check': false,
minimumDescriptionLength: 5,
},
],
'@typescript-eslint/consistent-type-definitions': ['error', 'interface'],
'@typescript-eslint/explicit-function-return-type': 'error',
'@typescript-eslint/explicit-module-boundary-types': 'off',
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
},
"dependencies": {
"@typescript-eslint/experimental-utils": "3.3.0",
"debug": "^4.1.1",
"functional-red-black-tree": "^1.0.1",
"regexpp": "^3.0.0",
"semver": "^7.3.2",
Expand Down
5 changes: 2 additions & 3 deletions packages/eslint-plugin/src/rules/await-thenable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as tsutils from 'tsutils';
import * as ts from 'typescript';

import * as util from '../util';

Expand Down Expand Up @@ -30,8 +29,8 @@ export default util.createRule({
const type = checker.getTypeAtLocation(originalNode.expression);

if (
!tsutils.isTypeFlagSet(type, ts.TypeFlags.Any) &&
!tsutils.isTypeFlagSet(type, ts.TypeFlags.Unknown) &&
!util.isTypeAnyType(type) &&
!util.isTypeUnknownType(type) &&
!tsutils.isThenableType(checker, originalNode.expression, type)
) {
context.report({
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/no-throw-literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ export default util.createRule({
}

if (
type.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown) ||
util.isTypeAnyType(type) ||
util.isTypeUnknownType(type) ||
isErrorLike(type)
) {
return;
Expand Down
20 changes: 10 additions & 10 deletions packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
NullThrowsReasons,
isMemberOrOptionalMemberExpression,
isIdentifier,
isTypeAnyType,
isTypeUnknownType,
} from '../util';

// Truthiness utilities
Expand Down Expand Up @@ -181,13 +183,11 @@ export default createRule<Options, MessageId>({
// Conditional is always necessary if it involves:
// `any` or `unknown` or a naked type parameter
if (
unionTypeParts(type).some(part =>
isTypeFlagSet(
part,
ts.TypeFlags.Any |
ts.TypeFlags.Unknown |
ts.TypeFlags.TypeParameter,
),
unionTypeParts(type).some(
part =>
isTypeAnyType(part) ||
isTypeUnknownType(part) ||
isTypeFlagSet(part, ts.TypeFlags.TypeParameter),
)
) {
return;
Expand All @@ -214,7 +214,7 @@ export default createRule<Options, MessageId>({
}
const type = getNodeType(node);
// Conditional is always necessary if it involves `any` or `unknown`
if (isTypeFlagSet(type, ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
if (isTypeAnyType(type) || isTypeUnknownType(type)) {
return;
}
const messageId = isTypeFlagSet(type, ts.TypeFlags.Never)
Expand Down Expand Up @@ -469,8 +469,8 @@ export default createRule<Options, MessageId>({
? !isNullableOriginFromPrev(node)
: true;
return (
isTypeFlagSet(type, ts.TypeFlags.Any) ||
isTypeFlagSet(type, ts.TypeFlags.Unknown) ||
isTypeAnyType(type) ||
isTypeUnknownType(type) ||
(isNullableType(type, { allowUndefined: true }) && isOwnNullable)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default util.createRule<Options, MessageId>({
return true;
}

if (options.allowAny && util.isTypeFlagSet(type, ts.TypeFlags.Any)) {
if (options.allowAny && util.isTypeAnyType(type)) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ export default util.createRule<Options, MessageId>({
}

if (
types.some(type =>
tsutils.isTypeFlagSet(type, ts.TypeFlags.Any | ts.TypeFlags.Unknown),
types.some(
type => util.isTypeAnyType(type) || util.isTypeUnknownType(type),
)
) {
variantTypes.add('any');
Expand Down
18 changes: 17 additions & 1 deletion packages/eslint-plugin/src/util/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import debug from 'debug';
import {
isCallExpression,
isJsxExpression,
Expand All @@ -13,6 +14,8 @@ import {
} from 'tsutils';
import * as ts from 'typescript';

const log = debug('typescript-eslint:eslint-plugin:utils:types');

/**
* Checks if the given type is either an array type,
* or a union made up solely of array types.
Expand Down Expand Up @@ -326,11 +329,24 @@ export function getTypeArguments(
return type.typeArguments ?? [];
}

/**
* @returns true if the type is `unknown`
*/
export function isTypeUnknownType(type: ts.Type): boolean {
return isTypeFlagSet(type, ts.TypeFlags.Unknown);
}

/**
* @returns true if the type is `any`
*/
export function isTypeAnyType(type: ts.Type): boolean {
return isTypeFlagSet(type, ts.TypeFlags.Any);
if (isTypeFlagSet(type, ts.TypeFlags.Any)) {
if (type.intrinsicName === 'error') {
log('Found an "error" any type');
}
return true;
}
return false;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions packages/eslint-plugin/typings/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@ declare module 'typescript' {
*/
getTypeOfPropertyOfType(type: Type, propertyName: string): Type | undefined;
}

interface Type {
/**
* If the type is `any`, and this is set to "error", then TS was unable to resolve the type
*/
intrinsicName?: string;
}
}