Skip to content

Commit

Permalink
Fix: Add space between async and param on fix (fixes #8682) (#8693)
Browse files Browse the repository at this point in the history
  • Loading branch information
soda0289 authored and kaicataldo committed Jun 8, 2017
1 parent c702858 commit 3cfe9ee
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 27 deletions.
55 changes: 28 additions & 27 deletions lib/rules/arrow-parens.js
Expand Up @@ -50,14 +50,31 @@ module.exports = {

const sourceCode = context.getSourceCode();


/**
* Determines whether a arrow function argument end with `)`
* @param {ASTNode} node The arrow function node.
* @returns {void}
*/
function parens(node) {
const token = sourceCode.getFirstToken(node, node.async ? 1 : 0);
const isAsync = node.async;
const firstTokenOfParam = sourceCode.getFirstToken(node, isAsync ? 1 : 0);

/**
* Remove the parenthesis around a parameter
* @param {Fixer} fixer Fixer
* @returns {string} fixed parameter
*/
function fixParamsWithParenthesis(fixer) {
const paramToken = sourceCode.getTokenAfter(firstTokenOfParam);
const closingParenToken = sourceCode.getTokenAfter(paramToken);
const asyncToken = isAsync ? sourceCode.getTokenBefore(firstTokenOfParam) : null;
const shouldAddSpaceForAsync = asyncToken && (asyncToken.end === firstTokenOfParam.start);

return fixer.replaceTextRange([
firstTokenOfParam.range[0],
closingParenToken.range[1]
], `${shouldAddSpaceForAsync ? " " : ""}${paramToken.value}`);
}

// "as-needed", { "requireForBlockBody": true }: x => x
if (
Expand All @@ -68,19 +85,11 @@ module.exports = {
node.body.type !== "BlockStatement" &&
!node.returnType
) {
if (astUtils.isOpeningParenToken(token)) {
if (astUtils.isOpeningParenToken(firstTokenOfParam)) {
context.report({
node,
message: requireForBlockBodyMessage,
fix(fixer) {
const paramToken = context.getTokenAfter(token);
const closingParenToken = context.getTokenAfter(paramToken);

return fixer.replaceTextRange([
token.range[0],
closingParenToken.range[1]
], paramToken.value);
}
fix: fixParamsWithParenthesis
});
}
return;
Expand All @@ -90,12 +99,12 @@ module.exports = {
requireForBlockBody &&
node.body.type === "BlockStatement"
) {
if (!astUtils.isOpeningParenToken(token)) {
if (!astUtils.isOpeningParenToken(firstTokenOfParam)) {
context.report({
node,
message: requireForBlockBodyNoParensMessage,
fix(fixer) {
return fixer.replaceText(token, `(${token.value})`);
return fixer.replaceText(firstTokenOfParam, `(${firstTokenOfParam.value})`);
}
});
}
Expand All @@ -109,34 +118,26 @@ module.exports = {
!node.params[0].typeAnnotation &&
!node.returnType
) {
if (astUtils.isOpeningParenToken(token)) {
if (astUtils.isOpeningParenToken(firstTokenOfParam)) {
context.report({
node,
message: asNeededMessage,
fix(fixer) {
const paramToken = context.getTokenAfter(token);
const closingParenToken = context.getTokenAfter(paramToken);

return fixer.replaceTextRange([
token.range[0],
closingParenToken.range[1]
], paramToken.value);
}
fix: fixParamsWithParenthesis
});
}
return;
}

if (token.type === "Identifier") {
const after = sourceCode.getTokenAfter(token);
if (firstTokenOfParam.type === "Identifier") {
const after = sourceCode.getTokenAfter(firstTokenOfParam);

// (x) => x
if (after.value !== ")") {
context.report({
node,
message,
fix(fixer) {
return fixer.replaceText(token, `(${token.value})`);
return fixer.replaceText(firstTokenOfParam, `(${firstTokenOfParam.value})`);
}
});
}
Expand Down
24 changes: 24 additions & 0 deletions tests/lib/rules/arrow-parens.js
Expand Up @@ -176,6 +176,18 @@ const invalid = [
type
}]
},
{
code: "async(a) => a",
output: "async a => a",
options: ["as-needed"],
parserOptions: { ecmaVersion: 8 },
errors: [{
line: 1,
column: 1,
message: asNeededMessage,
type
}]
},

// "as-needed", { "requireForBlockBody": true }
{
Expand Down Expand Up @@ -223,6 +235,18 @@ const invalid = [
message: requireForBlockBodyMessage,
type
}]
},
{
code: "async(a) => a",
output: "async a => a",
options: ["as-needed", { requireForBlockBody: true }],
parserOptions: { ecmaVersion: 8 },
errors: [{
line: 1,
column: 1,
message: requireForBlockBodyMessage,
type
}]
}
];

Expand Down

0 comments on commit 3cfe9ee

Please sign in to comment.