Skip to content

Commit

Permalink
fix(valid-title): ignore string addition (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Oct 28, 2019
1 parent 10e5087 commit b7c1be2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/rules/__tests__/valid-title.test.ts
Expand Up @@ -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`, () => {});',
Expand Down Expand Up @@ -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 }],
Expand Down
21 changes: 21 additions & 0 deletions src/rules/valid-title.ts
Expand Up @@ -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: {
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit b7c1be2

Please sign in to comment.