Skip to content

Commit

Permalink
Rule fix: Add fixer for no-error (#272)
Browse files Browse the repository at this point in the history
Also fix copy/paste error in valid cases.

Part of #115
  • Loading branch information
edg2s authored Oct 8, 2020
1 parent df8f984 commit 3f3286f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
15 changes: 11 additions & 4 deletions docs/rules/no-error.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@ Disallows the [`$.error`](https://api.jquery.com/jQuery.error/) utility. Prefer

⚙️ This rule is enabled in `plugin:no-jquery/all`.

🔧 The `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by this rule.

## Rule details

❌ Examples of **incorrect** code:
```js
$.error();
$.error( msg );
```

✔️ Examples of **correct** code:
```js
nodeName();
myClass.nodeName();
$div.nodeName();
error( msg );
myClass.error( msg );
$div.error( msg );
```

🔧 Examples of code **fixed** by this rule:
```js
$.error( msg ); /**/ throw new Error( msg );
```

## Resources
Expand Down
8 changes: 7 additions & 1 deletion src/rules/no-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ const utils = require( '../utils.js' );

module.exports = utils.createUtilMethodRule(
'error',
'Prefer `throw` to `$.error`'
'Prefer `throw` to `$.error`',
{
fixable: 'code',
fix: function ( node, fixer ) {
return fixer.replaceText( node.callee, 'throw new Error' );
}
}
);
7 changes: 4 additions & 3 deletions tests/rules/no-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ const error = 'Prefer throw to $.error';

const ruleTester = new RuleTester();
ruleTester.run( 'no-error', rule, {
valid: [ 'nodeName()', 'myClass.nodeName()', '$div.nodeName()' ],
valid: [ 'error(msg)', 'myClass.error(msg)', '$div.error(msg)' ],
invalid: [
{
code: '$.error()',
errors: [ error ]
code: '$.error(msg)',
errors: [ error ],
output: 'throw new Error(msg)'
}
]
} );

0 comments on commit 3f3286f

Please sign in to comment.