Skip to content

Commit 75ec753

Browse files
authored
fix: handle static template literals in eqeqeq rule (#21058)
1 parent b717a22 commit 75ec753

2 files changed

Lines changed: 60 additions & 5 deletions

File tree

lib/rules/eqeqeq.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,34 @@ module.exports = {
102102
return isTypeOf(node.left) || isTypeOf(node.right);
103103
}
104104

105+
/**
106+
* Gets the type of a literal node.
107+
* @param {ASTNode} node The node to check
108+
* @returns {string|null} The type of the literal
109+
* @private
110+
*/
111+
function getLiteralType(node) {
112+
if (node.type === "Literal") {
113+
return typeof node.value;
114+
}
115+
116+
if (astUtils.isStaticTemplateLiteral(node)) {
117+
return "string";
118+
}
119+
120+
return null;
121+
}
122+
105123
/**
106124
* Checks if operands are literals of the same type (via typeof)
107125
* @param {ASTNode} node The node to check
108126
* @returns {boolean} if operands are of same type
109127
* @private
110128
*/
111129
function areLiteralsAndSameType(node) {
112-
return (
113-
node.left.type === "Literal" &&
114-
node.right.type === "Literal" &&
115-
typeof node.left.value === typeof node.right.value
116-
);
130+
const leftType = getLiteralType(node.left);
131+
132+
return leftType !== null && leftType === getLiteralType(node.right);
117133
}
118134

119135
/**

tests/lib/rules/eqeqeq.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ ruleTester.run("eqeqeq", rule, {
3131
{ code: "typeof a == 'number'", options: ["smart"] },
3232
{ code: "'string' != typeof a", options: ["smart"] },
3333
{ code: "'hello' != 'world'", options: ["smart"] },
34+
{ code: "`hello` != `world`", options: ["smart"] },
35+
{ code: "`hello` == 'hello'", options: ["smart"] },
3436
{ code: "2 == 3", options: ["smart"] },
3537
{ code: "true == true", options: ["smart"] },
3638
{ code: "null == a", options: ["smart"] },
@@ -183,6 +185,26 @@ ruleTester.run("eqeqeq", rule, {
183185
},
184186
],
185187
},
188+
{
189+
code: "`hello` == `world`",
190+
output: "`hello` === `world`",
191+
errors: [
192+
{
193+
messageId: "unexpected",
194+
data: wantedEqEqEq,
195+
},
196+
],
197+
},
198+
{
199+
code: "`hello` != 'world'",
200+
output: "`hello` !== 'world'",
201+
errors: [
202+
{
203+
messageId: "unexpected",
204+
data: wantedNotEqEq,
205+
},
206+
],
207+
},
186208
{
187209
code: "a == null",
188210
errors: [
@@ -283,6 +305,23 @@ ruleTester.run("eqeqeq", rule, {
283305
},
284306
],
285307
},
308+
{
309+
code: "`hello${world}` == `hello`",
310+
options: ["smart"],
311+
errors: [
312+
{
313+
messageId: "unexpected",
314+
data: wantedEqEqEq,
315+
suggestions: [
316+
{
317+
messageId: "replaceOperator",
318+
data: wantedEqEqEq,
319+
output: "`hello${world}` === `hello`",
320+
},
321+
],
322+
},
323+
],
324+
},
286325
{
287326
code: "typeof a == 'number'",
288327
output: "typeof a === 'number'",

0 commit comments

Comments
 (0)