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

feat(experimental-utils): moved types eslint-plugin util to experimental-utils #3317

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
649a699
chore(experimental-utils): moved types eslint-plugin util to experime…
lnguyenfln Apr 26, 2021
32bf7ac
Merge branch 'master' into master
bradzacher May 15, 2021
01e026a
feat(experimental-utils): moved types eslint-plugin util to experimen…
lnguyenfln May 19, 2021
95fd90c
feat(experimental-utils): moved types eslint-plugin util to experimen…
lnguyenfln Jun 1, 2021
2a4fd07
feat(experimental-utils): moved types eslint-plugin util to experimen…
lnguyenfln Jun 1, 2021
bed4a9e
feat(experimental-utils): moved types eslint-plugin util to experimen…
detljh Jun 7, 2021
137e3fd
chore(experimental-utils): moved types eslint-plugin util to experime…
lnguyenfln Apr 26, 2021
9f4a4d6
feat(experimental-utils): moved types eslint-plugin util to experimen…
lnguyenfln May 19, 2021
eea8ef6
feat(experimental-utils): moved types eslint-plugin util to experimen…
lnguyenfln Jun 1, 2021
0a7893e
feat(experimental-utils): moved types eslint-plugin util to experimen…
lnguyenfln Jun 1, 2021
13fdf56
feat(experimental-utils): moved types eslint-plugin util to experimen…
detljh Jun 7, 2021
8fb132b
feat(experimental-utils): moved types eslint-plugin util to experimen…
detljh Jun 18, 2021
dd7bce1
Merge branch 'master' of https://github.com/detljh/typescript-eslint
detljh Jun 18, 2021
5ece2d9
feat(experimental-utils): moved types eslint-plugin util to experimen…
lnguyenfln Apr 26, 2021
e7248b5
Merge branch 'master' into master
detljh Jun 21, 2021
77926eb
Merge branch 'master' into master
detljh Jun 29, 2021
fbf346d
Merge branch 'master' into master
detljh Jul 14, 2021
466ed2c
Merge branch 'master' of https://github.com/detljh/typescript-eslint
detljh Jul 14, 2021
2853bde
feat(experimental-utils): moved types eslint-plugin util to experimen…
detljh Jul 14, 2021
be008bf
Merge branch 'main'
armano2 Dec 17, 2021
e2e2903
fix: remove typescript-eslint/parser to dev dep
armano2 Dec 17, 2021
1a47a2a
Merge branch 'main'
armano2 Dec 21, 2021
09d5ee9
fix(eslint-plugin): correct circular dependency
armano2 Dec 21, 2021
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
5 changes: 3 additions & 2 deletions packages/eslint-plugin/src/rules/await-thenable.ts
@@ -1,3 +1,4 @@
import { ESLintUtils } from '@typescript-eslint/experimental-utils';
import * as tsutils from 'tsutils';

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

