-
Notifications
You must be signed in to change notification settings - Fork 226
/
no-interpolation-in-snapshots.ts
53 lines (49 loc) · 1.31 KB
/
no-interpolation-in-snapshots.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
import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils';
import { createRule, isExpectCall, parseExpectCall } from './utils';
export default createRule({
name: __filename,
meta: {
docs: {
description: 'Disallow string interpolation inside snapshots',
recommended: 'error',
},
messages: {
noInterpolation: 'Do not use string interpolation inside of snapshots',
},
schema: [],
type: 'problem',
},
defaultOptions: [],
create(context) {
return {
CallExpression(node) {
if (!isExpectCall(node)) {
return;
}
const { matcher } = parseExpectCall(node);
if (!matcher) {
return;
}
if (
[
'toMatchInlineSnapshot',
'toThrowErrorMatchingInlineSnapshot',
].includes(matcher.name)
) {
// Check all since the optional 'propertyMatchers' argument might be present
matcher.arguments?.forEach(argument => {
if (
argument.type === AST_NODE_TYPES.TemplateLiteral &&
argument.expressions.length > 0
) {
context.report({
messageId: 'noInterpolation',
node: argument,
});
}
});
}
},
};
},
});