UNPKG

1.5 kBJavaScriptView Raw
1/**
2 * @fileoverview Rule to require parens in arrow function arguments.
3 * @author Jxck
4 * @copyright 2015 Jxck. All rights reserved.
5 */
6"use strict";
7
8//------------------------------------------------------------------------------
9// Rule Definition
10//------------------------------------------------------------------------------
11
12module.exports = function(context) {
13 var message = "Expected parentheses around arrow function argument.";
14 var asNeededMessage = "Unexpected parentheses around single function argument";
15 var asNeeded = context.options[0] === "as-needed";
16
17 /**
18 * Determines whether a arrow function argument end with `)`
19 * @param {ASTNode} node The arrow function node.
20 * @returns {void}
21 */
22 function parens(node) {
23 var token = context.getFirstToken(node);
24
25 // as-needed: x => x
26 if (asNeeded && node.params.length === 1 && node.params[0].type === "Identifier") {
27 if (token.type === "Punctuator" && token.value === "(") {
28 context.report(node, asNeededMessage);
29 }
30 return;
31 }
32
33 if (token.type === "Identifier") {
34 var after = context.getTokenAfter(token);
35
36 // (x) => x
37 if (after.value !== ")") {
38 context.report(node, message);
39 }
40 }
41 }
42
43 return {
44 "ArrowFunctionExpression": parens
45 };
46};
47
48module.exports.schema = [
49 {
50 "enum": ["always", "as-needed"]
51 }
52];