Skip to content

Commit

Permalink
test(eslint-plugin): full tests for rule no-base-to-string
Browse files Browse the repository at this point in the history
  • Loading branch information
duduluu committed Apr 25, 2020
1 parent c83950c commit c8b6838
Showing 1 changed file with 59 additions and 25 deletions.
84 changes: 59 additions & 25 deletions packages/eslint-plugin/tests/rules/no-base-to-string.test.ts
Expand Up @@ -11,39 +11,58 @@ const ruleTester = new RuleTester({
},
});

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

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

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

const literalListWrapped = [
...literalListBasic,
...literalListNeedParen.map(i => `(${i})`),
];

ruleTester.run('no-base-to-string', rule, {
valid: [
"`${''}`;",
'`${true}`;',
'`${[]}`;',
'`${function() {}}`;',
"'' + '';",
"'' + true;",
"'' + [];",
'true + true;',
"true + '';",
'true + [];',
'[] + [];',
'[] + true;',
"[] + '';",
'({}.constructor());',
"'text'.toString();",
'false.toString();',
`
let value = 1;
value.toString();
`,
`
let value = 1n;
value.toString();
`,
// template
...literalList.map(i => `\`\${${i}}\`;`),

// operator + +=
...literalListWrapped
.map(l => literalListWrapped.map(r => `${l} + ${r};`))
.reduce((pre, cur) => [...pre, ...cur]),

// toString()
...literalListWrapped.map(i => `${i === '1' ? `(${i})` : i}.toString();`),

// variable toString() and template
...literalList.map(
i => `
let value = ${i};
value.toString();
let text = \`\${value}\`;
`,
),

`
function someFunction() {}
someFunction.toString();
let text = \`\${someFunction}\`;
`,
'unknownObject.toString();',
'unknownObject.someOtherMethod();',
'(() => {}).toString();',
`
class CustomToString {
toString() {
Expand Down Expand Up @@ -85,6 +104,21 @@ tag\`\${{}}\`;
},
],
},
{
code: `
\`\${/regex/}\`;
'' + /regex/;
/regex/.toString();
let value = /regex/;
value.toString();
let text = \`\${value}\`;
`,
options: [
{
ignoredTypeNames: ['RegExp'],
},
],
},
],
invalid: [
{
Expand Down

0 comments on commit c8b6838

Please sign in to comment.