Skip to content

Commit

Permalink
fix: no longer removing Map or Set type when they are needed
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiesonthesky committed Apr 3, 2024
1 parent be900f9 commit 6a6c5ae
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
21 changes: 16 additions & 5 deletions src/shared/comparisons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,22 @@ export const declaredInitializedTypeNodeIsRedundant = (
return undefined;
}

return declaredTypeIsEquivalent(
request.services.program.getTypeChecker(),
declaredType,
initializedType,
);
const typeChecker = request.services.program.getTypeChecker();

const declaredEspacedName = declaredType.getSymbol()?.getEscapedName();

// This is brute-force way to keep Map<string, number> comparison to Map without type arguments...
// This will allow many type declarations that could be removed.
if (
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
(declaredEspacedName === "Map" || declaredEspacedName === "set") &&
typeChecker.typeToString(declaredType) !=
typeChecker.typeToString(initializedType)
) {
return false;
}

return declaredTypeIsEquivalent(typeChecker, declaredType, initializedType);
};

const declaredTypeIsEquivalent = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
let letInferableNullOrStrings = [null, ""];
let letInferableNumberOrRegExps = [0, /./];

// Non-inferable const arrays
const constNonInferableStringArray: string[] = [];

// Non-inferable const multi-type arrays
const constNonInferableNullOrStrings: (null | string)[] = [null];
const constNonInferableNumberOrRegExps: (number | RegExp)[] = [0];
Expand Down Expand Up @@ -87,7 +90,8 @@
// should keep their types
// map
type TypeSummariesPerNodeByName = Map<string, number>;
const incompleteTypes = new Map();
const incompleteTypes: TypeSummariesPerNodeByName = new Map();
const stringSet = new Set();
// array
interface Mutation {
readonly range: number;
Expand All @@ -102,4 +106,14 @@
const fixIncompleteImplicitClassGenerics: FileMutator = (
request: FileMutationsRequest,
) => undefined;

// should lose their types
const incompleteTypes2: TypeSummariesPerNodeByName = new Map<
string,
number
>();
const stringMapTyped = new Map<string, number>();
const anyMap = new Map();
const anySet = new Set();
const anyArray: any[] = [];
})();
14 changes: 14 additions & 0 deletions test/cases/fixes/noInferableTypes/variableDeclarations/original.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
let letInferableNullOrStrings: (null | string)[] = [null, ""];
let letInferableNumberOrRegExps: (number | RegExp)[] = [0, /./];

// Non-inferable const arrays
const constNonInferableStringArray: string[] = [];

// Non-inferable const multi-type arrays
const constNonInferableNullOrStrings: (null | string)[] = [null];
const constNonInferableNumberOrRegExps: (number | RegExp)[] = [0];
Expand Down Expand Up @@ -88,6 +91,7 @@
// map
type TypeSummariesPerNodeByName = Map<string, number>;
const incompleteTypes: TypeSummariesPerNodeByName = new Map();
const stringSet: Set<string> = new Set();
// array
interface Mutation {
readonly range: number;
Expand All @@ -102,4 +106,14 @@
const fixIncompleteImplicitClassGenerics: FileMutator = (
request: FileMutationsRequest,
) => undefined;

// should lose their types
const incompleteTypes2: TypeSummariesPerNodeByName = new Map<
string,
number
>();
const stringMapTyped: Map<string, number> = new Map<string, number>();
const anyMap: Map<any, any> = new Map();
const anySet: Set<any> = new Set();
const anyArray: any[] = [];
})();

0 comments on commit 6a6c5ae

Please sign in to comment.