Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion packages/eslint-plugin/src/rules/no-base-to-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,36 @@ export default createRule<Options, MessageIds>({
return Usefulness.Always;
}

function hasBaseTypes(type: ts.Type): type is ts.InterfaceType {
return (
tsutils.isObjectType(type) &&
tsutils.isObjectFlagSet(
type,
ts.ObjectFlags.Interface | ts.ObjectFlags.Class,
)
);
}

function isIgnoredTypeOrBase(
type: ts.Type,
seen = new Set<ts.Type>(),
): boolean {
if (seen.has(type)) {
return false;
}

seen.add(type);

const typeName = getTypeName(checker, type);
return (
ignoredTypeNames.includes(typeName) ||
(hasBaseTypes(type) &&
checker
.getBaseTypes(type)
.some(base => isIgnoredTypeOrBase(base, seen)))
);
}

function collectToStringCertainty(
type: ts.Type,
visited: Set<ts.Type>,
Expand Down Expand Up @@ -249,7 +279,7 @@ export default createRule<Options, MessageIds>({
return Usefulness.Always;
}

if (ignoredTypeNames.includes(getTypeName(checker, type))) {
if (isIgnoredTypeOrBase(type)) {
return Usefulness.Always;
}

Expand Down
96 changes: 96 additions & 0 deletions packages/eslint-plugin/tests/rules/no-base-to-string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,47 @@ error.toString();
`,
options: [{ ignoredTypeNames: ['MyError'] }],
},
{
code: `
interface Animal {}
interface Serializable {}
interface Cat extends Animal, Serializable {}

declare const whiskers: Cat;
whiskers.toString();
`,
options: [{ ignoredTypeNames: ['Animal'] }],
},
{
code: `
interface MyError extends Error {}

declare const error: MyError;
error.toString();
`,
},
{
code: `
class UnknownBase {}
class CustomError extends UnknownBase {}

declare const err: CustomError;
err.toString();
`,
options: [{ ignoredTypeNames: ['UnknownBase'] }],
},
{
code: `
interface Animal {}
interface Dog extends Animal {}
interface Cat extends Animal {}

declare const dog: Dog;
declare const cat: Cat;
cat.toString();
`,
options: [{ ignoredTypeNames: ['Animal'] }],
},
`
function String(value) {
return value;
Expand Down Expand Up @@ -2265,5 +2306,60 @@ v.join();
},
],
},
{
code: `
interface Dog extends Animal {}

declare const labrador: Dog;
labrador.toString();
`,
errors: [
{
data: {
certainty: 'will',
name: 'labrador',
},
messageId: 'baseToString',
},
],
},
{
code: `
interface A extends B {}
interface B extends A {}

declare const a: A;
a.toString();
`,
errors: [
{
data: {
certainty: 'will',
name: 'a',
},
messageId: 'baseToString',
},
],
},
{
code: `
interface Base {}
interface Left extends Base {}
interface Right extends Base {}
interface Diamond extends Left, Right {}

declare const d: Diamond;
d.toString();
`,
errors: [
{
data: {
certainty: 'will',
name: 'd',
},
messageId: 'baseToString',
},
],
},
],
});
Loading