5
5
6
6
"use strict" ;
7
7
8
+ const astUtils = require ( "../ast-utils" ) ;
9
+
8
10
//------------------------------------------------------------------------------
9
11
// Helpers
10
12
//------------------------------------------------------------------------------
@@ -105,15 +107,35 @@ function getNonNumericOperand(node) {
105
107
return null ;
106
108
}
107
109
110
+ /**
111
+ * Checks whether a node is a string literal or not.
112
+ * @param {ASTNode } node The node to check.
113
+ * @returns {boolean } Whether or not the passed in node is a
114
+ * string literal or not.
115
+ */
116
+ function isStringLiteral ( node ) {
117
+ return astUtils . isStringLiteral ( node ) && node . type !== "TemplateLiteral" ;
118
+ }
119
+
120
+ /**
121
+ * Checks whether a node is an empty string literal or not.
122
+ * @param {ASTNode } node The node to check.
123
+ * @returns {boolean } Whether or not the passed in node is an
124
+ * empty string literal or not.
125
+ */
126
+ function isEmptyString ( node ) {
127
+ return isStringLiteral ( node ) && node . value === "" ;
128
+ }
129
+
108
130
/**
109
131
* Checks whether or not a node is a concatenating with an empty string.
110
132
* @param {ASTNode } node - A BinaryExpression node to check.
111
133
* @returns {boolean } Whether or not the node is a concatenating with an empty string.
112
134
*/
113
135
function isConcatWithEmptyString ( node ) {
114
136
return node . operator === "+" && (
115
- ( node . left . type === "Literal" && node . left . value === "" ) ||
116
- ( node . right . type === "Literal" && node . right . value === "" )
137
+ ( isEmptyString ( node . left ) && ! isStringLiteral ( node . right ) ) ||
138
+ ( isEmptyString ( node . right ) && ! isStringLiteral ( node . left ) )
117
139
) ;
118
140
}
119
141
@@ -123,20 +145,16 @@ function isConcatWithEmptyString(node) {
123
145
* @returns {boolean } Whether or not the node is appended with an empty string.
124
146
*/
125
147
function isAppendEmptyString ( node ) {
126
- return node . operator === "+=" && node . right . type === "Literal" && node . right . value === "" ;
148
+ return node . operator === "+=" && isEmptyString ( node . right ) ;
127
149
}
128
150
129
151
/**
130
- * Gets a node that is the left or right operand of a node, is not the specified literal.
131
- * @param {ASTNode } node - A BinaryExpression node to get.
132
- * @param {any } value - A literal value to check.
133
- * @returns {ASTNode } A node that is the left or right operand of the node, is not the specified literal.
152
+ * Returns the operand that is not an empty string from a flagged BinaryExpression.
153
+ * @param {ASTNode } node - The flagged BinaryExpression node to check.
154
+ * @returns {ASTNode } The operand that is not an empty string from a flagged BinaryExpression.
134
155
*/
135
- function getOtherOperand ( node , value ) {
136
- if ( node . left . type === "Literal" && node . left . value === value ) {
137
- return node . right ;
138
- }
139
- return node . left ;
156
+ function getNonEmptyOperand ( node ) {
157
+ return isEmptyString ( node . left ) ? node . right : node . left ;
140
158
}
141
159
142
160
//------------------------------------------------------------------------------
@@ -236,7 +254,7 @@ module.exports = {
236
254
context . report (
237
255
node ,
238
256
"use `String({{code}})` instead." , {
239
- code : sourceCode . getText ( getOtherOperand ( node , "" ) )
257
+ code : sourceCode . getText ( getNonEmptyOperand ( node ) )
240
258
} ) ;
241
259
}
242
260
} ,
@@ -250,7 +268,7 @@ module.exports = {
250
268
context . report (
251
269
node ,
252
270
"use `{{code}} = String({{code}})` instead." , {
253
- code : sourceCode . getText ( getOtherOperand ( node , "" ) )
271
+ code : sourceCode . getText ( getNonEmptyOperand ( node ) )
254
272
} ) ;
255
273
}
256
274
}
0 commit comments