From b7c1be2f279b87366332fb2d3a3e49a71aa75711 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Tue, 29 Oct 2019 00:40:47 +1300 Subject: [PATCH] fix(valid-title): ignore string addition (#461) --- 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 534e5b0eb..84bdc3ca3 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`, () => {});', @@ -53,6 +55,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 4c9354798..ad515fe49 100644 --- a/src/rules/valid-title.ts +++ b/src/rules/valid-title.ts @@ -17,6 +17,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: { @@ -57,6 +71,13 @@ export default createRule({ const [argument] = getJestFunctionArguments(node); if (!isStringNode(argument)) { + if ( + argument.type === AST_NODE_TYPES.BinaryExpression && + doesBinaryExpressionContainStringNode(argument) + ) { + return; + } + if ( argument.type !== AST_NODE_TYPES.TemplateLiteral && !(ignoreTypeOfDescribeName && isDescribe(node))