From d82d39fa2a5c8e77b2830152ddd0fa52fa54cc25 Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Mon, 30 Sep 2019 02:37:44 +0100 Subject: [PATCH] Introduce `no-error` rule See #112 --- docs/no-contains.md | 4 ++-- docs/no-error.md | 17 +++++++++++++++++ rules/no-error.js | 8 ++++++++ tests/no-contains.js | 6 +----- tests/no-error.js | 17 +++++++++++++++++ 5 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 docs/no-error.md create mode 100644 rules/no-error.js create mode 100644 tests/no-error.js diff --git a/docs/no-contains.md b/docs/no-contains.md index f6c39ff2..3fc59b46 100644 --- a/docs/no-contains.md +++ b/docs/no-contains.md @@ -12,6 +12,6 @@ $.contains(); ✓ The following patterns are not considered errors: ```js contains(); -div.contains(); -div.contains; +myClass.contains(); +$div.contains(); ``` diff --git a/docs/no-error.md b/docs/no-error.md new file mode 100644 index 00000000..dd66669b --- /dev/null +++ b/docs/no-error.md @@ -0,0 +1,17 @@ +# no-error + +Disallows the $.error utility. Prefer throw to $.error. + +## Rule details + +✗ The following patterns are considered errors: +```js +$.error(); +``` + +✓ The following patterns are not considered errors: +```js +nodeName(); +myClass.nodeName(); +$div.nodeName(); +``` diff --git a/rules/no-error.js b/rules/no-error.js new file mode 100644 index 00000000..a19d4505 --- /dev/null +++ b/rules/no-error.js @@ -0,0 +1,8 @@ +'use strict'; + +const utils = require( './utils.js' ); + +module.exports = utils.createUtilMethodRule( + 'error', + 'Prefer throw to $.error' +); diff --git a/tests/no-contains.js b/tests/no-contains.js index a3ade051..8952f139 100644 --- a/tests/no-contains.js +++ b/tests/no-contains.js @@ -7,11 +7,7 @@ const error = 'Prefer Node#contains to $.contains'; const ruleTester = new RuleTesterAndDocs(); ruleTester.run( 'no-contains', rule, { - valid: [ - 'contains()', - 'div.contains()', - 'div.contains' - ], + valid: [ 'contains()', 'myClass.contains()', '$div.contains()' ], invalid: [ { code: '$.contains()', diff --git a/tests/no-error.js b/tests/no-error.js new file mode 100644 index 00000000..93f2341d --- /dev/null +++ b/tests/no-error.js @@ -0,0 +1,17 @@ +'use strict'; + +const rule = require( '../rules/no-error' ); +const RuleTesterAndDocs = require( '../rule-tester-and-docs' ); + +const error = 'Prefer throw to $.error'; + +const ruleTester = new RuleTesterAndDocs(); +ruleTester.run( 'no-error', rule, { + valid: [ 'nodeName()', 'myClass.nodeName()', '$div.nodeName()' ], + invalid: [ + { + code: '$.error()', + errors: [ { message: error, type: 'CallExpression' } ] + } + ] +} );