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-unnecessary-condition] false positives with branded types #7466

Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,23 @@ const isTruthyLiteral = (type: ts.Type): boolean =>
const isPossiblyFalsy = (type: ts.Type): boolean =>
tsutils
.unionTypeParts(type)
// Intersections like `string & {}` can also be possibly falsy,
// requiring us to look into the intersection.
.flatMap(type => tsutils.intersectionTypeParts(type))
Comment on lines +31 to +33
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This works because:

  • if it is not an intersection, intersectionTypeParts(type) just returns the original type, making the function behave like before
  • if it is something like string & {}, intersectionTypeParts(type) returns an array containing the types string and {}. string in this example is possibly falsy, making our function correctly report that the entire thing is possibly falsy
  • if we have something like string & number, the type checker directly reports this type as never, i.e. here we shouldn't even see that this was ever defined as an intersection in the first place

Copy link
Member

Choose a reason for hiding this comment

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

[External] Heh, I wonder if ts-api-utils should export a function like typeParts that essentially calls intersectionTypeParts(unionTypeParts(type))...? Not a blocker for this PR, just ruminating.

JoshuaKGoldberg/ts-api-utils#258

// PossiblyFalsy flag includes literal values, so exclude ones that
// are definitely truthy
.filter(t => !isTruthyLiteral(t))
.some(type => isTypeFlagSet(type, ts.TypeFlags.PossiblyFalsy));

const isPossiblyTruthy = (type: ts.Type): boolean =>
tsutils.unionTypeParts(type).some(type => !tsutils.isFalsyType(type));
tsutils
.unionTypeParts(type)
.map(type => tsutils.intersectionTypeParts(type))
.some(intersectionParts =>
// It is possible to define intersections that are always falsy,
// like `"" & { __brand: string }`.
intersectionParts.every(type => !tsutils.isFalsyType(type)),
);

// Nullish utilities
const nullishFlag = ts.TypeFlags.Undefined | ts.TypeFlags.Null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,34 @@ const result2 = foo() == null;
necessaryConditionTest('null | object'),
necessaryConditionTest('undefined | true'),
necessaryConditionTest('void | true'),
// "branded" type
necessaryConditionTest('string & {}'),
necessaryConditionTest('string & { __brand: string }'),
necessaryConditionTest('number & { __brand: string }'),
necessaryConditionTest('boolean & { __brand: string }'),
necessaryConditionTest('bigint & { __brand: string }'),
necessaryConditionTest('string & {} & { __brand: string }'),
necessaryConditionTest(
'string & { __brandA: string } & { __brandB: string }',
),
necessaryConditionTest('string & { __brand: string } | number'),
necessaryConditionTest('(string | number) & { __brand: string }'),
necessaryConditionTest('string & ({ __brand: string } | number)'),
necessaryConditionTest('("" | "foo") & { __brand: string }'),
necessaryConditionTest(
'(string & { __brandA: string }) | (number & { __brandB: string })',
),
necessaryConditionTest(
'((string & { __brandA: string }) | (number & { __brandB: string }) & ("" | "foo"))',
),
necessaryConditionTest(
'{ __brandA: string} & (({ __brandB: string } & string) | ({ __brandC: string } & number))',
),
necessaryConditionTest(
'(string | number) & ("foo" | 123 | { __brandA: string })',
),

necessaryConditionTest('string & string'),

necessaryConditionTest('any'), // any
necessaryConditionTest('unknown'), // unknown
Expand Down Expand Up @@ -645,6 +673,7 @@ const t1 = b2 && b1 ? 'yes' : 'no';
unnecessaryConditionTest('null', 'alwaysFalsy'),
unnecessaryConditionTest('void', 'alwaysFalsy'),
unnecessaryConditionTest('never', 'never'),
unnecessaryConditionTest('string & number', 'never'),

// More complex logical expressions
{
Expand Down Expand Up @@ -1821,5 +1850,37 @@ foo &&= null;
},
],
},

// "branded" types
unnecessaryConditionTest('"" & {}', 'alwaysFalsy'),
unnecessaryConditionTest('"" & { __brand: string }', 'alwaysFalsy'),
unnecessaryConditionTest(
'("" | false) & { __brand: string }',
'alwaysFalsy',
),
unnecessaryConditionTest(
'((string & { __brandA: string }) | (number & { __brandB: string })) & ""',
'alwaysFalsy',
),
unnecessaryConditionTest(
'("foo" | "bar") & { __brand: string }',
'alwaysTruthy',
),
unnecessaryConditionTest(
'(123 | true) & { __brand: string }',
'alwaysTruthy',
),
unnecessaryConditionTest(
'(string | number) & ("foo" | 123) & { __brand: string }',
'alwaysTruthy',
),
unnecessaryConditionTest(
'((string & { __brandA: string }) | (number & { __brandB: string })) & "foo"',
'alwaysTruthy',
),
unnecessaryConditionTest(
'((string & { __brandA: string }) | (number & { __brandB: string })) & ("foo" | 123)',
'alwaysTruthy',
),
],
});