Skip to content

feat(eslint-plugin): [no-unnecessary-type-assertion] improve reporting of literal types (POC) #10631

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
74 changes: 72 additions & 2 deletions packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import type { Scope } from '@typescript-eslint/scope-manager';
import type { TSESTree } from '@typescript-eslint/utils';
import type { ReportFixFunction } from '@typescript-eslint/utils/ts-eslint';

import { AST_NODE_TYPES, AST_TOKEN_TYPES } from '@typescript-eslint/utils';
import {
AST_NODE_TYPES,
AST_TOKEN_TYPES,
ASTUtils,
} from '@typescript-eslint/utils';
import * as tsutils from 'ts-api-utils';
import * as ts from 'typescript';

Expand Down Expand Up @@ -208,6 +212,63 @@ export default createRule<Options, MessageIds>({
return false;
}

function doesExpressionHaveFreshLiterals(
node: TSESTree.Expression,
): boolean {
// Actual literals don't seem to have the type of a "fresh literal"
if (
node.type === AST_NODE_TYPES.TemplateLiteral ||
node.type === AST_NODE_TYPES.Literal
) {
return true;
}

if (node.type === AST_NODE_TYPES.Identifier) {
// It seems "fresh literals" have a slightly different type at their
// definition rather than their reference.
const scope = context.sourceCode.getScope(node);
const superVar = ASTUtils.findVariable(scope, node.name);

if (superVar == null) {
return true;
}

const definition = superVar.defs.at(0);

if (definition == null) {
return true;
}

const typeAtDefinition = services.getTypeAtLocation(definition.node);

if (tsutils.isUnionType(typeAtDefinition)) {
return tsutils
.unionTypeParts(typeAtDefinition)
.some(part => isFreshLiteralType(part));
}

return isFreshLiteralType(typeAtDefinition);
}

if (node.type === AST_NODE_TYPES.ConditionalExpression) {
return (
doesExpressionHaveFreshLiterals(node.alternate) ||
doesExpressionHaveFreshLiterals(node.consequent)
);
}

if (
tsutils.isTypeFlagSet(
services.getTypeAtLocation(node),
ts.TypeFlags.EnumLiteral | ts.TypeFlags.UniqueESSymbol,
)
) {
return true;
}

return false;
}

return {
'TSAsExpression, TSTypeAssertion'(
node: TSESTree.TSAsExpression | TSESTree.TSTypeAssertion,
Expand All @@ -224,7 +285,11 @@ export default createRule<Options, MessageIds>({
const uncastType = services.getTypeAtLocation(node.expression);
const typeIsUnchanged = isTypeUnchanged(uncastType, castType);

const wouldSameTypeBeInferred = castType.isLiteral()
const hasFreshLiterals = doesExpressionHaveFreshLiterals(
node.expression,
);

const wouldSameTypeBeInferred = hasFreshLiterals
? isImplicitlyNarrowedConstDeclaration(node)
: !isConstAssertion(node.typeAnnotation);

Expand Down Expand Up @@ -387,3 +452,8 @@ export default createRule<Options, MessageIds>({
};
},
});

// https://github.com/microsoft/TypeScript/blob/21c1a61b49082915f93e3327dad0d73205cf4273/src/compiler/checker.ts/#L19679-L19681
function isFreshLiteralType(type: ts.Type) {
return tsutils.isFreshableType(type) && type.freshType === type;
}
Loading
Loading