Skip to content

Commit

Permalink
chore: add type cache
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg committed Jan 23, 2022
1 parent ff49065 commit 9742e45
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions packages/eslint-plugin/src/rules/no-redundant-type-constituents.ts
@@ -1,7 +1,4 @@
import {
TSESTree,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import { TSESTree, AST_NODE_TYPES } from '@typescript-eslint/utils';
import * as tsutils from 'tsutils';
import * as ts from 'typescript';
import * as util from '../util';
Expand Down Expand Up @@ -193,6 +190,7 @@ export default util.createRule({
defaultOptions: [],
create(context) {
const parserServices = util.getParserServices(context);
const typesCache = new Map<TSESTree.TypeNode, TypeFlagsWithName[]>();

function getTypeNodeTypePartFlags(
typeNode: TSESTree.TypeNode,
Expand Down Expand Up @@ -238,8 +236,21 @@ export default util.createRule({
}));
}

function getTypeNodeTypePartFlagsCached(
typeNode: TSESTree.TypeNode,
): TypeFlagsWithName[] {
const existing = typesCache.get(typeNode);
if (existing) {
return existing;
}

const created = getTypeNodeTypePartFlags(typeNode);
typesCache.set(typeNode, created);
return created;
}

return {
TSIntersectionType(node): void {
'TSIntersectionType:exit'(node: TSESTree.TSIntersectionType): void {
const seenLiteralTypes = new Map<PrimitiveTypeFlag, string[]>();
const seenPrimitiveTypes = new Map<
PrimitiveTypeFlag,
Expand Down Expand Up @@ -272,7 +283,7 @@ export default util.createRule({
}

for (const typeNode of node.types) {
const typePartFlags = getTypeNodeTypePartFlags(typeNode);
const typePartFlags = getTypeNodeTypePartFlagsCached(typeNode);

for (const typePart of typePartFlags) {
if (checkIntersectionBottomAndTopTypes(typePart, typeNode)) {
Expand Down Expand Up @@ -317,7 +328,7 @@ export default util.createRule({
}
}
},
TSUnionType(node): void {
'TSUnionType:exit'(node: TSESTree.TSUnionType): void {
const seenLiteralTypes = new Map<
PrimitiveTypeFlag,
TypeNodeWithValue[]
Expand Down Expand Up @@ -364,7 +375,7 @@ export default util.createRule({
}

for (const typeNode of node.types) {
const typePartFlags = getTypeNodeTypePartFlags(typeNode);
const typePartFlags = getTypeNodeTypePartFlagsCached(typeNode);

for (const typePart of typePartFlags) {
if (checkUnionBottomAndTopTypes(typePart, typeNode)) {
Expand Down

0 comments on commit 9742e45

Please sign in to comment.