Skip to content

Commit

Permalink
fix(eslint-plugin): [no-base-to-string] handle intersection types (#2170
Browse files Browse the repository at this point in the history
)
  • Loading branch information
bogdanb committed Jun 19, 2020
1 parent 4c9293b commit 9cca3a9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/eslint-plugin/src/rules/no-base-to-string.ts
Expand Up @@ -112,6 +112,18 @@ export default util.createRule<Options, MessageIds>({
return Usefulness.Always;
}

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

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

return 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',
},
],
},
],
});

0 comments on commit 9cca3a9

Please sign in to comment.