Merge pull request babel/eslint-plugin-babel#62 from lemonmade/fix-for-arrow-parens
Add an automated fix for arrow-parens
This commit is contained in:
parent
8487ec8a67
commit
f57dc5227a
@ -28,7 +28,18 @@ module.exports = function(context) {
|
|||||||
&& node.params[0].type === "Identifier"
|
&& node.params[0].type === "Identifier"
|
||||||
&& node.params[0].typeAnnotation === undefined) {
|
&& node.params[0].typeAnnotation === undefined) {
|
||||||
if (token.type === "Punctuator" && token.value === "(") {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
@ -38,7 +49,13 @@ module.exports = function(context) {
|
|||||||
|
|
||||||
// (x) => x
|
// (x) => x
|
||||||
if (after.value !== ")") {
|
if (after.value !== ")") {
|
||||||
context.report(node, message);
|
context.report({
|
||||||
|
node: node,
|
||||||
|
message: message,
|
||||||
|
fix: function(fixer) {
|
||||||
|
return fixer.replaceText(token, '(' + token.value + ')');
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,9 +18,10 @@ function ok(code, args){
|
|||||||
return { code: code, options: args, parser: 'babel-eslint' }
|
return { code: code, options: args, parser: 'babel-eslint' }
|
||||||
}
|
}
|
||||||
|
|
||||||
function err(code, errors, args){
|
function err(code, output, errors, args){
|
||||||
var e = ok(code, args)
|
var e = ok(code, args)
|
||||||
e.errors = errors
|
e.errors = errors
|
||||||
|
e.output = output
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,6 +75,7 @@ var type = type;
|
|||||||
var invalid = [
|
var invalid = [
|
||||||
{
|
{
|
||||||
code: "a => {}",
|
code: "a => {}",
|
||||||
|
output: "(a) => {}",
|
||||||
ecmaFeatures: { arrowFunctions: true },
|
ecmaFeatures: { arrowFunctions: true },
|
||||||
errors: [{
|
errors: [{
|
||||||
line: 1,
|
line: 1,
|
||||||
@ -84,6 +86,7 @@ var invalid = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: "a => a",
|
code: "a => a",
|
||||||
|
output: "(a) => a",
|
||||||
ecmaFeatures: { arrowFunctions: true },
|
ecmaFeatures: { arrowFunctions: true },
|
||||||
errors: [{
|
errors: [{
|
||||||
line: 1,
|
line: 1,
|
||||||
@ -94,6 +97,7 @@ var invalid = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: "a => {\n}",
|
code: "a => {\n}",
|
||||||
|
output: "(a) => {\n}",
|
||||||
ecmaFeatures: { arrowFunctions: true },
|
ecmaFeatures: { arrowFunctions: true },
|
||||||
errors: [{
|
errors: [{
|
||||||
line: 1,
|
line: 1,
|
||||||
@ -104,6 +108,7 @@ var invalid = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: "a.then(foo => {});",
|
code: "a.then(foo => {});",
|
||||||
|
output: "a.then((foo) => {});",
|
||||||
ecmaFeatures: { arrowFunctions: true },
|
ecmaFeatures: { arrowFunctions: true },
|
||||||
errors: [{
|
errors: [{
|
||||||
line: 1,
|
line: 1,
|
||||||
@ -114,6 +119,7 @@ var invalid = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: "a.then(foo => a);",
|
code: "a.then(foo => a);",
|
||||||
|
output: "a.then((foo) => a);",
|
||||||
ecmaFeatures: { arrowFunctions: true },
|
ecmaFeatures: { arrowFunctions: true },
|
||||||
errors: [{
|
errors: [{
|
||||||
line: 1,
|
line: 1,
|
||||||
@ -124,6 +130,7 @@ var invalid = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: "a(foo => { if (true) {}; });",
|
code: "a(foo => { if (true) {}; });",
|
||||||
|
output: "a((foo) => { if (true) {}; });",
|
||||||
ecmaFeatures: { arrowFunctions: true },
|
ecmaFeatures: { arrowFunctions: true },
|
||||||
errors: [{
|
errors: [{
|
||||||
line: 1,
|
line: 1,
|
||||||
@ -136,6 +143,7 @@ var invalid = [
|
|||||||
// as-needed
|
// as-needed
|
||||||
{
|
{
|
||||||
code: "(a) => a",
|
code: "(a) => a",
|
||||||
|
output: "a => a",
|
||||||
options: ["as-needed"],
|
options: ["as-needed"],
|
||||||
ecmaFeatures: { arrowFunctions: true },
|
ecmaFeatures: { arrowFunctions: true },
|
||||||
errors: [{
|
errors: [{
|
||||||
@ -147,6 +155,7 @@ var invalid = [
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: "(b) => b",
|
code: "(b) => b",
|
||||||
|
output: "b => b",
|
||||||
options: ["as-needed"],
|
options: ["as-needed"],
|
||||||
ecmaFeatures: { arrowFunctions: true },
|
ecmaFeatures: { arrowFunctions: true },
|
||||||
errors: [{
|
errors: [{
|
||||||
@ -158,15 +167,15 @@ var invalid = [
|
|||||||
},
|
},
|
||||||
|
|
||||||
// async
|
// async
|
||||||
err('async a => {}', [
|
err('async a => {}', 'async (a) => {}', [
|
||||||
{ message: 'Expected parentheses around arrow function argument.' },
|
{ 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.' },
|
{ 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' },
|
{ message: 'Unexpected parentheses around single function argument' },
|
||||||
],
|
],
|
||||||
["as-needed"])
|
["as-needed"])
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user