Skip to content

Commit

Permalink
chore(require-tothrow-message): convert to typescript (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored and SimenB committed Aug 15, 2019
1 parent c446449 commit c0b0626
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RuleTester } from 'eslint';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../require-tothrow-message';

const ruleTester = new RuleTester({
const ruleTester = new TSESLint.RuleTester({
parserOptions: {
ecmaVersion: 8,
},
Expand Down
41 changes: 0 additions & 41 deletions src/rules/require-tothrow-message.js

This file was deleted.

50 changes: 50 additions & 0 deletions src/rules/require-tothrow-message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
ModifierName,
createRule,
isExpectCall,
parseExpectCall,
} from './tsUtils';

export default createRule({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Require a message for `toThrow()`',
recommended: false,
},
messages: {
requireRethrow: 'Add an error message to {{ propertyName }}()',
},
type: 'suggestion',
schema: [],
},
defaultOptions: [],
create(context) {
return {
CallExpression(node) {
if (!isExpectCall(node)) {
return;
}

const { matcher, modifier } = parseExpectCall(node);

if (
matcher &&
matcher.arguments &&
matcher.arguments.length === 0 &&
['toThrow', 'toThrowError'].includes(matcher.name) &&
(!modifier ||
!(modifier.name === ModifierName.not || modifier.negation))
) {
// Look for `toThrow` calls with no arguments.
context.report({
messageId: 'requireRethrow', // todo: rename to 'addErrorMessage'
data: { propertyName: matcher.name }, // todo: rename to 'matcherName'
node: matcher.node.property,
});
}
},
};
},
});

0 comments on commit c0b0626

Please sign in to comment.