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

Skip to content

chore(eslint-plugin): switch auto-generated test cases to hand-written in no-base-to-string.test.ts #11273

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

Merged
Merged
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
177 changes: 63 additions & 114 deletions packages/eslint-plugin/tests/rules/no-base-to-string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,33 @@ const ruleTester = new RuleTester({
},
});

/**
* ref: https://github.com/typescript-eslint/typescript-eslint/issues/11043
* Be careful with dynamic test case generation.
* Iterate based on the following cases:
* 1. literalListBasic
* ```
[
"''",
"'text'",
'true',
'false',
'1',
'1n',
'[]',
'/regex/',
];
* ```
* 2. literalListNeedParen
* ```
[
"__dirname === 'foobar'",
'{}.constructor()',
'() => {}',
'function() {}',
];
* ```
*/
ruleTester.run('no-base-to-string', rule, {
valid: [
// template
Expand All @@ -30,7 +57,6 @@ ruleTester.run('no-base-to-string', rule, {
'`${function () {}}`;',

// operator + +=
Copy link
Contributor Author

@nayounsang nayounsang Jun 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many of the test cases in the operator + section are essentially duplicates that only differ in operand order. We could consider reducing them by half.
related to this comment: #11273 (comment)

"'' + '';",
"'' + 'text';",
"'' + true;",
"'' + false;",
Expand All @@ -42,8 +68,6 @@ ruleTester.run('no-base-to-string', rule, {
"'' + {}.constructor();",
"'' + (() => {});",
"'' + function () {};",
"'text' + '';",
"'text' + 'text';",
"'text' + true;",
"'text' + false;",
"'text' + 1;",
Expand All @@ -54,9 +78,6 @@ ruleTester.run('no-base-to-string', rule, {
"'text' + {}.constructor();",
"'text' + (() => {});",
"'text' + function () {};",
"true + '';",
"true + 'text';",
'true + true;',
'true + false;',
'true + 1;',
'true + 1n;',
Expand All @@ -66,10 +87,6 @@ ruleTester.run('no-base-to-string', rule, {
'true + {}.constructor();',
'true + (() => {});',
'true + function () {};',
"false + '';",
"false + 'text';",
'false + true;',
'false + false;',
'false + 1;',
'false + 1n;',
'false + [];',
Expand All @@ -78,102 +95,34 @@ ruleTester.run('no-base-to-string', rule, {
'false + {}.constructor();',
'false + (() => {});',
'false + function () {};',
"1 + '';",
"1 + 'text';",
'1 + true;',
'1 + false;',
'1 + 1;',
'1 + 1n;',
'1 + [];',
'1 + /regex/;',
"1 + (__dirname === 'foobar');",
'1 + {}.constructor();',
'1 + (() => {});',
'1 + function () {};',
"1n + '';",
"1n + 'text';",
'1n + true;',
'1n + false;',
'1n + 1;',
'1n + 1n;',
'1n + [];',
'1n + /regex/;',
"1n + (__dirname === 'foobar');",
'1n + {}.constructor();',
'1n + (() => {});',
'1n + function () {};',
"[] + '';",
"[] + 'text';",
'[] + true;',
'[] + false;',
'[] + 1;',
'[] + 1n;',
'[] + [];',
'[] + /regex/;',
"[] + (__dirname === 'foobar');",
'[] + {}.constructor();',
'[] + (() => {});',
'[] + function () {};',
"/regex/ + '';",
"/regex/ + 'text';",
'/regex/ + true;',
'/regex/ + false;',
'/regex/ + 1;',
'/regex/ + 1n;',
'/regex/ + [];',
'/regex/ + /regex/;',
"/regex/ + (__dirname === 'foobar');",
'/regex/ + {}.constructor();',
'/regex/ + (() => {});',
'/regex/ + function () {};',
"(__dirname === 'foobar') + '';",
"(__dirname === 'foobar') + 'text';",
"(__dirname === 'foobar') + true;",
"(__dirname === 'foobar') + false;",
"(__dirname === 'foobar') + 1;",
"(__dirname === 'foobar') + 1n;",
"(__dirname === 'foobar') + [];",
"(__dirname === 'foobar') + /regex/;",
"(__dirname === 'foobar') + (__dirname === 'foobar');",
"(__dirname === 'foobar') + {}.constructor();",
"(__dirname === 'foobar') + (() => {});",
"(__dirname === 'foobar') + function () {};",
"({}).constructor() + '';",
"({}).constructor() + 'text';",
'({}).constructor() + true;',
'({}).constructor() + false;',
'({}).constructor() + 1;',
'({}).constructor() + 1n;',
'({}).constructor() + [];',
'({}).constructor() + /regex/;',
"({}).constructor() + (__dirname === 'foobar');",
'({}).constructor() + {}.constructor();',
'({}).constructor() + (() => {});',
'({}).constructor() + function () {};',
"(() => {}) + '';",
"(() => {}) + 'text';",
'(() => {}) + true;',
'(() => {}) + false;',
'(() => {}) + 1;',
'(() => {}) + 1n;',
'(() => {}) + [];',
'(() => {}) + /regex/;',
"(() => {}) + (__dirname === 'foobar');",
'(() => {}) + {}.constructor();',
'(() => {}) + (() => {});',
'(() => {}) + function () {};',
"(function () {}) + '';",
"(function () {}) + 'text';",
'(function () {}) + true;',
'(function () {}) + false;',
'(function () {}) + 1;',
'(function () {}) + 1n;',
'(function () {}) + [];',
'(function () {}) + /regex/;',
"(function () {}) + (__dirname === 'foobar');",
'(function () {}) + {}.constructor();',
'(function () {}) + (() => {});',
'(function () {}) + function () {};',

// toString()
"''.toString();",
Expand All @@ -191,64 +140,64 @@ ruleTester.run('no-base-to-string', rule, {

// variable toString() and template
`
let value = '';
value.toString();
let text = \`\${value}\`;
let value = '';
value.toString();
let text = \`\${value}\`;
`,
`
let value = 'text';
value.toString();
let text = \`\${value}\`;
let value = 'text';
value.toString();
let text = \`\${value}\`;
`,
`
let value = true;
value.toString();
let text = \`\${value}\`;
let value = true;
value.toString();
let text = \`\${value}\`;
`,
`
let value = false;
value.toString();
let text = \`\${value}\`;
let value = false;
value.toString();
let text = \`\${value}\`;
`,
`
let value = 1;
value.toString();
let text = \`\${value}\`;
let value = 1;
value.toString();
let text = \`\${value}\`;
`,
`
let value = 1n;
value.toString();
let text = \`\${value}\`;
let value = 1n;
value.toString();
let text = \`\${value}\`;
`,
`
let value = [];
value.toString();
let text = \`\${value}\`;
let value = [];
value.toString();
let text = \`\${value}\`;
`,
`
let value = /regex/;
value.toString();
let text = \`\${value}\`;
let value = /regex/;
value.toString();
let text = \`\${value}\`;
`,
`
let value = __dirname === 'foobar';
value.toString();
let text = \`\${value}\`;
let value = __dirname === 'foobar';
value.toString();
let text = \`\${value}\`;
`,
`
let value = {}.constructor();
value.toString();
let text = \`\${value}\`;
let value = {}.constructor();
value.toString();
let text = \`\${value}\`;
`,
`
let value = () => {};
value.toString();
let text = \`\${value}\`;
let value = () => {};
value.toString();
let text = \`\${value}\`;
`,
`
let value = function () {};
value.toString();
let text = \`\${value}\`;
let value = function () {};
value.toString();
let text = \`\${value}\`;
`,

// String()
Expand Down