From cd285da378e300052de4b9700eb99b4f7f50a804 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sun, 9 Aug 2020 00:22:07 +1200 Subject: [PATCH] fix(no-large-snapshots): run on all files regardless of type (#637) BREAKING CHANGE: `no-large-snapshots` runs on all files regardless of type Fixes #370 --- .../__tests__/no-large-snapshots.test.ts | 19 +++++-- src/rules/no-large-snapshots.ts | 50 ++++++++++--------- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/rules/__tests__/no-large-snapshots.test.ts b/src/rules/__tests__/no-large-snapshots.test.ts index 8a38bb524..cafc4f955 100644 --- a/src/rules/__tests__/no-large-snapshots.test.ts +++ b/src/rules/__tests__/no-large-snapshots.test.ts @@ -24,6 +24,9 @@ const generateExpectInlineSnapsCode = ( ruleTester.run('no-large-snapshots', rule, { valid: [ + 'expect(something)', + 'expect(something).toBe(1)', + 'expect(something).toMatchInlineSnapshot', { filename: 'mock.js', code: generateExpectInlineSnapsCode(2, 'toMatchInlineSnapshot'), @@ -35,11 +38,6 @@ ruleTester.run('no-large-snapshots', rule, { 'toThrowErrorMatchingInlineSnapshot', ), }, - { - // "it should return an empty object for non snapshot files" - filename: 'mock.jsx', - code: generateExpectInlineSnapsCode(50, 'toMatchInlineSnapshot'), - }, { filename: 'mock.jsx', code: generateExpectInlineSnapsCode(20, 'toMatchInlineSnapshot'), @@ -125,6 +123,17 @@ ruleTester.run('no-large-snapshots', rule, { }, ], }, + { + // "it should return an empty object for non snapshot files" + filename: 'mock.jsx', + code: generateExpectInlineSnapsCode(50, 'toMatchInlineSnapshot'), + errors: [ + { + messageId: 'tooLongSnapshots', + data: { lineLimit: 50, lineCount: 51 }, + }, + ], + }, { // "should report if node has more than 50 lines of code, and no sizeThreshold option is passed" filename: '/mock-component.jsx.snap', diff --git a/src/rules/no-large-snapshots.ts b/src/rules/no-large-snapshots.ts index 712bdc8b7..3c26548f5 100644 --- a/src/rules/no-large-snapshots.ts +++ b/src/rules/no-large-snapshots.ts @@ -7,8 +7,9 @@ import { import { createRule, getAccessorValue, + isExpectCall, isExpectMember, - isSupportedAccessor, + parseExpectCall, } from './utils'; interface RuleOptions { @@ -111,29 +112,32 @@ export default createRule<[RuleOptions], MessageId>({ reportOnViolation(context, node, options); }, }; - } else if (context.getFilename().endsWith('.js')) { - return { - CallExpression(node) { - if ( - 'property' in node.callee && - (isSupportedAccessor( - node.callee.property, - 'toMatchInlineSnapshot', - ) || - isSupportedAccessor( - node.callee.property, - 'toThrowErrorMatchingInlineSnapshot', - )) - ) { - reportOnViolation(context, node, { - ...options, - maxSize: options.inlineMaxSize ?? options.maxSize, - }); - } - }, - }; } - return {}; + return { + CallExpression(node) { + if (!isExpectCall(node)) { + return; + } + + const { matcher } = parseExpectCall(node); + + if (matcher?.node.parent?.type !== AST_NODE_TYPES.CallExpression) { + return; + } + + if ( + [ + 'toMatchInlineSnapshot', + 'toThrowErrorMatchingInlineSnapshot', + ].includes(matcher.name) + ) { + reportOnViolation(context, matcher.node.parent, { + ...options, + maxSize: options.inlineMaxSize ?? options.maxSize, + }); + } + }, + }; }, });