Skip to content

Commit

Permalink
chore(prefer-inline-snapshots): migrate to TS (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored and SimenB committed Jul 22, 2019
1 parent 41ed53a commit c455100
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 51 deletions.
@@ -1,7 +1,7 @@
import { RuleTester } from 'eslint';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../prefer-inline-snapshots';

const ruleTester = new RuleTester();
const ruleTester = new TSESLint.RuleTester();

ruleTester.run('prefer-inline-snapshots', rule, {
valid: [
Expand Down
49 changes: 0 additions & 49 deletions src/rules/prefer-inline-snapshots.js

This file was deleted.

60 changes: 60 additions & 0 deletions src/rules/prefer-inline-snapshots.ts
@@ -0,0 +1,60 @@
import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils';
import { createRule } from './tsUtils';

export default createRule({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Suggest using inline snapshots',
recommended: false,
},
messages: {
toMatch: 'Use toMatchInlineSnapshot() instead',
toMatchError: 'Use toThrowErrorMatchingInlineSnapshot() instead',
},
fixable: 'code',
schema: [],
type: 'suggestion',
},
defaultOptions: [],
create(context) {
return {
CallExpression(node) {
const { callee } = node;

if (
callee.type !== AST_NODE_TYPES.MemberExpression ||
callee.property.type !== AST_NODE_TYPES.Identifier
) {
return;
}

if (callee.property.name === 'toMatchSnapshot') {
context.report({
fix(fixer) {
return [
fixer.replaceText(callee.property, 'toMatchInlineSnapshot'),
];
},
messageId: 'toMatch',
node: callee.property,
});
} else if (callee.property.name === 'toThrowErrorMatchingSnapshot') {
context.report({
fix(fixer) {
return [
fixer.replaceText(
callee.property,
'toThrowErrorMatchingInlineSnapshot',
),
];
},
messageId: 'toMatchError',
node: callee.property,
});
}
},
};
},
});

0 comments on commit c455100

Please sign in to comment.