@@ -50,14 +50,31 @@ module.exports = {
50
50
51
51
const sourceCode = context . getSourceCode ( ) ;
52
52
53
-
54
53
/**
55
54
* Determines whether a arrow function argument end with `)`
56
55
* @param {ASTNode } node The arrow function node.
57
56
* @returns {void }
58
57
*/
59
58
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
+ }
61
78
62
79
// "as-needed", { "requireForBlockBody": true }: x => x
63
80
if (
@@ -68,19 +85,11 @@ module.exports = {
68
85
node . body . type !== "BlockStatement" &&
69
86
! node . returnType
70
87
) {
71
- if ( astUtils . isOpeningParenToken ( token ) ) {
88
+ if ( astUtils . isOpeningParenToken ( firstTokenOfParam ) ) {
72
89
context . report ( {
73
90
node,
74
91
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
84
93
} ) ;
85
94
}
86
95
return ;
@@ -90,12 +99,12 @@ module.exports = {
90
99
requireForBlockBody &&
91
100
node . body . type === "BlockStatement"
92
101
) {
93
- if ( ! astUtils . isOpeningParenToken ( token ) ) {
102
+ if ( ! astUtils . isOpeningParenToken ( firstTokenOfParam ) ) {
94
103
context . report ( {
95
104
node,
96
105
message : requireForBlockBodyNoParensMessage ,
97
106
fix ( fixer ) {
98
- return fixer . replaceText ( token , `(${ token . value } )` ) ;
107
+ return fixer . replaceText ( firstTokenOfParam , `(${ firstTokenOfParam . value } )` ) ;
99
108
}
100
109
} ) ;
101
110
}
@@ -109,34 +118,26 @@ module.exports = {
109
118
! node . params [ 0 ] . typeAnnotation &&
110
119
! node . returnType
111
120
) {
112
- if ( astUtils . isOpeningParenToken ( token ) ) {
121
+ if ( astUtils . isOpeningParenToken ( firstTokenOfParam ) ) {
113
122
context . report ( {
114
123
node,
115
124
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
125
126
} ) ;
126
127
}
127
128
return ;
128
129
}
129
130
130
- if ( token . type === "Identifier" ) {
131
- const after = sourceCode . getTokenAfter ( token ) ;
131
+ if ( firstTokenOfParam . type === "Identifier" ) {
132
+ const after = sourceCode . getTokenAfter ( firstTokenOfParam ) ;
132
133
133
134
// (x) => x
134
135
if ( after . value !== ")" ) {
135
136
context . report ( {
136
137
node,
137
138
message,
138
139
fix ( fixer ) {
139
- return fixer . replaceText ( token , `(${ token . value } )` ) ;
140
+ return fixer . replaceText ( firstTokenOfParam , `(${ firstTokenOfParam . value } )` ) ;
140
141
}
141
142
} ) ;
142
143
}
0 commit comments