Skip to content

Commit

Permalink
Fix: prevent space-before-function-paren from checking type annotatio…
Browse files Browse the repository at this point in the history
…ns (#8349)

* Fix: prevent space-before-function-paren from checking type annotations

Previously, the space-before-function-paren rule's listener would be triggered for custom nodes such as `FunctionTypeAnnotation`, and would throw an error. This broke the regression build. This commit updates the rule to only listen for known function nodes.

* Move parser fixture to babel-eslint7/
  • Loading branch information
not-an-aardvark committed Mar 31, 2017
1 parent 3342e9f commit 35c93e6
Show file tree
Hide file tree
Showing 3 changed files with 566 additions and 27 deletions.
59 changes: 34 additions & 25 deletions lib/rules/space-before-function-paren.js
Expand Up @@ -102,34 +102,43 @@ module.exports = {
return "ignore";
}

return {
":function"(node) {
const functionConfig = getConfigForFunction(node);
/**
* Checks the parens of a function node
* @param {ASTNode} node A function node
* @returns {void}
*/
function checkFunction(node) {
const functionConfig = getConfigForFunction(node);

if (functionConfig === "ignore") {
return;
}
if (functionConfig === "ignore") {
return;
}

const rightToken = sourceCode.getFirstToken(node, astUtils.isOpeningParenToken);
const leftToken = sourceCode.getTokenBefore(rightToken);
const hasSpacing = sourceCode.isSpaceBetweenTokens(leftToken, rightToken);

if (hasSpacing && functionConfig === "never") {
context.report({
node,
loc: leftToken.loc.end,
message: "Unexpected space before function parentheses.",
fix: fixer => fixer.removeRange([leftToken.range[1], rightToken.range[0]])
});
} else if (!hasSpacing && functionConfig === "always") {
context.report({
node,
loc: leftToken.loc.end,
message: "Missing space before function parentheses.",
fix: fixer => fixer.insertTextAfter(leftToken, " ")
});
}
const rightToken = sourceCode.getFirstToken(node, astUtils.isOpeningParenToken);
const leftToken = sourceCode.getTokenBefore(rightToken);
const hasSpacing = sourceCode.isSpaceBetweenTokens(leftToken, rightToken);

if (hasSpacing && functionConfig === "never") {
context.report({
node,
loc: leftToken.loc.end,
message: "Unexpected space before function parentheses.",
fix: fixer => fixer.removeRange([leftToken.range[1], rightToken.range[0]])
});
} else if (!hasSpacing && functionConfig === "always") {
context.report({
node,
loc: leftToken.loc.end,
message: "Missing space before function parentheses.",
fix: fixer => fixer.insertTextAfter(leftToken, " ")
});
}
}

return {
ArrowFunctionExpression: checkFunction,
FunctionDeclaration: checkFunction,
FunctionExpression: checkFunction
};
}
};

0 comments on commit 35c93e6

Please sign in to comment.