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-base-to-string] support boolean in unions #1979

Merged
merged 3 commits into from May 11, 2020
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
21 changes: 19 additions & 2 deletions packages/eslint-plugin/src/rules/no-base-to-string.ts
Expand Up @@ -123,10 +123,27 @@ export default util.createRule<Options, MessageIds>({
return Usefulness.Never;
}

let allSubtypesUseful = true;
let someSubtypeUseful = false;

for (const subType of type.types) {
if (collectToStringCertainty(subType) !== Usefulness.Never) {
return Usefulness.Sometimes;
const subtypeUsefulness = collectToStringCertainty(subType);

if (subtypeUsefulness !== Usefulness.Always && allSubtypesUseful) {
allSubtypesUseful = false;
}

if (subtypeUsefulness !== Usefulness.Never && !someSubtypeUseful) {
someSubtypeUseful = true;
}
}

if (allSubtypesUseful && someSubtypeUseful) {
return Usefulness.Always;
}

if (someSubtypeUseful) {
return Usefulness.Sometimes;
}

return Usefulness.Never;
Expand Down
8 changes: 8 additions & 0 deletions packages/eslint-plugin/tests/rules/no-base-to-string.test.ts
Expand Up @@ -78,6 +78,14 @@ const literalWithToString = {
toString: () => 'Hello, world!',
};
'' + literalToString;
`,
`
const printer = (inVar: string | number | boolean) => {
inVar.toString();
};
printer('');
printer(1);
printer(true);
`,
'let _ = {} * {};',
'let _ = {} / {};',
Expand Down