Skip to content

Commit

Permalink
prefer-optional-catch-binding: Improve output (#884)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Oct 21, 2020
1 parent 52edd3b commit 375d11a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
20 changes: 14 additions & 6 deletions rules/prefer-optional-catch-binding.js
Expand Up @@ -14,6 +14,8 @@ const selector = [
].join('');

const create = context => {
const sourceCode = context.getSourceCode();

return {
[selector]: node => {
const scope = context.getScope();
Expand All @@ -29,7 +31,7 @@ const create = context => {
node,
messageId: ERROR_MESSAGE_ID,
data: {name},
fix: fixer => {
* fix(fixer) {
const tokenBefore = context.getTokenBefore(node);
const tokenAfter = context.getTokenAfter(node);

Expand All @@ -38,11 +40,17 @@ const create = context => {
throw new Error('Unexpected token.');
}

return [
tokenBefore,
node,
tokenAfter
].map(nodeOrToken => fixer.remove(nodeOrToken));
yield fixer.remove(tokenBefore);
yield fixer.remove(node);
yield fixer.remove(tokenAfter);

const [, endOfClosingParenthesis] = tokenAfter.range;
const [startOfCatchClauseBody] = node.parent.body.range;
const text = sourceCode.text.slice(endOfClosingParenthesis, startOfCatchClauseBody);
const leadingSpacesLength = text.length - text.trimStart().length;
if (leadingSpacesLength !== 0) {
yield fixer.removeRange([endOfClosingParenthesis, endOfClosingParenthesis + leadingSpacesLength]);
}
}
});
}
Expand Down
39 changes: 28 additions & 11 deletions test/prefer-optional-catch-binding.js
Expand Up @@ -32,13 +32,13 @@ test({
],
invalid: [
{
code: 'try {} catch(_) {}',
code: 'try {} catch (_) {}',
output: 'try {} catch {}',
errors: [generateError('_')]
},
{
code: outdent`
try {} catch(foo) {
try {} catch (foo) {
function bar(foo) {}
}
`,
Expand All @@ -52,18 +52,18 @@ test({
// Many
{
code: outdent`
try {} catch(outer) {
try {} catch(inner) {
try {} catch (outer) {
try {} catch (inner) {
}
}
try {
try {} catch(inTry) {
try {} catch (inTry) {
}
} catch(another) {
try {} catch(inCatch) {
} catch (another) {
try {} catch (inCatch) {
}
} finally {
try {} catch(inFinally) {
try {} catch (inFinally) {
}
}
`,
Expand Down Expand Up @@ -94,15 +94,15 @@ test({
},
// Actual message
{
code: 'try {} catch(theRealErrorName) {}',
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) {
// } catch (foo) {
// var foo = 1;
// }
// `,
Expand Down Expand Up @@ -144,13 +144,30 @@ test({
{TAB}
/* comment */
// comment
{
{
/* comment */
// comment
}
/* comment */
`.replace('{SPACE}', ' ').replace('{TAB}', '\t'),
errors: [generateError('unused')]
},
// Spaces
{
code: 'try { } catch (e) \n \t { }',
// ^^^^^^^^^^ spaces after param will be removed.
output: 'try { } catch { }',
errors: 1
},
{
code: 'try {} catch(e) {}',
output: 'try {} catch{}',
errors: 1
},
{
code: 'try {} catch (e){}',
output: 'try {} catch {}',
errors: 1
}
]
});

0 comments on commit 375d11a

Please sign in to comment.