Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
prefer-optional-catch-binding
rule (#671)
- Loading branch information
Showing
8 changed files
with
254 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Prefer omitting the `catch` binding parameter | ||
|
||
If the `catch` binding parameter is not used, it should be omitted. | ||
|
||
This rule is fixable. | ||
|
||
## Fail | ||
|
||
```js | ||
try {} catch (usedError) {} | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
try {} catch {} | ||
``` | ||
|
||
```js | ||
try {} catch (error) { | ||
console.error(error); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict'; | ||
const getDocumentationUrl = require('./utils/get-documentation-url'); | ||
const {findVariable, isOpeningParenToken, isClosingParenToken} = require('eslint-utils'); | ||
|
||
const ERROR_MESSAGE_ID = 'error'; | ||
|
||
const selector = [ | ||
'CatchClause', | ||
'>', | ||
'Identifier.param' | ||
].join(''); | ||
|
||
const create = context => { | ||
return { | ||
[selector]: node => { | ||
const scope = context.getScope(); | ||
const variable = findVariable(scope, node); | ||
|
||
if (variable.references.length !== 0) { | ||
return; | ||
} | ||
|
||
const {name} = node; | ||
|
||
context.report({ | ||
node, | ||
messageId: ERROR_MESSAGE_ID, | ||
data: {name}, | ||
fix: fixer => { | ||
const tokenBefore = context.getTokenBefore(node); | ||
const tokenAfter = context.getTokenAfter(node); | ||
|
||
/* istanbul ignore next */ | ||
if (!isOpeningParenToken(tokenBefore) || !isClosingParenToken(tokenAfter)) { | ||
throw new Error('Unexpected token.'); | ||
} | ||
|
||
return [ | ||
tokenBefore, | ||
node, | ||
tokenAfter | ||
].map(nodeOrToken => fixer.remove(nodeOrToken)); | ||
} | ||
}); | ||
} | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: getDocumentationUrl(__filename) | ||
}, | ||
fixable: 'code', | ||
messages: { | ||
[ERROR_MESSAGE_ID]: 'Remove unused catch binding `{{name}}`.' | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import test from 'ava'; | ||
import avaRuleTester from 'eslint-ava-rule-tester'; | ||
import {outdent} from 'outdent'; | ||
import rule from '../rules/prefer-optional-catch-binding'; | ||
|
||
const ERROR_MESSAGE_ID = 'error'; | ||
const generateError = name => ({messageId: ERROR_MESSAGE_ID, data: {name}}); | ||
|
||
const ruleTester = avaRuleTester(test, { | ||
parserOptions: { | ||
ecmaVersion: 2020 | ||
} | ||
}); | ||
|
||
ruleTester.run('prefer-optional-catch-binding', rule, { | ||
valid: [ | ||
'try {} catch {}', | ||
outdent` | ||
try {} catch { | ||
error | ||
} | ||
`, | ||
outdent` | ||
try {} catch(used) { | ||
console.error(used); | ||
} | ||
`, | ||
outdent` | ||
try {} catch(usedInADeeperScope) { | ||
function foo() { | ||
function bar() { | ||
console.error(usedInADeeperScope); | ||
} | ||
} | ||
} | ||
`, | ||
// We are not checking destructuring | ||
'try {} catch({message}) {}', | ||
'try {} catch({nonExistsProperty = thisWillExecute()}) {}' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'try {} catch(_) {}', | ||
output: 'try {} catch {}', | ||
errors: [generateError('_')] | ||
}, | ||
{ | ||
code: outdent` | ||
try {} catch(foo) { | ||
function bar(foo) {} | ||
} | ||
`, | ||
output: outdent` | ||
try {} catch { | ||
function bar(foo) {} | ||
} | ||
`, | ||
errors: [generateError('foo')] | ||
}, | ||
// Many | ||
{ | ||
code: outdent` | ||
try {} catch(outer) { | ||
try {} catch(inner) { | ||
} | ||
} | ||
try { | ||
try {} catch(inTry) { | ||
} | ||
} catch(another) { | ||
try {} catch(inCatch) { | ||
} | ||
} finally { | ||
try {} catch(inFinally) { | ||
} | ||
} | ||
`, | ||
output: outdent` | ||
try {} catch { | ||
try {} catch { | ||
} | ||
} | ||
try { | ||
try {} catch { | ||
} | ||
} catch { | ||
try {} catch { | ||
} | ||
} finally { | ||
try {} catch { | ||
} | ||
} | ||
`, | ||
errors: [ | ||
generateError('outer'), | ||
generateError('inner'), | ||
generateError('inTry'), | ||
generateError('another'), | ||
generateError('inCatch'), | ||
generateError('inFinally') | ||
] | ||
}, | ||
// Actual message | ||
{ | ||
code: 'try {} catch(theRealErrorName) {}', | ||
output: 'try {} catch {}', | ||
errors: [{message: 'Remove unused catch binding `theRealErrorName`.'}] | ||
}, | ||
// TODO: this `error` should be able to remove | ||
// { | ||
// code: outdent` | ||
// try { | ||
// } catch(foo) { | ||
// var foo = 1; | ||
// } | ||
// `, | ||
// output: outdent` | ||
// try { | ||
// } catch { | ||
// var foo = 1; | ||
// } | ||
// `, | ||
// errors: [generateError('foo')] | ||
// }, | ||
// Comments | ||
{ | ||
code: outdent` | ||
/* comment */ | ||
try { | ||
/* comment */ | ||
// comment | ||
} catch ( | ||
/* comment */ | ||
// comment | ||
unused | ||
/* comment */ | ||
// comment | ||
) { | ||
/* comment */ | ||
// comment | ||
} | ||
/* comment */ | ||
`, | ||
output: outdent` | ||
/* comment */ | ||
try { | ||
/* comment */ | ||
// comment | ||
} catch{SPACE} | ||
/* comment */ | ||
// comment | ||
{TAB} | ||
/* comment */ | ||
// comment | ||
{ | ||
/* comment */ | ||
// comment | ||
} | ||
/* comment */ | ||
`.replace('{SPACE}', ' ').replace('{TAB}', '\t'), | ||
errors: [generateError('unused')] | ||
} | ||
] | ||
}); |