1 | 'use strict';
|
2 |
|
3 | const { getDocsUrl } = require('./util');
|
4 |
|
5 | const reportOnViolation = (context, node) => {
|
6 | const lineLimit =
|
7 | context.options[0] && Number.isFinite(context.options[0].maxSize)
|
8 | ? context.options[0].maxSize
|
9 | : 50;
|
10 | const startLine = node.loc.start.line;
|
11 | const endLine = node.loc.end.line;
|
12 | const lineCount = endLine - startLine;
|
13 |
|
14 | if (lineCount > lineLimit) {
|
15 | context.report({
|
16 | message:
|
17 | lineLimit === 0
|
18 | ? 'Expected to not encounter a Jest snapshot but was found with {{ lineCount }} lines long'
|
19 | : 'Expected Jest snapshot to be smaller than {{ lineLimit }} lines but was {{ lineCount }} lines long',
|
20 | data: { lineLimit, lineCount },
|
21 | node,
|
22 | });
|
23 | }
|
24 | };
|
25 |
|
26 | module.exports = {
|
27 | meta: {
|
28 | docs: {
|
29 | url: getDocsUrl(__filename),
|
30 | },
|
31 | },
|
32 | create(context) {
|
33 | if (context.getFilename().endsWith('.snap')) {
|
34 | return {
|
35 | ExpressionStatement(node) {
|
36 | reportOnViolation(context, node);
|
37 | },
|
38 | };
|
39 | } else if (context.getFilename().endsWith('.js')) {
|
40 | return {
|
41 | CallExpression(node) {
|
42 | const propertyName =
|
43 | node.callee.property && node.callee.property.name;
|
44 | if (
|
45 | propertyName === 'toMatchInlineSnapshot' ||
|
46 | propertyName === 'toThrowErrorMatchingInlineSnapshot'
|
47 | ) {
|
48 | reportOnViolation(context, node);
|
49 | }
|
50 | },
|
51 | };
|
52 | }
|
53 |
|
54 | return {};
|
55 | },
|
56 | };
|