/
prefer-snapshot-hint.ts
134 lines (114 loc) · 3.44 KB
/
prefer-snapshot-hint.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import {
ParsedExpectFnCall,
createRule,
getAccessorValue,
isStringNode,
isSupportedAccessor,
isTypeOfJestFnCall,
parseJestFnCall,
} from './utils';
const snapshotMatchers = ['toMatchSnapshot', 'toThrowErrorMatchingSnapshot'];
const snapshotMatcherNames = snapshotMatchers;
const isSnapshotMatcherWithoutHint = (expectFnCall: ParsedExpectFnCall) => {
if (expectFnCall.args.length === 0) {
return true;
}
// this matcher only supports one argument which is the hint
if (!isSupportedAccessor(expectFnCall.matcher, 'toMatchSnapshot')) {
return expectFnCall.args.length !== 1;
}
// if we're being passed two arguments,
// the second one should be the hint
if (expectFnCall.args.length === 2) {
return false;
}
const [arg] = expectFnCall.args;
// the first argument to `toMatchSnapshot` can be _either_ a snapshot hint or
// an object with asymmetric matchers, so we can't just assume that the first
// argument is a hint when it's by itself.
return !isStringNode(arg);
};
const messages = {
missingHint: 'You should provide a hint for this snapshot',
};
export default createRule<[('always' | 'multi')?], keyof typeof messages>({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Prefer including a hint with external snapshots',
recommended: false,
},
messages,
type: 'suggestion',
schema: [
{
type: 'string',
enum: ['always', 'multi'],
},
],
},
defaultOptions: ['multi'],
create(context, [mode]) {
const snapshotMatchers: ParsedExpectFnCall[] = [];
const depths: number[] = [];
let expressionDepth = 0;
const reportSnapshotMatchersWithoutHints = () => {
for (const snapshotMatcher of snapshotMatchers) {
if (isSnapshotMatcherWithoutHint(snapshotMatcher)) {
context.report({
messageId: 'missingHint',
node: snapshotMatcher.matcher,
});
}
}
};
const enterExpression = () => {
expressionDepth++;
};
const exitExpression = () => {
expressionDepth--;
if (mode === 'always') {
reportSnapshotMatchersWithoutHints();
snapshotMatchers.length = 0;
}
if (mode === 'multi' && expressionDepth === 0) {
if (snapshotMatchers.length > 1) {
reportSnapshotMatchersWithoutHints();
}
snapshotMatchers.length = 0;
}
};
return {
'Program:exit'() {
enterExpression();
exitExpression();
},
FunctionExpression: enterExpression,
'FunctionExpression:exit': exitExpression,
ArrowFunctionExpression: enterExpression,
'ArrowFunctionExpression:exit': exitExpression,
'CallExpression:exit'(node) {
if (isTypeOfJestFnCall(node, context, ['describe', 'test'])) {
/* istanbul ignore next */
expressionDepth = depths.pop() ?? 0;
}
},
CallExpression(node) {
const jestFnCall = parseJestFnCall(node, context);
if (jestFnCall?.type !== 'expect') {
if (jestFnCall?.type === 'describe' || jestFnCall?.type === 'test') {
depths.push(expressionDepth);
expressionDepth = 0;
}
return;
}
const matcherName = getAccessorValue(jestFnCall.matcher);
if (!snapshotMatcherNames.includes(matcherName)) {
return;
}
snapshotMatchers.push(jestFnCall);
},
};
},
});