Skip to content
This repository has been archived by the owner on Aug 4, 2020. It is now read-only.

Commit

Permalink
Merge pull request #62 from lemonmade/fix-for-arrow-parens
Browse files Browse the repository at this point in the history
Add an automated fix for arrow-parens
  • Loading branch information
jquense committed Jun 1, 2016
2 parents 5c20dd6 + 603a619 commit 934ec49
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
21 changes: 19 additions & 2 deletions rules/arrow-parens.js
Expand Up @@ -28,7 +28,18 @@ module.exports = function(context) {
&& node.params[0].type === "Identifier"
&& node.params[0].typeAnnotation === undefined) {
if (token.type === "Punctuator" && token.value === "(") {
context.report(node, asNeededMessage);
context.report({
node: node,
message: asNeededMessage,
fix: function(fixer) {
var paramToken = context.getTokenAfter(token);
var closingParenToken = context.getTokenAfter(paramToken);
return fixer.replaceTextRange([
token.range[0],
closingParenToken.range[1]
], paramToken.value);
}
});
}
return;
}
Expand All @@ -38,7 +49,13 @@ module.exports = function(context) {

// (x) => x
if (after.value !== ")") {
context.report(node, message);
context.report({
node: node,
message: message,
fix: function(fixer) {
return fixer.replaceText(token, '(' + token.value + ')');
}
});
}
}
}
Expand Down
17 changes: 13 additions & 4 deletions tests/arrow-parens.js
Expand Up @@ -18,9 +18,10 @@ function ok(code, args){
return { code: code, options: args, parser: 'babel-eslint' }
}

function err(code, errors, args){
function err(code, output, errors, args){
var e = ok(code, args)
e.errors = errors
e.output = output
return e
}

Expand Down Expand Up @@ -74,6 +75,7 @@ var type = type;
var invalid = [
{
code: "a => {}",
output: "(a) => {}",
ecmaFeatures: { arrowFunctions: true },
errors: [{
line: 1,
Expand All @@ -84,6 +86,7 @@ var invalid = [
},
{
code: "a => a",
output: "(a) => a",
ecmaFeatures: { arrowFunctions: true },
errors: [{
line: 1,
Expand All @@ -94,6 +97,7 @@ var invalid = [
},
{
code: "a => {\n}",
output: "(a) => {\n}",
ecmaFeatures: { arrowFunctions: true },
errors: [{
line: 1,
Expand All @@ -104,6 +108,7 @@ var invalid = [
},
{
code: "a.then(foo => {});",
output: "a.then((foo) => {});",
ecmaFeatures: { arrowFunctions: true },
errors: [{
line: 1,
Expand All @@ -114,6 +119,7 @@ var invalid = [
},
{
code: "a.then(foo => a);",
output: "a.then((foo) => a);",
ecmaFeatures: { arrowFunctions: true },
errors: [{
line: 1,
Expand All @@ -124,6 +130,7 @@ var invalid = [
},
{
code: "a(foo => { if (true) {}; });",
output: "a((foo) => { if (true) {}; });",
ecmaFeatures: { arrowFunctions: true },
errors: [{
line: 1,
Expand All @@ -136,6 +143,7 @@ var invalid = [
// as-needed
{
code: "(a) => a",
output: "a => a",
options: ["as-needed"],
ecmaFeatures: { arrowFunctions: true },
errors: [{
Expand All @@ -147,6 +155,7 @@ var invalid = [
},
{
code: "(b) => b",
output: "b => b",
options: ["as-needed"],
ecmaFeatures: { arrowFunctions: true },
errors: [{
Expand All @@ -158,15 +167,15 @@ var invalid = [
},

// async
err('async a => {}', [
err('async a => {}', 'async (a) => {}', [
{ message: 'Expected parentheses around arrow function argument.' },
]),

err('async a => a', [
err('async a => a', 'async (a) => a', [
{ message: 'Expected parentheses around arrow function argument.' },
]),

err('async (a) => a', [
err('async (a) => a', 'async a => a', [
{ message: 'Unexpected parentheses around single function argument' },
],
["as-needed"])
Expand Down

0 comments on commit 934ec49

Please sign in to comment.