From 672917bb79ec3a0a1161028294708cc276861057 Mon Sep 17 00:00:00 2001 From: Chris Blossom Date: Wed, 17 Jul 2019 12:29:00 -0700 Subject: [PATCH] fix(require-tothrow-message): rename rule to require-to-throw-message This change makes consistent with other rule naming format and removes spelling error message from editors --- README.md | 4 +- ...message.md => require-to-throw-message.md} | 2 +- ...st.js => require-to-throw-message.test.js} | 4 +- src/rules/require-to-throw-message.js | 36 +++++++++++++++++ src/rules/require-tothrow-message.js | 39 +++---------------- 5 files changed, 47 insertions(+), 38 deletions(-) rename docs/rules/{require-tothrow-message.md => require-to-throw-message.md} (91%) rename src/rules/__tests__/{require-tothrow-message.test.js => require-to-throw-message.test.js} (92%) create mode 100644 src/rules/require-to-throw-message.js diff --git a/README.md b/README.md index e4ac4b164..81c588d95 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ installations requiring long-term consistency. | [prefer-to-contain][] | Suggest using `toContain()` | | ![fixable-green][] | | [prefer-to-have-length][] | Suggest using `toHaveLength()` | | ![fixable-green][] | | [prefer-inline-snapshots][] | Suggest using `toMatchInlineSnapshot()` | | ![fixable-green][] | -| [require-tothrow-message][] | Require that `toThrow()` and `toThrowError` includes a message | | | +| [require-to-throw-message][] | Require that `toThrow()` and `toThrowError` includes a message | | | | [valid-describe][] | Enforce valid `describe()` callback | ![recommended][] | | | [valid-expect-in-promise][] | Enforce having return statement when testing with promises | ![recommended][] | | | [valid-expect][] | Enforce valid `expect()` usage | ![recommended][] | | @@ -184,7 +184,7 @@ https://github.com/dangreenisrael/eslint-plugin-jest-formatting [prefer-to-contain]: docs/rules/prefer-to-contain.md [prefer-to-have-length]: docs/rules/prefer-to-have-length.md [prefer-inline-snapshots]: docs/rules/prefer-inline-snapshots.md -[require-tothrow-message]: docs/rules/require-tothrow-message.md +[require-to-throw-message]: docs/rules/require-to-throw-message.md [valid-describe]: docs/rules/valid-describe.md [valid-expect-in-promise]: docs/rules/valid-expect-in-promise.md [valid-expect]: docs/rules/valid-expect.md diff --git a/docs/rules/require-tothrow-message.md b/docs/rules/require-to-throw-message.md similarity index 91% rename from docs/rules/require-tothrow-message.md rename to docs/rules/require-to-throw-message.md index 444d36437..8e1b3877f 100644 --- a/docs/rules/require-tothrow-message.md +++ b/docs/rules/require-to-throw-message.md @@ -1,4 +1,4 @@ -# Require a message for `toThrow()` (require-tothrow-message) +# Require a message for `toThrow()` (require-to-throw-message) `toThrow()`, and its alias `toThrowError()`, are used to check if an error is thrown by a function call, such as in `expect(() => a()).toThrow()`. However, if diff --git a/src/rules/__tests__/require-tothrow-message.test.js b/src/rules/__tests__/require-to-throw-message.test.js similarity index 92% rename from src/rules/__tests__/require-tothrow-message.test.js rename to src/rules/__tests__/require-to-throw-message.test.js index 25c6fe58c..67996c174 100644 --- a/src/rules/__tests__/require-tothrow-message.test.js +++ b/src/rules/__tests__/require-to-throw-message.test.js @@ -1,5 +1,5 @@ import { RuleTester } from 'eslint'; -import rule from '../require-tothrow-message'; +import rule from '../require-to-throw-message'; const ruleTester = new RuleTester({ parserOptions: { @@ -7,7 +7,7 @@ const ruleTester = new RuleTester({ }, }); -ruleTester.run('require-tothrow-message', rule, { +ruleTester.run('require-to-throw-message', rule, { valid: [ // String "expect(() => { throw new Error('a'); }).toThrow('a');", diff --git a/src/rules/require-to-throw-message.js b/src/rules/require-to-throw-message.js new file mode 100644 index 000000000..0ae027775 --- /dev/null +++ b/src/rules/require-to-throw-message.js @@ -0,0 +1,36 @@ +import { argument, expectCase, getDocsUrl, method } from './util'; + +export default { + meta: { + docs: { + url: getDocsUrl(__filename), + }, + messages: { + requireRethrow: 'Add an error message to {{ propertyName }}()', + }, + schema: [], + }, + create(context) { + return { + CallExpression(node) { + if (!expectCase(node)) { + return; + } + + const propertyName = method(node) && method(node).name; + + // Look for `toThrow` calls with no arguments. + if ( + ['toThrow', 'toThrowError'].includes(propertyName) && + !argument(node) + ) { + context.report({ + messageId: 'requireRethrow', + data: { propertyName }, + node: method(node), + }); + } + }, + }; + }, +}; diff --git a/src/rules/require-tothrow-message.js b/src/rules/require-tothrow-message.js index 0ae027775..a0f128dbf 100644 --- a/src/rules/require-tothrow-message.js +++ b/src/rules/require-tothrow-message.js @@ -1,36 +1,9 @@ -import { argument, expectCase, getDocsUrl, method } from './util'; +import requireToThrowMessage from './require-to-throw-message'; -export default { - meta: { - docs: { - url: getDocsUrl(__filename), - }, - messages: { - requireRethrow: 'Add an error message to {{ propertyName }}()', - }, - schema: [], - }, - create(context) { - return { - CallExpression(node) { - if (!expectCase(node)) { - return; - } +const merged = Object.assign({}, requireToThrowMessage); - const propertyName = method(node) && method(node).name; +merged.meta.deprecated = true; +merged.meta.replacedBy = ['require-to-throw-message']; +merged.meta.docs.replacedBy = ['require-to-throw-message']; - // Look for `toThrow` calls with no arguments. - if ( - ['toThrow', 'toThrowError'].includes(propertyName) && - !argument(node) - ) { - context.report({ - messageId: 'requireRethrow', - data: { propertyName }, - node: method(node), - }); - } - }, - }; - }, -}; +export default merged;