Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(eslint-plugin): [no-unnecessary-type-arguments] Use Symbol to che…
…ck if it's the same type (#4543)

Co-authored-by: Josh Goldberg <me@joshuakgoldberg.com>
  • Loading branch information
islandryu and JoshuaKGoldberg committed Feb 12, 2022
1 parent e32bef6 commit 5b7d8df
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/eslint-plugin/src/rules/no-unnecessary-type-arguments.ts
Expand Up @@ -37,7 +37,6 @@ export default util.createRule<[], MessageIds>({
create(context) {
const parserServices = util.getParserServices(context);
const checker = parserServices.program.getTypeChecker();
const sourceCode = context.getSourceCode();

function checkTSArgsAndParameters(
esParameters: TSESTree.TSTypeParameterInstantiation,
Expand All @@ -47,11 +46,20 @@ export default util.createRule<[], MessageIds>({
const i = esParameters.params.length - 1;
const arg = esParameters.params[i];
const param = typeParameters[i];

if (!param?.default) {
return;
}
// TODO: would like checker.areTypesEquivalent. https://github.com/Microsoft/TypeScript/issues/13502
if (
!param?.default ||
param.default.getText() !== sourceCode.getText(arg)
const defaultType = checker.getTypeAtLocation(param.default);
const argTsNode = parserServices.esTreeNodeToTSNodeMap.get(arg);
const argType = checker.getTypeAtLocation(argTsNode);
if (!argType.aliasSymbol && !defaultType.aliasSymbol) {
if (argType.flags !== defaultType.flags) {
return;
}
} else if (
argType.aliasSymbol !== defaultType.aliasSymbol ||
argType.aliasTypeArguments !== defaultType.aliasTypeArguments
) {
return;
}
Expand Down
Expand Up @@ -290,5 +290,32 @@ function bar<T = F<string>>() {}
bar();
`,
},
{
code: `
type DefaultE = { foo: string };
type T<E = DefaultE> = { box: E };
type G = T<DefaultE>;
declare module 'bar' {
type DefaultE = { somethingElse: true };
type G = T<DefaultE>;
}
`,
errors: [
{
line: 4,
column: 12,
messageId: 'unnecessaryTypeParameter',
},
],
output: `
type DefaultE = { foo: string };
type T<E = DefaultE> = { box: E };
type G = T;
declare module 'bar' {
type DefaultE = { somethingElse: true };
type G = T<DefaultE>;
}
`,
},
],
});

0 comments on commit 5b7d8df

Please sign in to comment.