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

fix(eslint-plugin): [no-redundant-type-constituents] incorrectly marks & string as redundant #8282

Merged
merged 6 commits into from Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
Expand Up @@ -272,6 +272,10 @@ export default createRule({
PrimitiveTypeFlag,
TSESTree.TypeNode[]
>();
const seenUnionTypes = new Map<
TSESTree.TypeNode,
TypeFlagsWithName[]
>();

function checkIntersectionBottomAndTopTypes(
{ typeFlags, typeName }: TypeFlagsWithName,
Expand Down Expand Up @@ -323,8 +327,58 @@ export default createRule({
}
}
}
}

typePartFlags.length >= 2
? seenUnionTypes.set(typeNode, typePartFlags)
: null;
arka1002 marked this conversation as resolved.
Show resolved Hide resolved
}
const checkIfUnionsAreAssignable = (): undefined => {
let typeFlagsOfUnions: ts.TypeFlags[] = [];
let result = true;
arka1002 marked this conversation as resolved.
Show resolved Hide resolved
let primitiveUnit: number;
arka1002 marked this conversation as resolved.
Show resolved Hide resolved

seenUnionTypes.forEach((value, key) => {
arka1002 marked this conversation as resolved.
Show resolved Hide resolved
arka1002 marked this conversation as resolved.
Show resolved Hide resolved
value.forEach(union => {
typeFlagsOfUnions.push(union.typeFlags);
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Style] Is there a need to have a shared variable outside the forEach? Switching it to being inside the .forEach and never modified doesn't fail any unit tests:

Suggested change
value.forEach(union => {
typeFlagsOfUnions.push(union.typeFlags);
});
const typeFlagsOfUnions = unionTypeFlags.flatMap(
union =>
union.typeFlags as keyof typeof literalToPrimitiveTypeFlags,
);

That includes an extra tip: since we know the type flags are all the keyof typeof literalToPrimitiveTypeFlags, using the assertion early on means we don't need multiple assertions later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the variable, I dont think we need it anymore.
I might need some help with the type assertions though 😅

for (const iterator of typeFlagsOfUnions) {
arka1002 marked this conversation as resolved.
Show resolved Hide resolved
if (
seenPrimitiveTypes.has(
literalToPrimitiveTypeFlags[
iterator as keyof typeof literalToPrimitiveTypeFlags
],
)
) {
result = true;
primitiveUnit =
literalToPrimitiveTypeFlags[
iterator as keyof typeof literalToPrimitiveTypeFlags
];
arka1002 marked this conversation as resolved.
Show resolved Hide resolved
} else {
result = false;
break;
arka1002 marked this conversation as resolved.
Show resolved Hide resolved
}
}
if (result) {
context.report({
data: {
literal: value.map(name => name.typeName).join(' | '),
primitive:
primitiveTypeFlagNames[
primitiveUnit as keyof typeof primitiveTypeFlagNames
],
},
messageId: 'primitiveOverridden',
node: key,
});
}
typeFlagsOfUnions = [];
arka1002 marked this conversation as resolved.
Show resolved Hide resolved
});
};
if (seenUnionTypes.size > 0) {
checkIfUnionsAreAssignable();
return;
}
// For each primitive type of all the seen primitive types,
// if there was a literal type seen that overrides it,
// report each of the primitive type's type nodes
Expand Down
Expand Up @@ -156,6 +156,10 @@ ruleTester.run('no-redundant-type-constituents', rule, {
type B = string;
type T = B & null;
`,
`
type T = 'a' | 1;
type U = T & string;
`,
{
code: 'type T = `${string}` & null;',
dependencyConstraints: {
Expand Down Expand Up @@ -804,5 +808,46 @@ ruleTester.run('no-redundant-type-constituents', rule, {
},
],
},
{
code: `
type T = 'a' | 'b';
type U = T & string;
`,
errors: [
{
column: 18,
data: {
literal: '"a" | "b"',
primitive: 'string',
},
messageId: 'primitiveOverridden',
},
],
},
{
code: `
type S = 1 | 2;
type T = 'a' | 'b';
type U = S & T & string & number;
`,
errors: [
{
column: 18,
data: {
literal: '1 | 2',
primitive: 'number',
},
messageId: 'primitiveOverridden',
},
{
column: 22,
data: {
literal: '"a" | "b"',
primitive: 'string',
},
messageId: 'primitiveOverridden',
},
],
},
],
});