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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
304 changes: 267 additions & 37 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,55 +13,285 @@ const ruleTester = new RuleTester({
},
});

const literalListBasic: string[] = [
"''",
"'text'",
'true',
'false',
'1',
'1n',
'[]',
'/regex/',
];

const literalListNeedParen: string[] = [
"__dirname === 'foobar'",
'{}.constructor()',
'() => {}',
'function() {}',
];

const literalList = [...literalListBasic, ...literalListNeedParen];

const literalListWrapped = [
...literalListBasic,
...literalListNeedParen.map(i => `(${i})`),
];
/**
* 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
...literalList.map(i => `\`\${${i}}\`;`),
"`${''}`;",
"`${'text'}`;",
'`${true}`;',
'`${false}`;',
'`${1}`;',
'`${1n}`;',
'`${[]}`;',
'`${/regex/}`;',
"`${__dirname === 'foobar'}`;",
'`${{}.constructor()}`;',
'`${() => {}}`;',
'`${function () {}}`;',

// operator + +=
Copy link
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)

...literalListWrapped.flatMap(l =>
literalListWrapped.map(r => `${l} + ${r};`),
),
"'' + '';",
"'' + 'text';",
"'' + true;",
"'' + false;",
"'' + 1;",
"'' + 1n;",
"'' + [];",
"'' + /regex/;",
"'' + (__dirname === 'foobar');",
"'' + {}.constructor();",
"'' + (() => {});",
"'' + function () {};",
"'text' + '';",
"'text' + 'text';",
"'text' + true;",
"'text' + false;",
"'text' + 1;",
"'text' + 1n;",
"'text' + [];",
"'text' + /regex/;",
"'text' + (__dirname === 'foobar');",
"'text' + {}.constructor();",
"'text' + (() => {});",
"'text' + function () {};",
"true + '';",
"true + 'text';",
'true + true;',
'true + false;',
'true + 1;',
'true + 1n;',
'true + [];',
'true + /regex/;',
"true + (__dirname === 'foobar');",
'true + {}.constructor();',
'true + (() => {});',
'true + function () {};',
"false + '';",
"false + 'text';",
'false + true;',
'false + false;',
Copy link
Author

Choose a reason for hiding this comment

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

'false + true;',
'false + false;',
'true + true;',
'true + false;',

In my opinion, it would be neater to leave only one of these.

'false + 1;',
'false + 1n;',
'false + [];',
'false + /regex/;',
"false + (__dirname === 'foobar');",
'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()
...literalListWrapped.map(i => `${i === '1' ? `(${i})` : i}.toString();`),
"''.toString();",
"'text'.toString();",
'true.toString();',
'false.toString();',
'(1).toString();',
'1n.toString();',
'[].toString();',
'/regex/.toString();',
"(__dirname === 'foobar').toString();",
'({}).constructor().toString();',
'(() => {}).toString();',
'(function () {}).toString();',

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

// String()
...literalList.map(i => `String(${i});`),
"String('');",
"String('text');",
'String(true);',
'String(false);',
'String(1);',
'String(1n);',
'String([]);',
'String(/regex/);',
"String(__dirname === 'foobar');",
'String({}.constructor());',
'String(() => {});',
'String(function () {});',
`
function someFunction() {}
someFunction.toString();
Expand Down