From b6c50ec9a1ae5cdea27c0e6af9002e0c421d15b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20C=2E=20Viesca=20Ruiz?= Date: Mon, 4 May 2020 13:59:03 +0200 Subject: [PATCH] fix: add chamges requested in review --- .../no-interpolation-inline-snapshot.test.ts | 26 +++++++++++++++++++ src/rules/no-interpolation-inline-snapshot.ts | 15 ++++++----- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/rules/__tests__/no-interpolation-inline-snapshot.test.ts b/src/rules/__tests__/no-interpolation-inline-snapshot.test.ts index 7600302a6..81be79169 100644 --- a/src/rules/__tests__/no-interpolation-inline-snapshot.test.ts +++ b/src/rules/__tests__/no-interpolation-inline-snapshot.test.ts @@ -14,6 +14,11 @@ ruleTester.run('no-interpolation-inline-snapshot', rule, { 'expect(something).toMatchInlineSnapshot();', 'expect(something).toMatchInlineSnapshot(`No interpolation`);', 'expect(something).toMatchInlineSnapshot({}, `No interpolation`);', + 'expect(something);', + 'expect(something).not;', + 'expect.toHaveAssertions();', + 'myObjectWants.toMatchInlineSnapshot({}, `${interpolated}`);', + 'toMatchInlineSnapshot({}, `${interpolated}`);', ], invalid: [ { @@ -26,6 +31,16 @@ ruleTester.run('no-interpolation-inline-snapshot', rule, { }, ], }, + { + code: 'expect(something).not.toMatchInlineSnapshot(`${interpolated}`);', + errors: [ + { + endColumn: 62, + column: 45, + messageId: 'noInterpolation', + }, + ], + }, { code: 'expect(something).toMatchInlineSnapshot({}, `${interpolated}`);', errors: [ @@ -36,5 +51,16 @@ ruleTester.run('no-interpolation-inline-snapshot', rule, { }, ], }, + { + code: + 'expect(something).not.toMatchInlineSnapshot({}, `${interpolated}`);', + errors: [ + { + endColumn: 66, + column: 49, + messageId: 'noInterpolation', + }, + ], + }, ], }); diff --git a/src/rules/no-interpolation-inline-snapshot.ts b/src/rules/no-interpolation-inline-snapshot.ts index 85db261c3..d5890712a 100644 --- a/src/rules/no-interpolation-inline-snapshot.ts +++ b/src/rules/no-interpolation-inline-snapshot.ts @@ -1,5 +1,5 @@ import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils'; -import { createRule } from './utils'; +import { createRule, isExpectCall, parseExpectCall } from './utils'; export default createRule({ name: __filename, @@ -20,18 +20,21 @@ export default createRule({ create(context) { return { CallExpression(node) { - const { callee, arguments: nodeArguments } = node; + if (!isExpectCall(node)) { + return; + } + + const { matcher } = parseExpectCall(node); if ( - callee.type !== AST_NODE_TYPES.MemberExpression || - callee.property.type !== AST_NODE_TYPES.Identifier || - callee.property.name !== 'toMatchInlineSnapshot' + matcher?.name !== 'toMatchInlineSnapshot' || + matcher?.arguments === null ) { return; } // Check all since the optional 'propertyMatchers' argument might be present - nodeArguments.forEach(argument => { + matcher.arguments.forEach(argument => { if ( argument.type === AST_NODE_TYPES.TemplateLiteral && argument.expressions.length > 0