-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
arrow-parens.js
87 lines (81 loc) · 2.94 KB
/
arrow-parens.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* @author Toru Nagashima
* @copyright 2015 Toru Nagashima. All rights reserved.
* See LICENSE file in root directory for full license.
*/
"use strict"
/**
* Checks whether or not a given token is `(`.
* @param {Token} token - A token to check.
* @returns {boolean} `true` when the token is `(`.
*/
function isOpenParen(token) {
return token.type === "Punctuator" && token.value === "("
}
/**
* Checks whether or not given two tokens are at a same line.
* @param {Token} a - A left token.
* @param {Token} b - A right token.
* @returns {boolean} `true` when the tokens are at a same line.
*/
function isSameLine(a, b) {
return a.loc.end.line === b.loc.start.line
}
module.exports = {
meta: {
docs: {
description: "enforce the parentheses style of arrow functions.",
category: "Stylistic Issues",
recommended: false,
url: "https://github.com/mysticatea/eslint-plugin/blob/v13.0.0/docs/rules/arrow-parens.md",
},
fixable: "code",
schema: [],
type: "suggestion",
},
create(context) {
return {
ArrowFunctionExpression(node) {
const first = context
.getSourceCode()
.getFirstToken(node, node.async ? 1 : 0)
const before = context.getSourceCode().getTokenBefore(first)
if (isOpenParen(first)) {
if (
node.params.length === 1 &&
node.params[0].type === "Identifier" &&
isOpenParen(before) &&
isSameLine(before, first)
) {
context.report({
node,
message:
"Unexpected parentheses enclosing this argument.",
fix(fixer) {
const id = node.params[0]
const begin = first.range[0]
const end = context
.getSourceCode()
.getTokenAfter(id).range[1]
return fixer.replaceTextRange(
[begin, end],
id.name
)
},
})
}
} else if (!isOpenParen(before) || !isSameLine(before, first)) {
context.report({
node,
message:
"Expected to enclose this argument with parentheses.",
fix(fixer) {
const id = node.params[0]
return fixer.replaceText(id, `(${id.name})`)
},
})
}
},
}
},
}