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