/
prefer-snapshot-hint.ts
121 lines (102 loc) · 2.97 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
import {
ParsedExpectMatcher,
createRule,
isExpectCall,
isStringNode,
parseExpectCall,
} from './utils';
const snapshotMatchers = ['toMatchSnapshot', 'toThrowErrorMatchingSnapshot'];
const isSnapshotMatcher = (matcher: ParsedExpectMatcher) => {
return snapshotMatchers.includes(matcher.name);
};
const isSnapshotMatcherWithoutHint = (matcher: ParsedExpectMatcher) => {
if (!matcher.arguments || matcher.arguments.length === 0) {
return true;
}
// this matcher only supports one argument which is the hint
if (matcher.name !== 'toMatchSnapshot') {
return matcher.arguments.length !== 1;
}
// if we're being passed two arguments,
// the second one should be the hint
if (matcher.arguments.length === 2) {
return false;
}
const [arg] = matcher.arguments;
// 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: ParsedExpectMatcher[] = [];
let expressionDepth = 0;
const reportSnapshotMatchersWithoutHints = () => {
for (const snapshotMatcher of snapshotMatchers) {
if (isSnapshotMatcherWithoutHint(snapshotMatcher)) {
context.report({
messageId: 'missingHint',
node: snapshotMatcher.node.property,
});
}
}
};
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(node) {
if (!isExpectCall(node)) {
return;
}
const { matcher } = parseExpectCall(node);
if (!matcher || !isSnapshotMatcher(matcher)) {
return;
}
snapshotMatchers.push(matcher);
},
};
},
});