Skip to content

Commit 5b040e4

Browse files
committed
fix(valid-title): ignore string addition
1 parent a7b91f7 commit 5b040e4

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/rules/__tests__/valid-title.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const ruleTester = new TSESLint.RuleTester({
1212
ruleTester.run('title-must-be-string', rule, {
1313
valid: [
1414
'it("is a string", () => {});',
15+
'it("is" + " a " + " string", () => {});',
16+
'it(1 + " + " + 1, () => {});',
1517
'test("is a string", () => {});',
1618
'xtest("is a string", () => {});',
1719
'xtest(`${myFunc} is a string`, () => {});',
@@ -43,6 +45,16 @@ ruleTester.run('title-must-be-string', rule, {
4345
},
4446
],
4547
},
48+
{
49+
code: 'it(1 + 2 + 3, () => {});',
50+
errors: [
51+
{
52+
messageId: 'titleMustBeString',
53+
column: 4,
54+
line: 1,
55+
},
56+
],
57+
},
4658
{
4759
code: 'test.skip(123, () => {});',
4860
options: [{ ignoreTypeOfDescribeName: true }],

src/rules/valid-title.ts

+21
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ import {
1616
const trimFXprefix = (word: string) =>
1717
['f', 'x'].includes(word.charAt(0)) ? word.substr(1) : word;
1818

19+
const doesBinaryExpressionContainStringNode = (
20+
binaryExp: TSESTree.BinaryExpression,
21+
): boolean => {
22+
if (isStringNode(binaryExp.right)) {
23+
return true;
24+
}
25+
26+
if (binaryExp.left.type === AST_NODE_TYPES.BinaryExpression) {
27+
return doesBinaryExpressionContainStringNode(binaryExp.left);
28+
}
29+
30+
return isStringNode(binaryExp.left);
31+
};
32+
1933
export default createRule({
2034
name: __filename,
2135
meta: {
@@ -56,6 +70,13 @@ export default createRule({
5670
const [argument] = node.arguments;
5771

5872
if (!isStringNode(argument)) {
73+
if (
74+
argument.type === AST_NODE_TYPES.BinaryExpression &&
75+
doesBinaryExpressionContainStringNode(argument)
76+
) {
77+
return;
78+
}
79+
5980
if (
6081
argument.type !== AST_NODE_TYPES.TemplateLiteral &&
6182
!(ignoreTypeOfDescribeName && isDescribe(node))

0 commit comments

Comments
 (0)