Skip to content

Commit 3cfe9ee

Browse files
soda0289kaicataldo
authored andcommittedJun 8, 2017
Fix: Add space between async and param on fix (fixes #8682) (#8693)
1 parent c702858 commit 3cfe9ee

File tree

2 files changed

+52
-27
lines changed

2 files changed

+52
-27
lines changed
 

‎lib/rules/arrow-parens.js

+28-27
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,31 @@ module.exports = {
5050

5151
const sourceCode = context.getSourceCode();
5252

53-
5453
/**
5554
* Determines whether a arrow function argument end with `)`
5655
* @param {ASTNode} node The arrow function node.
5756
* @returns {void}
5857
*/
5958
function parens(node) {
60-
const token = sourceCode.getFirstToken(node, node.async ? 1 : 0);
59+
const isAsync = node.async;
60+
const firstTokenOfParam = sourceCode.getFirstToken(node, isAsync ? 1 : 0);
61+
62+
/**
63+
* Remove the parenthesis around a parameter
64+
* @param {Fixer} fixer Fixer
65+
* @returns {string} fixed parameter
66+
*/
67+
function fixParamsWithParenthesis(fixer) {
68+
const paramToken = sourceCode.getTokenAfter(firstTokenOfParam);
69+
const closingParenToken = sourceCode.getTokenAfter(paramToken);
70+
const asyncToken = isAsync ? sourceCode.getTokenBefore(firstTokenOfParam) : null;
71+
const shouldAddSpaceForAsync = asyncToken && (asyncToken.end === firstTokenOfParam.start);
72+
73+
return fixer.replaceTextRange([
74+
firstTokenOfParam.range[0],
75+
closingParenToken.range[1]
76+
], `${shouldAddSpaceForAsync ? " " : ""}${paramToken.value}`);
77+
}
6178

6279
// "as-needed", { "requireForBlockBody": true }: x => x
6380
if (
@@ -68,19 +85,11 @@ module.exports = {
6885
node.body.type !== "BlockStatement" &&
6986
!node.returnType
7087
) {
71-
if (astUtils.isOpeningParenToken(token)) {
88+
if (astUtils.isOpeningParenToken(firstTokenOfParam)) {
7289
context.report({
7390
node,
7491
message: requireForBlockBodyMessage,
75-
fix(fixer) {
76-
const paramToken = context.getTokenAfter(token);
77-
const closingParenToken = context.getTokenAfter(paramToken);
78-
79-
return fixer.replaceTextRange([
80-
token.range[0],
81-
closingParenToken.range[1]
82-
], paramToken.value);
83-
}
92+
fix: fixParamsWithParenthesis
8493
});
8594
}
8695
return;
@@ -90,12 +99,12 @@ module.exports = {
9099
requireForBlockBody &&
91100
node.body.type === "BlockStatement"
92101
) {
93-
if (!astUtils.isOpeningParenToken(token)) {
102+
if (!astUtils.isOpeningParenToken(firstTokenOfParam)) {
94103
context.report({
95104
node,
96105
message: requireForBlockBodyNoParensMessage,
97106
fix(fixer) {
98-
return fixer.replaceText(token, `(${token.value})`);
107+
return fixer.replaceText(firstTokenOfParam, `(${firstTokenOfParam.value})`);
99108
}
100109
});
101110
}
@@ -109,34 +118,26 @@ module.exports = {
109118
!node.params[0].typeAnnotation &&
110119
!node.returnType
111120
) {
112-
if (astUtils.isOpeningParenToken(token)) {
121+
if (astUtils.isOpeningParenToken(firstTokenOfParam)) {
113122
context.report({
114123
node,
115124
message: asNeededMessage,
116-
fix(fixer) {
117-
const paramToken = context.getTokenAfter(token);
118-
const closingParenToken = context.getTokenAfter(paramToken);
119-
120-
return fixer.replaceTextRange([
121-
token.range[0],
122-
closingParenToken.range[1]
123-
], paramToken.value);
124-
}
125+
fix: fixParamsWithParenthesis
125126
});
126127
}
127128
return;
128129
}
129130

130-
if (token.type === "Identifier") {
131-
const after = sourceCode.getTokenAfter(token);
131+
if (firstTokenOfParam.type === "Identifier") {
132+
const after = sourceCode.getTokenAfter(firstTokenOfParam);
132133

133134
// (x) => x
134135
if (after.value !== ")") {
135136
context.report({
136137
node,
137138
message,
138139
fix(fixer) {
139-
return fixer.replaceText(token, `(${token.value})`);
140+
return fixer.replaceText(firstTokenOfParam, `(${firstTokenOfParam.value})`);
140141
}
141142
});
142143
}

‎tests/lib/rules/arrow-parens.js

+24
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ const invalid = [
176176
type
177177
}]
178178
},
179+
{
180+
code: "async(a) => a",
181+
output: "async a => a",
182+
options: ["as-needed"],
183+
parserOptions: { ecmaVersion: 8 },
184+
errors: [{
185+
line: 1,
186+
column: 1,
187+
message: asNeededMessage,
188+
type
189+
}]
190+
},
179191

180192
// "as-needed", { "requireForBlockBody": true }
181193
{
@@ -223,6 +235,18 @@ const invalid = [
223235
message: requireForBlockBodyMessage,
224236
type
225237
}]
238+
},
239+
{
240+
code: "async(a) => a",
241+
output: "async a => a",
242+
options: ["as-needed", { requireForBlockBody: true }],
243+
parserOptions: { ecmaVersion: 8 },
244+
errors: [{
245+
line: 1,
246+
column: 1,
247+
message: requireForBlockBodyMessage,
248+
type
249+
}]
226250
}
227251
];
228252

0 commit comments

Comments
 (0)
Please sign in to comment.