Skip to content

Commit fd6b22e

Browse files
G-RathSimenB
authored andcommitted
chore(prefer-inline-snapshots): convert to typescript (#319)
1 parent 3ed29ef commit fd6b22e

File tree

3 files changed

+62
-51
lines changed

3 files changed

+62
-51
lines changed

src/rules/__tests__/prefer-inline-snapshots.test.js renamed to src/rules/__tests__/prefer-inline-snapshots.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { RuleTester } from 'eslint';
1+
import { TSESLint } from '@typescript-eslint/experimental-utils';
22
import rule from '../prefer-inline-snapshots';
33

4-
const ruleTester = new RuleTester();
4+
const ruleTester = new TSESLint.RuleTester();
55

66
ruleTester.run('prefer-inline-snapshots', rule, {
77
valid: [

src/rules/prefer-inline-snapshots.js

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils';
2+
import { createRule } from './tsUtils';
3+
4+
export default createRule({
5+
name: __filename,
6+
meta: {
7+
docs: {
8+
category: 'Best Practices',
9+
description: 'Suggest using inline snapshots',
10+
recommended: false,
11+
},
12+
messages: {
13+
toMatch: 'Use toMatchInlineSnapshot() instead',
14+
toMatchError: 'Use toThrowErrorMatchingInlineSnapshot() instead',
15+
},
16+
fixable: 'code',
17+
schema: [],
18+
type: 'suggestion',
19+
},
20+
defaultOptions: [],
21+
create(context) {
22+
return {
23+
CallExpression(node) {
24+
const { callee } = node;
25+
26+
if (
27+
callee.type !== AST_NODE_TYPES.MemberExpression ||
28+
callee.property.type !== AST_NODE_TYPES.Identifier
29+
) {
30+
return;
31+
}
32+
33+
if (callee.property.name === 'toMatchSnapshot') {
34+
context.report({
35+
fix(fixer) {
36+
return [
37+
fixer.replaceText(callee.property, 'toMatchInlineSnapshot'),
38+
];
39+
},
40+
messageId: 'toMatch',
41+
node: callee.property,
42+
});
43+
} else if (callee.property.name === 'toThrowErrorMatchingSnapshot') {
44+
context.report({
45+
fix(fixer) {
46+
return [
47+
fixer.replaceText(
48+
callee.property,
49+
'toThrowErrorMatchingInlineSnapshot',
50+
),
51+
];
52+
},
53+
messageId: 'toMatchError',
54+
node: callee.property,
55+
});
56+
}
57+
},
58+
};
59+
},
60+
});

0 commit comments

Comments
 (0)