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] handle intersection types #2170

Merged
merged 4 commits into from Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 18 additions & 0 deletions packages/eslint-plugin/src/rules/no-base-to-string.ts
Expand Up @@ -112,6 +112,24 @@ export default util.createRule<Options, MessageIds>({
return Usefulness.Always;
}

if (type.isIntersection()) {
let someSubtypeUseful = false;

for (const subType of type.types) {
const subtypeUsefulness = collectToStringCertainty(subType);

if (subtypeUsefulness === Usefulness.Always) {
return Usefulness.Always;
}

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

return someSubtypeUseful ? Usefulness.Sometimes : Usefulness.Never;
}

if (!type.isUnion()) {
return Usefulness.Never;
}
Expand Down
24 changes: 24 additions & 0 deletions packages/eslint-plugin/tests/rules/no-base-to-string.test.ts
Expand Up @@ -107,6 +107,12 @@ tag\`\${{}}\`;
function tag() {}
tag\`\${{}}\`;
`,
`
interface Brand {}
function test(v: string & Brand): string {
return v + '';
}
`,
],
invalid: [
{
Expand Down Expand Up @@ -217,5 +223,23 @@ tag\`\${{}}\`;
},
],
},
{
code: `
interface A {}
interface B {}
function test(intersection: A & B): string {
return intersection + '';
}
`,
errors: [
{
data: {
certainty: 'will',
name: 'intersection',
},
messageId: 'baseToString',
},
],
},
],
});