@@ -7,6 +7,32 @@ const messages = {
7
7
[ MESSAGE_ID ] : 'Prefer `Math.{{method}}()` to simplify ternary expressions.' ,
8
8
} ;
9
9
10
+ const isNumberTypeAnnotation = typeAnnotation => {
11
+ if ( typeAnnotation . type === 'TSNumberKeyword' ) {
12
+ return true ;
13
+ }
14
+
15
+ if ( typeAnnotation . type === 'TSTypeAnnotation' && typeAnnotation . typeAnnotation . type === 'TSNumberKeyword' ) {
16
+ return true ;
17
+ }
18
+
19
+ if ( typeAnnotation . type === 'TSTypeReference' && typeAnnotation . typeName . name === 'Number' ) {
20
+ return true ;
21
+ }
22
+
23
+ return false ;
24
+ } ;
25
+
26
+ const getExpressionText = ( node , sourceCode ) => {
27
+ const expressionNode = node . type === 'TSAsExpression' ? node . expression : node ;
28
+
29
+ if ( node . type === 'TSAsExpression' ) {
30
+ return getExpressionText ( expressionNode , sourceCode ) ;
31
+ }
32
+
33
+ return sourceCode . getText ( expressionNode ) ;
34
+ } ;
35
+
10
36
/** @param {import('eslint').Rule.RuleContext } context */
11
37
const create = context => ( {
12
38
/** @param {import('estree').ConditionalExpression } conditionalExpression */
@@ -33,7 +59,7 @@ const create = context => ({
33
59
return ;
34
60
}
35
61
36
- const [ leftText , rightText , alternateText , consequentText ] = [ left , right , alternate , consequent ] . map ( node => context . sourceCode . getText ( node ) ) ;
62
+ const [ leftText , rightText , alternateText , consequentText ] = [ left , right , alternate , consequent ] . map ( node => getExpressionText ( node , context . sourceCode ) ) ;
37
63
38
64
const isGreaterOrEqual = operator === '>' || operator === '>=' ;
39
65
const isLessOrEqual = operator === '<' || operator === '<=' ;
@@ -61,6 +87,87 @@ const create = context => ({
61
87
return ;
62
88
}
63
89
90
+ for ( const node of [ left , right ] ) {
91
+ let expressionNode = node ;
92
+
93
+ if ( expressionNode . typeAnnotation && expressionNode . type === 'TSAsExpression' ) {
94
+ // Ignore if the test is not a number comparison operator
95
+ if ( ! isNumberTypeAnnotation ( expressionNode . typeAnnotation ) ) {
96
+ return ;
97
+ }
98
+
99
+ expressionNode = expressionNode . expression ;
100
+ }
101
+
102
+ // Find variable declaration
103
+ if ( expressionNode . type === 'Identifier' ) {
104
+ const variable = context . sourceCode . getScope ( expressionNode ) . variables . find ( variable => variable . name === expressionNode . name ) ;
105
+
106
+ for ( const definition of variable ?. defs ?? [ ] ) {
107
+ switch ( definition . type ) {
108
+ case 'Parameter' : {
109
+ const identifier = definition . name ;
110
+
111
+ /**
112
+ Capture the following statement
113
+
114
+ ```js
115
+ function foo(a: number) {}
116
+ ```
117
+ */
118
+ if ( identifier . typeAnnotation ?. type === 'TSTypeAnnotation' && ! isNumberTypeAnnotation ( identifier . typeAnnotation ) ) {
119
+ return ;
120
+ }
121
+
122
+ /**
123
+ Capture the following statement
124
+
125
+ ```js
126
+ function foo(a = 10) {}
127
+ ```
128
+ */
129
+ if ( identifier . parent . type === 'AssignmentPattern' && identifier . parent . right . type === 'Literal' && typeof identifier . parent . right . value !== 'number' ) {
130
+ return ;
131
+ }
132
+
133
+ break ;
134
+ }
135
+
136
+ case 'Variable' : {
137
+ /** @type {import('estree').VariableDeclarator } */
138
+ const variableDeclarator = definition . node ;
139
+
140
+ /**
141
+ Capture the following statement
142
+
143
+ ```js
144
+ var foo: number
145
+ ```
146
+ */
147
+ if ( variableDeclarator . id . typeAnnotation ?. type === 'TSTypeAnnotation' && ! isNumberTypeAnnotation ( variableDeclarator . id . typeAnnotation ) ) {
148
+ return ;
149
+ }
150
+
151
+ /**
152
+ Capture the following statement
153
+
154
+ ```js
155
+ var foo = 10
156
+ ```
157
+ */
158
+ if ( variableDeclarator . init ?. type === 'Literal' && typeof variableDeclarator . init . value !== 'number' ) {
159
+ return ;
160
+ }
161
+
162
+ break ;
163
+ }
164
+
165
+ default :
166
+ }
167
+ }
168
+ }
169
+ }
170
+
64
171
return {
65
172
node : conditionalExpression ,
66
173
messageId : MESSAGE_ID ,
0 commit comments