Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit73596cb

Browse files
mdjermanovicmysticatea
authored andcommitted
Update: Add enforceForSwitchCase option to use-isnan (#12106)
1 parentd592a24 commit73596cb

File tree

3 files changed

+293
-24
lines changed

3 files changed

+293
-24
lines changed

‎docs/rules/use-isnan.md‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,66 @@ if (!isNaN(foo)) {
4040
// ...
4141
}
4242
```
43+
44+
##Options
45+
46+
This rule has an object option, with one option:
47+
48+
*`"enforceForSwitchCase"` when set to`true` disallows`case NaN` and`switch(NaN)` in`switch` statements. Default is`false`, meaning
49+
that this rule by default does not warn about`case NaN` or`switch(NaN)`.
50+
51+
###enforceForSwitchCase
52+
53+
The`switch` statement internally uses the`===` comparison to match the expression's value to a case clause.
54+
Therefore, it can never match`case NaN`. Also,`switch(NaN)` can never match a case clause.
55+
56+
Set`"enforceForSwitchCase"` to`true` if you want this rule to report`case NaN` and`switch(NaN)` in`switch` statements.
57+
58+
Examples of**incorrect** code for this rule with`"enforceForSwitchCase"` option set to`true`:
59+
60+
```js
61+
/*eslint use-isnan: ["error", {"enforceForSwitchCase": true}]*/
62+
63+
switch (foo) {
64+
caseNaN:
65+
bar();
66+
break;
67+
case1:
68+
baz();
69+
break;
70+
// ...
71+
}
72+
73+
switch (NaN) {
74+
case a:
75+
bar();
76+
break;
77+
case b:
78+
baz();
79+
break;
80+
// ...
81+
}
82+
```
83+
84+
Examples of**correct** code for this rule with`"enforceForSwitchCase"` option set to`true`:
85+
86+
```js
87+
/*eslint use-isnan: ["error", {"enforceForSwitchCase": true}]*/
88+
89+
if (Number.isNaN(foo)) {
90+
bar();
91+
}else {
92+
switch (foo) {
93+
case1:
94+
baz();
95+
break;
96+
// ...
97+
}
98+
}
99+
100+
if (Number.isNaN(a)) {
101+
bar();
102+
}elseif (Number.isNaN(b)) {
103+
baz();
104+
}// ...
105+
```

‎lib/rules/use-isnan.js‎

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55

66
"use strict";
77

8+
//------------------------------------------------------------------------------
9+
// Helpers
10+
//------------------------------------------------------------------------------
11+
12+
/**
13+
* Determines if the given node is a NaN `Identifier` node.
14+
*@param {ASTNode|null} node The node to check.
15+
*@returns {boolean} `true` if the node is 'NaN' identifier.
16+
*/
17+
functionisNaNIdentifier(node){
18+
returnBoolean(node)&&node.type==="Identifier"&&node.name==="NaN";
19+
}
20+
821
//------------------------------------------------------------------------------
922
// Rule Definition
1023
//------------------------------------------------------------------------------
@@ -20,21 +33,69 @@ module.exports = {
2033
url:"https://eslint.org/docs/rules/use-isnan"
2134
},
2235

23-
schema:[],
36+
schema:[
37+
{
38+
type:"object",
39+
properties:{
40+
enforceForSwitchCase:{
41+
type:"boolean",
42+
default:false
43+
}
44+
},
45+
additionalProperties:false
46+
}
47+
],
48+
2449
messages:{
25-
useIsNaN:"Use the isNaN function to compare with NaN."
50+
comparisonWithNaN:"Use the isNaN function to compare with NaN.",
51+
switchNaN:"'switch(NaN)' can never match a case clause. Use Number.isNaN instead of the switch.",
52+
caseNaN:"'case NaN' can never match. Use Number.isNaN before the switch."
2653
}
2754
},
2855

2956
create(context){
3057

31-
return{
32-
BinaryExpression(node){
33-
if(/^(?:[<>]|[!=]=)=?$/u.test(node.operator)&&(node.left.name==="NaN"||node.right.name==="NaN")){
34-
context.report({ node,messageId:"useIsNaN"});
58+
constenforceForSwitchCase=context.options[0]&&context.options[0].enforceForSwitchCase;
59+
60+
/**
61+
* Checks the given `BinaryExpression` node.
62+
*@param {ASTNode} node The node to check.
63+
*@returns {void}
64+
*/
65+
functioncheckBinaryExpression(node){
66+
if(
67+
/^(?:[<>]|[!=]=)=?$/u.test(node.operator)&&
68+
(isNaNIdentifier(node.left)||isNaNIdentifier(node.right))
69+
){
70+
context.report({ node,messageId:"comparisonWithNaN"});
71+
}
72+
}
73+
74+
/**
75+
* Checks the discriminant and all case clauses of the given `SwitchStatement` node.
76+
*@param {ASTNode} node The node to check.
77+
*@returns {void}
78+
*/
79+
functioncheckSwitchStatement(node){
80+
if(isNaNIdentifier(node.discriminant)){
81+
context.report({ node,messageId:"switchNaN"});
82+
}
83+
84+
for(constswitchCaseofnode.cases){
85+
if(isNaNIdentifier(switchCase.test)){
86+
context.report({node:switchCase,messageId:"caseNaN"});
3587
}
3688
}
89+
}
90+
91+
constlisteners={
92+
BinaryExpression:checkBinaryExpression
3793
};
3894

95+
if(enforceForSwitchCase){
96+
listeners.SwitchStatement=checkSwitchStatement;
97+
}
98+
99+
returnlisteners;
39100
}
40101
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp