From 5b040e49dbe4c04d0ffd03e22d4ea053027cface Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Tue, 29 Oct 2019 00:27:15 +1300 Subject: [PATCH] fix(valid-title): ignore string addition --- src/rules/__tests__/valid-title.test.ts | 12 ++++++++++++ src/rules/valid-title.ts | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/rules/__tests__/valid-title.test.ts b/src/rules/__tests__/valid-title.test.ts index abbf81ee7..57a67077f 100644 --- a/src/rules/__tests__/valid-title.test.ts +++ b/src/rules/__tests__/valid-title.test.ts @@ -12,6 +12,8 @@ const ruleTester = new TSESLint.RuleTester({ ruleTester.run('title-must-be-string', rule, { valid: [ 'it("is a string", () => {});', + 'it("is" + " a " + " string", () => {});', + 'it(1 + " + " + 1, () => {});', 'test("is a string", () => {});', 'xtest("is a string", () => {});', 'xtest(`${myFunc} is a string`, () => {});', @@ -43,6 +45,16 @@ ruleTester.run('title-must-be-string', rule, { }, ], }, + { + code: 'it(1 + 2 + 3, () => {});', + errors: [ + { + messageId: 'titleMustBeString', + column: 4, + line: 1, + }, + ], + }, { code: 'test.skip(123, () => {});', options: [{ ignoreTypeOfDescribeName: true }], diff --git a/src/rules/valid-title.ts b/src/rules/valid-title.ts index c0cd8ab3b..6ed2f7b56 100644 --- a/src/rules/valid-title.ts +++ b/src/rules/valid-title.ts @@ -16,6 +16,20 @@ import { const trimFXprefix = (word: string) => ['f', 'x'].includes(word.charAt(0)) ? word.substr(1) : word; +const doesBinaryExpressionContainStringNode = ( + binaryExp: TSESTree.BinaryExpression, +): boolean => { + if (isStringNode(binaryExp.right)) { + return true; + } + + if (binaryExp.left.type === AST_NODE_TYPES.BinaryExpression) { + return doesBinaryExpressionContainStringNode(binaryExp.left); + } + + return isStringNode(binaryExp.left); +}; + export default createRule({ name: __filename, meta: { @@ -56,6 +70,13 @@ export default createRule({ const [argument] = node.arguments; if (!isStringNode(argument)) { + if ( + argument.type === AST_NODE_TYPES.BinaryExpression && + doesBinaryExpressionContainStringNode(argument) + ) { + return; + } + if ( argument.type !== AST_NODE_TYPES.TemplateLiteral && !(ignoreTypeOfDescribeName && isDescribe(node))