if (
!util.isTypeAnyType(type) &&
!util.isTypeUnknownType(type) &&
!ESLintUtils.isTypeAnyType(type) &&
!ESLintUtils.isTypeUnknownType(type) &&
!tsutils.isThenableType(checker, originalNode.expression, type)
) {
context.report({
Expand Down
11 changes: 8 additions & 3 deletions packages/eslint-plugin/src/rules/no-base-to-string.ts
@@ -1,6 +1,7 @@
import {
TSESTree,
AST_NODE_TYPES,
ESLintUtils,
} from '@typescript-eslint/experimental-utils';
import * as ts from 'typescript';

Expand Down Expand Up @@ -99,7 +100,9 @@ export default util.createRule<Options, MessageIds>({
return Usefulness.Always;
}

if (ignoredTypeNames.includes(util.getTypeName(typeChecker, type))) {
if (
ignoredTypeNames.includes(ESLintUtils.getTypeName(typeChecker, type))
) {
return Usefulness.Always;
}

Expand Down Expand Up @@ -165,9 +168,11 @@ export default util.createRule<Options, MessageIds>({
parserServices.esTreeNodeToTSNodeMap.get(node.right),
);

if (util.getTypeName(typeChecker, leftType) === 'string') {
if (ESLintUtils.getTypeName(typeChecker, leftType) === 'string') {
checkExpression(node.right, rightType);
} else if (util.getTypeName(typeChecker, rightType) === 'string') {
} else if (
ESLintUtils.getTypeName(typeChecker, rightType) === 'string'
) {
checkExpression(node.left, leftType);
}
},
Expand Down
Expand Up @@ -2,6 +2,7 @@ import {
AST_NODE_TYPES,
TSESLint,
TSESTree,
ESLintUtils,
} from '@typescript-eslint/experimental-utils';
import * as tsutils from 'tsutils';
import * as ts from 'typescript';
Expand Down Expand Up @@ -85,7 +86,7 @@ export default util.createRule<Options, MessageId>({
const parserServices = util.getParserServices(context);
const checker = parserServices.program.getTypeChecker();
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
const type = util.getConstrainedTypeAtLocation(checker, tsNode);
const type = ESLintUtils.getConstrainedTypeAtLocation(checker, tsNode);
if (!tsutils.isTypeFlagSet(type, ts.TypeFlags.VoidLike)) {
// not a void expression
return;
Expand Down
5 changes: 3 additions & 2 deletions packages/eslint-plugin/src/rules/no-for-in-array.ts
@@ -1,3 +1,4 @@
import { ESLintUtils } from '@typescript-eslint/experimental-utils';
import * as ts from 'typescript';
import * as util from '../util';

Expand Down Expand Up @@ -25,13 +26,13 @@ export default util.createRule({
const checker = parserServices.program.getTypeChecker();
const originalNode = parserServices.esTreeNodeToTSNodeMap.get(node);

const type = util.getConstrainedTypeAtLocation(
const type = ESLintUtils.getConstrainedTypeAtLocation(
checker,
originalNode.expression,
);

if (
util.isTypeArrayTypeOrUnionOfArrayTypes(type, checker) ||
ESLintUtils.isTypeArrayTypeOrUnionOfArrayTypes(type, checker) ||
(type.flags & ts.TypeFlags.StringLike) !== 0
) {
context.report({
Expand Down
5 changes: 3 additions & 2 deletions packages/eslint-plugin/src/rules/no-throw-literal.ts
Expand Up @@ -3,6 +3,7 @@ import * as util from '../util';
import {
TSESTree,
AST_NODE_TYPES,
ESLintUtils,
} from '@typescript-eslint/experimental-utils';

export default util.createRule({
Expand Down Expand Up @@ -79,8 +80,8 @@ export default util.createRule({
}

if (
util.isTypeAnyType(type) ||
util.isTypeUnknownType(type) ||
ESLintUtils.isTypeAnyType(type) ||
ESLintUtils.isTypeUnknownType(type) ||
isErrorLike(type)
) {
return;
Expand Down
@@ -1,6 +1,7 @@
import {
AST_NODE_TYPES,
TSESTree,
ESLintUtils,
} from '@typescript-eslint/experimental-utils';
import * as tsutils from 'tsutils';
import * as ts from 'typescript';
Expand Down Expand Up @@ -159,7 +160,7 @@ export default util.createRule<Options, MessageIds>({
function deconstructComparison(
node: TSESTree.BinaryExpression,
): BooleanComparison | undefined {
const comparisonType = util.getEqualsKind(node.operator);
const comparisonType = ESLintUtils.getEqualsKind(node.operator);
if (!comparisonType) {
return undefined;
}
Expand Down
56 changes: 32 additions & 24 deletions packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
Expand Up @@ -2,6 +2,7 @@ import {
TSESTree,
AST_NODE_TYPES,
AST_TOKEN_TYPES,
ESLintUtils,
} from '@typescript-eslint/experimental-utils';
import * as ts from 'typescript';
import {
Expand All @@ -13,17 +14,11 @@ import {
isStrictCompilerOptionEnabled,
} from 'tsutils';
import {
isTypeFlagSet,
createRule,
getParserServices,
getConstrainedTypeAtLocation,
isNullableType,
nullThrows,
NullThrowsReasons,
isIdentifier,
isTypeAnyType,
isTypeUnknownType,
getTypeName,
getTypeOfPropertyOfName,
} from '../util';

Expand All @@ -37,15 +32,15 @@ const isPossiblyFalsy = (type: ts.Type): boolean =>
// PossiblyFalsy flag includes literal values, so exclude ones that
// are definitely truthy
.filter(t => !isTruthyLiteral(t))
.some(type => isTypeFlagSet(type, ts.TypeFlags.PossiblyFalsy));
.some(type => ESLintUtils.isTypeFlagSet(type, ts.TypeFlags.PossiblyFalsy));

const isPossiblyTruthy = (type: ts.Type): boolean =>
unionTypeParts(type).some(type => !isFalsyType(type));

// Nullish utilities
const nullishFlag = ts.TypeFlags.Undefined | ts.TypeFlags.Null;
const isNullishType = (type: ts.Type): boolean =>
isTypeFlagSet(type, nullishFlag);
ESLintUtils.isTypeFlagSet(type, nullishFlag);

const isPossiblyNullish = (type: ts.Type): boolean =>
unionTypeParts(type).some(isNullishType);
Expand Down Expand Up @@ -169,7 +164,7 @@ export default createRule<Options, MessageId>({

function getNodeType(node: TSESTree.Node): ts.Type {
const tsNode = service.esTreeNodeToTSNodeMap.get(node);
return getConstrainedTypeAtLocation(checker, tsNode);
return ESLintUtils.getConstrainedTypeAtLocation(checker, tsNode);
}

function nodeIsArrayType(node: TSESTree.Expression): boolean {
Expand Down Expand Up @@ -238,16 +233,16 @@ export default createRule<Options, MessageId>({
if (
unionTypeParts(type).some(
part =>
isTypeAnyType(part) ||
isTypeUnknownType(part) ||
isTypeFlagSet(part, ts.TypeFlags.TypeParameter),
ESLintUtils.isTypeAnyType(part) ||
ESLintUtils.isTypeUnknownType(part) ||
ESLintUtils.isTypeFlagSet(part, ts.TypeFlags.TypeParameter),
)
) {
return;
}
let messageId: MessageId | null = null;

if (isTypeFlagSet(type, ts.TypeFlags.Never)) {
if (ESLintUtils.isTypeFlagSet(type, ts.TypeFlags.Never)) {
messageId = 'never';
} else if (!isPossiblyTruthy(type)) {
messageId = !isUnaryNotArgument ? 'alwaysFalsy' : 'alwaysTruthy';
Expand All @@ -269,12 +264,15 @@ export default createRule<Options, MessageId>({
}
const type = getNodeType(node);
// Conditional is always necessary if it involves `any` or `unknown`
if (isTypeAnyType(type) || isTypeUnknownType(type)) {
if (
ESLintUtils.isTypeAnyType(type) ||
ESLintUtils.isTypeUnknownType(type)
) {
return;
}

let messageId: MessageId | null = null;
if (isTypeFlagSet(type, ts.TypeFlags.Never)) {
if (ESLintUtils.isTypeFlagSet(type, ts.TypeFlags.Never)) {
messageId = 'never';
} else if (!isPossiblyNullish(type)) {
messageId = 'neverNullish';
Expand Down Expand Up @@ -335,7 +333,7 @@ export default createRule<Options, MessageId>({
flag |= NULL | UNDEFINED;
}

return isTypeFlagSet(type, flag);
return ESLintUtils.isTypeFlagSet(type, flag);
};

if (
Expand Down Expand Up @@ -451,7 +449,12 @@ export default createRule<Options, MessageId>({
return;
}
// Predicate is always necessary if it involves `any` or `unknown`
if (returnTypes.some(t => isTypeAnyType(t) || isTypeUnknownType(t))) {
if (
returnTypes.some(
t =>
ESLintUtils.isTypeAnyType(t) || ESLintUtils.isTypeUnknownType(t),
)
) {
return;
}
if (!returnTypes.some(isPossiblyFalsy)) {
Expand Down Expand Up @@ -510,10 +513,10 @@ export default createRule<Options, MessageId>({
propertyType.value.toString(),
);
if (propType) {
return isNullableType(propType, { allowUndefined: true });
return ESLintUtils.isNullableType(propType, { allowUndefined: true });
}
}
const typeName = getTypeName(checker, propertyType);
const typeName = ESLintUtils.getTypeName(checker, propertyType);
return !!(
(typeName === 'string' &&
checker.getIndexInfoOfType(objType, ts.IndexKind.String)) ||
Expand Down Expand Up @@ -546,10 +549,14 @@ export default createRule<Options, MessageId>({
type,
property.name,
);
return propType && isNullableType(propType, { allowUndefined: true });
return (
propType &&
ESLintUtils.isNullableType(propType, { allowUndefined: true })
);
});
return (
!isOwnNullable && isNullableType(prevType, { allowUndefined: true })
!isOwnNullable &&
ESLintUtils.isNullableType(prevType, { allowUndefined: true })
);
}
return false;
Expand All @@ -564,9 +571,10 @@ export default createRule<Options, MessageId>({
? !isNullableOriginFromPrev(node)
: true;
return (
isTypeAnyType(type) ||
isTypeUnknownType(type) ||
(isNullableType(type, { allowUndefined: true }) && isOwnNullable)
ESLintUtils.isTypeAnyType(type) ||
ESLintUtils.isTypeUnknownType(type) ||
(ESLintUtils.isNullableType(type, { allowUndefined: true }) &&
isOwnNullable)
);
}

Expand Down
22 changes: 13 additions & 9 deletions packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts
@@ -1,6 +1,7 @@
import {
TSESTree,
AST_NODE_TYPES,
ESLintUtils,
} from '@typescript-eslint/experimental-utils';
import {
isObjectType,
Expand Down Expand Up @@ -93,7 +94,7 @@ export default util.createRule<Options, MessageIds>({
* Returns true if there's a chance the variable has been used before a value has been assigned to it
*/
function isPossiblyUsedBeforeAssigned(node: ts.Expression): boolean {
const declaration = util.getDeclaration(checker, node);
const declaration = ESLintUtils.getDeclaration(checker, node);
if (!declaration) {
// don't know what the declaration is for some reason, so just assume the worst
return true;
Expand All @@ -112,7 +113,7 @@ export default util.createRule<Options, MessageIds>({
) {
// check if the defined variable type has changed since assignment
const declarationType = checker.getTypeFromTypeNode(declaration.type);
const type = util.getConstrainedTypeAtLocation(checker, node);
const type = ESLintUtils.getConstrainedTypeAtLocation(checker, node);
if (declarationType === type) {
// possibly used before assigned, so just skip it
// better to false negative and skip it, than false positive and fix to compile erroring code
Expand Down Expand Up @@ -160,12 +161,12 @@ export default util.createRule<Options, MessageIds>({

const originalNode = parserServices.esTreeNodeToTSNodeMap.get(node);

const type = util.getConstrainedTypeAtLocation(
const type = ESLintUtils.getConstrainedTypeAtLocation(
checker,
originalNode.expression,
);

if (!util.isNullableType(type)) {
if (!ESLintUtils.isNullableType(type)) {
if (isPossiblyUsedBeforeAssigned(originalNode.expression)) {
return;
}
Expand All @@ -184,24 +185,27 @@ export default util.createRule<Options, MessageIds>({
// we know it's a nullable type
// so figure out if the variable is used in a place that accepts nullable types

const contextualType = util.getContextualType(checker, originalNode);
const contextualType = ESLintUtils.getContextualType(
checker,
originalNode,
);
if (contextualType) {
// in strict mode you can't assign null to undefined, so we have to make sure that
// the two types share a nullable type
const typeIncludesUndefined = util.isTypeFlagSet(
const typeIncludesUndefined = ESLintUtils.isTypeFlagSet(
type,
ts.TypeFlags.Undefined,
);
const typeIncludesNull = util.isTypeFlagSet(
const typeIncludesNull = ESLintUtils.isTypeFlagSet(
type,
ts.TypeFlags.Null,
);

const contextualTypeIncludesUndefined = util.isTypeFlagSet(
const contextualTypeIncludesUndefined = ESLintUtils.isTypeFlagSet(
contextualType,
ts.TypeFlags.Undefined,
);
const contextualTypeIncludesNull = util.isTypeFlagSet(
const contextualTypeIncludesNull = ESLintUtils.isTypeFlagSet(
contextualType,
ts.TypeFlags.Null,
);
Expand Down