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
18 changes: 18 additions & 0 deletions packages/eslint-plugin/src/rules/no-base-to-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ export type Options = [
},
];
export type MessageIds = 'baseArrayJoin' | 'baseToString';
const canHaveTypeParameters = (declaration: ts.Declaration) => {
return (
ts.isTypeAliasDeclaration(declaration) ||
ts.isInterfaceDeclaration(declaration) ||
ts.isClassDeclaration(declaration)
);
};

export default createRule<Options, MessageIds>({
name: 'no-base-to-string',
Expand Down Expand Up @@ -231,6 +238,17 @@ export default createRule<Options, MessageIds>({
return Usefulness.Always;
}

const symbol = type.aliasSymbol ?? type.getSymbol();
const decl = symbol?.getDeclarations()?.[0];
if (
decl &&
canHaveTypeParameters(decl) &&
decl.typeParameters &&
ignoredTypeNames.includes(symbol.name)
) {
return Usefulness.Always;
}

if (ignoredTypeNames.includes(getTypeName(checker, type))) {
return Usefulness.Always;
}
Expand Down
24 changes: 24 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 @@ -300,6 +300,30 @@ String(foo);
`,
options: [{ ignoredTypeNames: ['Foo'] }],
},
{
code: `
interface MyError<T> {}
declare const error: MyError<number>;
error.toString();
`,
options: [{ ignoredTypeNames: ['MyError'] }],
},
{
code: `
type MyError<T> = {};
declare const error: MyError<number>;
error.toString();
`,
options: [{ ignoredTypeNames: ['MyError'] }],
},
{
code: `
class MyError<T> {}
declare const error: MyError<number>;
error.toString();
`,
options: [{ ignoredTypeNames: ['MyError'] }],
},
`
function String(value) {
return value;
Expand Down
Loading